Skip to content

Commit

Permalink
ROO-2803: Create add-on for displaying a pretty-print representation …
Browse files Browse the repository at this point in the history
…of a class - renamed add-on to addon-displaystring and method generated to getDisplayString()
  • Loading branch information
Alan Stewart committed Oct 7, 2011
1 parent 8c645bb commit 879c42f
Show file tree
Hide file tree
Showing 25 changed files with 127 additions and 241 deletions.

This file was deleted.

4 changes: 2 additions & 2 deletions addon-displayname/pom.xml → addon-displaystring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<version>1.2.0.BUILD-SNAPSHOT</version>
<relativePath>../osgi-roo-bundle</relativePath>
</parent>
<artifactId>org.springframework.roo.addon.displayname</artifactId>
<artifactId>org.springframework.roo.addon.displaystring</artifactId>
<packaging>bundle</packaging>
<name>Spring Roo - Addon - Display Name</name>
<name>Spring Roo - Addon - Display String</name>
<description>Provides a pretty string representation of a class for use in UIs</description>
<dependencies>
<!-- OSGi -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package org.springframework.roo.addon.displayname;
package org.springframework.roo.addon.displaystring;

import static org.springframework.roo.model.RooJavaType.ROO_DISPLAY_NAME;
import static org.springframework.roo.model.RooJavaType.ROO_DISPLAY_STRING;

import org.springframework.roo.classpath.PhysicalTypeMetadata;
import org.springframework.roo.classpath.details.annotations.populator.AbstractAnnotationValues;
import org.springframework.roo.classpath.details.annotations.populator.AutoPopulate;
import org.springframework.roo.classpath.details.annotations.populator.AutoPopulationUtils;

/**
* Represents a parsed {@link RooDisplayName} annotation.
* Represents a parsed {@link RooDisplayString} annotation.
*
* @author Alan Stewart
* @since 1.2.0
*/
public class DisplayNameAnnotationValues extends AbstractAnnotationValues {
public class DisplayStringAnnotationValues extends AbstractAnnotationValues {

// From annotation
@AutoPopulate private String methodName = "getDisplayName";
@AutoPopulate private String[] fields;
@AutoPopulate private String separator;

Expand All @@ -25,15 +24,11 @@ public class DisplayNameAnnotationValues extends AbstractAnnotationValues {
*
* @param governorPhysicalTypeMetadata
*/
public DisplayNameAnnotationValues(final PhysicalTypeMetadata governorPhysicalTypeMetadata) {
super(governorPhysicalTypeMetadata, ROO_DISPLAY_NAME);
public DisplayStringAnnotationValues(final PhysicalTypeMetadata governorPhysicalTypeMetadata) {
super(governorPhysicalTypeMetadata, ROO_DISPLAY_STRING);
AutoPopulationUtils.populate(this, annotationMetadata);
}

public String getMethodName() {
return methodName;
}

public String[] getFields() {
return fields;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.springframework.roo.addon.displayname;
package org.springframework.roo.addon.displaystring;

import static org.springframework.roo.model.JavaType.STRING;
import static org.springframework.roo.model.JdkJavaType.ARRAYS;
Expand Down Expand Up @@ -28,20 +28,20 @@
import org.springframework.roo.support.util.StringUtils;

/**
* Metadata for {@link RooDisplayName}.
* Metadata for {@link RooDisplayString}.
*
* @author Alan Stewart
* @since 1.2.0
*/
public class DisplayNameMetadata extends AbstractItdTypeDetailsProvidingMetadataItem {
public class DisplayStringMetadata extends AbstractItdTypeDetailsProvidingMetadataItem {

// Constants
private static final String PROVIDES_TYPE_STRING = DisplayNameMetadata.class.getName();
private static final String PROVIDES_TYPE_STRING = DisplayStringMetadata.class.getName();
private static final String PROVIDES_TYPE = MetadataIdentificationUtils.create(PROVIDES_TYPE_STRING);
private static final int MAX_LIST_VIEW_FIELDS = 4;

// Fields
private final DisplayNameAnnotationValues annotationValues;
private final DisplayStringAnnotationValues annotationValues;
private final List<MethodMetadata> locatedAccessors;
private final MethodMetadata identifierAccessor;
private String methodName;
Expand All @@ -56,7 +56,7 @@ public class DisplayNameMetadata extends AbstractItdTypeDetailsProvidingMetadata
* @param locatedAccessors
* @param identifierAccessor
*/
public DisplayNameMetadata(final String identifier, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final DisplayNameAnnotationValues annotationValues, final List<MethodMetadata> locatedAccessors, final MethodMetadata identifierAccessor) {
public DisplayStringMetadata(final String identifier, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final DisplayStringAnnotationValues annotationValues, final List<MethodMetadata> locatedAccessors, final MethodMetadata identifierAccessor) {
super(identifier, aspectName, governorPhysicalTypeMetadata);
Assert.isTrue(isValid(identifier), "Metadata identification string '" + identifier + "' does not appear to be a valid");
Assert.notNull(annotationValues, "Annotation values required");
Expand All @@ -66,11 +66,11 @@ public DisplayNameMetadata(final String identifier, final JavaType aspectName, f
this.locatedAccessors = locatedAccessors;
this.identifierAccessor = identifierAccessor;

// Generate the display name method
final MethodMetadata displayNameMethod = getDisplayNameMethod();
if (displayNameMethod != null) {
builder.addMethod(displayNameMethod);
methodName = displayNameMethod.getMethodName().getSymbolName();
// Generate the getDisplayString method
final MethodMetadata displayStringMethod = getDisplayStringMethod();
if (displayStringMethod != null) {
builder.addMethod(displayStringMethod);
methodName = displayStringMethod.getMethodName().getSymbolName();
}

// Create a representation of the desired output ITD
Expand All @@ -82,22 +82,14 @@ public String getMethodName() {
}

/**
* Obtains the display name method for this type, if available.
* Obtains the display string method for this type, if available.
* <p>
* If the user provided a non-default name for "getDisplayName", that method will be returned.
* If the user provided a non-default name for "getDisplayString", that method will be returned.
*
* @return the display name method declared on this type or that will be introduced (or null if undeclared and not introduced)
*/
private MethodMetadata getDisplayNameMethod() {
String displayNameMethod = annotationValues.getMethodName();
if (!StringUtils.hasText(displayNameMethod)) {
return null;
}

// Compute the relevant toString method name
JavaSymbolName methodName = new JavaSymbolName(displayNameMethod);

// See if the type itself declared the method
private MethodMetadata getDisplayStringMethod() {
JavaSymbolName methodName = new JavaSymbolName("getDisplayString");
if (getGovernorMethod(methodName) != null) {
return null;
}
Expand Down Expand Up @@ -136,10 +128,9 @@ private MethodMetadata getDisplayNameMethod() {
if (methodCount <= MAX_LIST_VIEW_FIELDS) {
methodCount++;
if (identifierAccessor != null && accessor.hasSameName(identifierAccessor)) {
displayMethods.add(0, accessorText);
} else {
displayMethods.add(accessorText);
continue;
}
displayMethods.add(accessorText);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.springframework.roo.addon.displaystring;

import org.springframework.roo.classpath.itd.ItdTriggerBasedMetadataProvider;

/**
* Provides {@link DisplayStringMetadata}.
*
* @author Alan Stewart
* @since 1.2.0
*/
public interface DisplayStringMetadataProvider extends ItdTriggerBasedMetadataProvider {}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.springframework.roo.addon.displayname;
package org.springframework.roo.addon.displaystring;

import static org.springframework.roo.addon.displayname.RooDisplayName.DISPLAY_NAME_DEFAULT;
import static org.springframework.roo.model.RooJavaType.ROO_DISPLAY_NAME;
import static org.springframework.roo.model.RooJavaType.ROO_DISPLAY_STRING;

import java.util.ArrayList;
import java.util.Comparator;
Expand All @@ -10,63 +9,45 @@
import java.util.TreeSet;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.osgi.service.component.ComponentContext;
import org.springframework.roo.classpath.PhysicalTypeIdentifier;
import org.springframework.roo.classpath.PhysicalTypeMetadata;
import org.springframework.roo.classpath.customdata.CustomDataKeys;
import org.springframework.roo.classpath.customdata.taggers.CustomDataKeyDecorator;
import org.springframework.roo.classpath.customdata.taggers.MethodMatcher;
import org.springframework.roo.classpath.details.BeanInfoUtils;
import org.springframework.roo.classpath.details.FieldMetadata;
import org.springframework.roo.classpath.details.ItdTypeDetails;
import org.springframework.roo.classpath.details.MethodMetadata;
import org.springframework.roo.classpath.itd.AbstractMemberDiscoveringItdMetadataProvider;
import org.springframework.roo.classpath.itd.ItdTypeDetailsProvidingMetadataItem;
import org.springframework.roo.classpath.scanner.MemberDetails;
import org.springframework.roo.model.JavaSymbolName;
import org.springframework.roo.model.JavaType;
import org.springframework.roo.project.Path;

/**
* Implementation of {@link DisplayNameMetadataProvider}.
* Implementation of {@link DisplayStringMetadataProvider}.
*
* @author Alan Stewart
* @since 1.2.0
*/
@Component(immediate = true)
@Service
public class DisplayNameMetadataProviderImpl extends AbstractMemberDiscoveringItdMetadataProvider implements DisplayNameMetadataProvider {

// Fields
@Reference private CustomDataKeyDecorator customDataKeyDecorator;
public class DisplayStringMetadataProviderImpl extends AbstractMemberDiscoveringItdMetadataProvider implements DisplayStringMetadataProvider {

protected void activate(final ComponentContext context) {
metadataDependencyRegistry.addNotificationListener(this);
metadataDependencyRegistry.registerDependency(PhysicalTypeIdentifier.getMetadataIdentiferType(), getProvidesType());
addMetadataTrigger(ROO_DISPLAY_NAME);
registerMatchers();
addMetadataTrigger(ROO_DISPLAY_STRING);
}

protected void deactivate(final ComponentContext context) {
metadataDependencyRegistry.removeNotificationListener(this);
metadataDependencyRegistry.deregisterDependency(PhysicalTypeIdentifier.getMetadataIdentiferType(), getProvidesType());
removeMetadataTrigger(ROO_DISPLAY_NAME);
customDataKeyDecorator.unregisterMatchers(getClass());
}

@SuppressWarnings("unchecked")
private void registerMatchers() {
customDataKeyDecorator.registerMatchers(
getClass(),
new MethodMatcher(CustomDataKeys.DISPLAY_NAME_METHOD, ROO_DISPLAY_NAME, new JavaSymbolName("methodName"), DISPLAY_NAME_DEFAULT)
);
removeMetadataTrigger(ROO_DISPLAY_STRING);
}

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
final DisplayNameAnnotationValues annotationValues = new DisplayNameAnnotationValues(governorPhysicalTypeMetadata);
final DisplayStringAnnotationValues annotationValues = new DisplayStringAnnotationValues(governorPhysicalTypeMetadata);
if (!annotationValues.isAnnotationFound()) {
return null;
}
Expand All @@ -84,7 +65,7 @@ protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataI

final MethodMetadata identifierAccessor = persistenceMemberLocator.getIdentifierAccessor(entity);

return new DisplayNameMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, annotationValues, locatedAccessors, identifierAccessor);
return new DisplayStringMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, annotationValues, locatedAccessors, identifierAccessor);
}

@Override
Expand Down Expand Up @@ -123,22 +104,22 @@ private boolean isApplicationType(final JavaType javaType) {
}

public String getItdUniquenessFilenameSuffix() {
return "DisplayName";
return "DisplayString";
}

@Override
protected String getGovernorPhysicalTypeIdentifier(final String metadataIdentificationString) {
JavaType javaType = DisplayNameMetadata.getJavaType(metadataIdentificationString);
Path path = DisplayNameMetadata.getPath(metadataIdentificationString);
JavaType javaType = DisplayStringMetadata.getJavaType(metadataIdentificationString);
Path path = DisplayStringMetadata.getPath(metadataIdentificationString);
return PhysicalTypeIdentifier.createIdentifier(javaType, path);
}

@Override
protected String createLocalIdentifier(final JavaType javaType, final Path path) {
return DisplayNameMetadata.createIdentifier(javaType, path);
return DisplayStringMetadata.createIdentifier(javaType, path);
}

public String getProvidesType() {
return DisplayNameMetadata.getMetadataIdentiferType();
return DisplayStringMetadata.getMetadataIdentiferType();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.springframework.roo.addon.displayname;
package org.springframework.roo.addon.displaystring;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand All @@ -13,15 +13,7 @@
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface RooDisplayName {

// Constants for the non-blank default attribute value
String DISPLAY_NAME_DEFAULT = "getDisplayName";

/**
* @return the name of the display name method to generate (defaults to "getDisplayName"; if empty, does not create)
*/
String methodName() default DISPLAY_NAME_DEFAULT;
public @interface RooDisplayString {

/**
* @return an array of fields to use in the display name method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static org.springframework.roo.model.GoogleJavaType.GAE_DATASTORE_KEY;
import static org.springframework.roo.model.JavaType.LONG_OBJECT;
import static org.springframework.roo.model.RooJavaType.ROO_DISPLAY_NAME;
import static org.springframework.roo.model.RooJavaType.ROO_DISPLAY_STRING;
import static org.springframework.roo.model.RooJavaType.ROO_EQUALS;
import static org.springframework.roo.model.RooJavaType.ROO_JAVA_BEAN;
import static org.springframework.roo.model.RooJavaType.ROO_SERIALIZABLE;
Expand Down Expand Up @@ -43,7 +43,7 @@ public class EntityCommands implements CommandMarker {
private static final AnnotationMetadataBuilder ROO_SERIALIZABLE_BUILDER = new AnnotationMetadataBuilder(ROO_SERIALIZABLE);
private static final AnnotationMetadataBuilder ROO_TO_STRING_BUILDER = new AnnotationMetadataBuilder(ROO_TO_STRING);
private static final AnnotationMetadataBuilder ROO_JAVA_BEAN_BUILDER = new AnnotationMetadataBuilder(ROO_JAVA_BEAN);
private static final AnnotationMetadataBuilder ROO_DISPLAY_NAME_BUILDER = new AnnotationMetadataBuilder(ROO_DISPLAY_NAME);
private static final AnnotationMetadataBuilder ROO_DISPLAY_STRING_BUILDER = new AnnotationMetadataBuilder(ROO_DISPLAY_STRING);

// Fields
@Reference private EntityOperations entityOperations;
Expand Down Expand Up @@ -74,13 +74,11 @@ public void newPersistenceClassJpa(
@CliOption(key = "mappedSuperclass", mandatory = false, specifiedDefaultValue = "true", unspecifiedDefaultValue = "false", help = "Apply @MappedSuperclass for this entity") final boolean mappedSuperclass,
@CliOption(key = "equals", mandatory = false, unspecifiedDefaultValue = "false", specifiedDefaultValue = "true", help = "Whether the generated class should implement equals and hashCode methods") final boolean equals,
@CliOption(key = "serializable", mandatory = false, unspecifiedDefaultValue = "false", specifiedDefaultValue = "true", help = "Whether the generated class should implement java.io.Serializable") final boolean serializable,
@CliOption(key = "displayName", mandatory = false, unspecifiedDefaultValue = "false", specifiedDefaultValue = "true", help = "Whether the generated class should provide a pretty-print method") final boolean displayName,
@CliOption(key = "persistenceUnit", mandatory = false, help = "The persistence unit name to be used in the persistence.xml file") final String persistenceUnit,
@CliOption(key = "transactionManager", mandatory = false, help = "The transaction manager name") final String transactionManager,
@CliOption(key = "permitReservedWords", mandatory = false, unspecifiedDefaultValue = "false", specifiedDefaultValue = "true", help = "Indicates whether reserved words are ignored by Roo") final boolean permitReservedWords,
@CliOption(key = "entityName", mandatory = false, help = "The name used to refer to the entity in queries") final String entityName,
@CliOption(key = "activeRecord", mandatory = false, specifiedDefaultValue = "true", unspecifiedDefaultValue = "true", help = "Generate CRUD active record methods for this entity") final boolean activeRecord)
{
@CliOption(key = "activeRecord", mandatory = false, specifiedDefaultValue = "true", unspecifiedDefaultValue = "true", help = "Generate CRUD active record methods for this entity") final boolean activeRecord) {
Assert.isTrue(!identifierType.isPrimitive(), "Identifier type cannot be a primitive");

if (!permitReservedWords) {
Expand All @@ -106,6 +104,7 @@ public void newPersistenceClassJpa(
final List<AnnotationMetadataBuilder> annotationBuilder = new ArrayList<AnnotationMetadataBuilder>();
annotationBuilder.add(ROO_JAVA_BEAN_BUILDER);
annotationBuilder.add(ROO_TO_STRING_BUILDER);
annotationBuilder.add(ROO_DISPLAY_STRING_BUILDER);
annotationBuilder.add(getEntityAnnotationBuilder(table, schema, catalog, identifierField, identifierColumn, identifierType, versionField, versionColumn, versionType, inheritanceType, mappedSuperclass, persistenceUnit, transactionManager, entityName, activeRecord));
if (equals) {
annotationBuilder.add(ROO_EQUALS_BUILDER);
Expand All @@ -114,9 +113,6 @@ public void newPersistenceClassJpa(
if (serializable) {
annotationBuilder.add(ROO_SERIALIZABLE_BUILDER);
}
if (displayName) {
annotationBuilder.add(ROO_DISPLAY_NAME_BUILDER);
}

// Produce the entity itself
entityOperations.newEntity(name, createAbstract, superclass, annotationBuilder);
Expand Down
Loading

0 comments on commit 879c42f

Please sign in to comment.