Permalink
Browse files

ROO-4: Initial import of Spring Roo code.

  • Loading branch information...
0 parents commit 42ff85d7937e1416e5051dbaadd7be840bbfe1c6 Ben Alex committed May 15, 2009
Showing 376 changed files with 36,995 additions and 0 deletions.
@@ -0,0 +1,51 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.springframework.roo</groupId>
+ <artifactId>spring-roo</artifactId>
+ <version>1.0.0.M1</version>
+ </parent>
+ <artifactId>spring-roo-addon-beaninfo</artifactId>
+ <packaging>jar</packaging>
+ <name>Spring Roo - Addon - JavaBean Information</name>
+ <dependencies>
+ <!-- ROO modules -->
+ <dependency>
+ <groupId>org.springframework.roo</groupId>
+ <artifactId>spring-roo-classpath</artifactId>
+ <version>${roo.version}</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework.roo</groupId>
+ <artifactId>spring-roo-support</artifactId>
+ <version>${roo.version}</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework.roo</groupId>
+ <artifactId>spring-roo-metadata</artifactId>
+ <version>${roo.version}</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework.roo</groupId>
+ <artifactId>spring-roo-model</artifactId>
+ <version>${roo.version}</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework.roo</groupId>
+ <artifactId>spring-roo-project</artifactId>
+ <version>${roo.version}</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework.roo</groupId>
+ <artifactId>spring-roo-process-manager</artifactId>
+ <version>${roo.version}</version>
+ <scope>provided</scope>
+ </dependency>
+ </dependencies>
+</project>
@@ -0,0 +1,196 @@
+package org.springframework.roo.addon.beaninfo;
+
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.TreeMap;
+
+import org.springframework.roo.classpath.PhysicalTypeIdentifierNamingUtils;
+import org.springframework.roo.classpath.PhysicalTypeMetadata;
+import org.springframework.roo.classpath.details.FieldMetadata;
+import org.springframework.roo.classpath.details.MemberFindingUtils;
+import org.springframework.roo.classpath.details.MemberHoldingTypeDetails;
+import org.springframework.roo.classpath.details.MethodMetadata;
+import org.springframework.roo.classpath.itd.AbstractItdTypeDetailsProvidingMetadataItem;
+import org.springframework.roo.classpath.itd.ItdProviderRole;
+import org.springframework.roo.metadata.MetadataIdentificationUtils;
+import org.springframework.roo.model.JavaSymbolName;
+import org.springframework.roo.model.JavaType;
+import org.springframework.roo.project.Path;
+import org.springframework.roo.support.style.ToStringCreator;
+import org.springframework.roo.support.util.Assert;
+import org.springframework.roo.support.util.StringUtils;
+
+/**
+ * Metadata for {@link RooBeanInfo}.
+ *
+ * <p>
+ * Represents common information about beans, such as their accessors and mutators.
+ *
+ * <p>
+ * This metadata will introspect all {@link ItdProviderRole#ACCESSOR_MUTATOR} providers. As such those providers
+ * must not rely on details provided by this metadata (it is permitted, however, for those providers to register
+ * an additional trigger against this provider).
+ *
+ * @author Ben Alex
+ * @since 1.0
+ *
+ */
+public class BeanInfoMetadata extends AbstractItdTypeDetailsProvidingMetadataItem {
+
+ private static final String PROVIDES_TYPE_STRING = BeanInfoMetadata.class.getName();
+ private static final String PROVIDES_TYPE = MetadataIdentificationUtils.create(PROVIDES_TYPE_STRING);
+
+ private List<MemberHoldingTypeDetails> memberHoldingTypeDetails = new ArrayList<MemberHoldingTypeDetails>();
+
+ public BeanInfoMetadata(String identifier, JavaType aspectName, PhysicalTypeMetadata governorPhysicalTypeMetadata, List<MemberHoldingTypeDetails> memberHoldingTypeDetails) {
+ super(identifier, aspectName, governorPhysicalTypeMetadata, getJavaType(identifier));
+ Assert.isTrue(isValid(identifier), "Metadata identification string '" + identifier + "' does not appear to be a valid");
+ Assert.notNull(memberHoldingTypeDetails, "Member holding type details required");
+
+ if (!isValid()) {
+ return;
+ }
+
+ this.memberHoldingTypeDetails = memberHoldingTypeDetails;
+
+ // Create a representation of the desired output ITD
+ itdTypeDetails = builder.build();
+ }
+
+ /**
+ * Obtains the property name for the specified JavaBean accessor or mutator method. This is determined by
+ * discarding the first 2 or 3 letters of the method name (depending whether it is a "get", "set" or "is" method).
+ * There is no special searching back to the actual field name.
+ *
+ * @param methodMetadata to search (required, and must be a "get", "set" or "is" method)
+ * @return the name of the property (never returned null)
+ */
+ public JavaSymbolName getPropertyNameForJavaBeanMethod(MethodMetadata methodMetadata) {
+ Assert.notNull(methodMetadata, "Method metadata is required");
+ String name = methodMetadata.getMethodName().getSymbolName();
+ if (name.startsWith("set") || name.startsWith("get")) {
+ return new JavaSymbolName(StringUtils.uncapitalize(name.substring(3)));
+ }
+ if (name.startsWith("is")) {
+ return new JavaSymbolName(StringUtils.uncapitalize(name.substring(2)));
+ }
+ throw new IllegalStateException("Method name '" + name + "' does not observe JavaBean method naming conventions");
+ }
+
+ /**
+ * Attempts to locate the field which is represented by the presented property name.
+ *
+ * <p>
+ * Not every JavaBean getter or setter actually backs to a field with an identical name. In such
+ * cases, null will be returned.
+ *
+ * @param propertyName the property name (required)
+ * @return the field if found, or null if it could not be found
+ */
+ public FieldMetadata getFieldForPropertyName(JavaSymbolName propertyName) {
+ Assert.notNull(propertyName, "Property name required");
+ for (MemberHoldingTypeDetails holder : memberHoldingTypeDetails) {
+ FieldMetadata result = MemberFindingUtils.getDeclaredField(holder, propertyName);
+ if (result != null) {
+ return result;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * @return all public accessor methods defined by the class and all superclasses, sorted alphabetically (never null, but may be empty)
+ */
+ public List<MethodMetadata> getPublicAccessors() {
+ return getPublicAccessors(true);
+ }
+
+ /**
+ * @param sortByAccessorName indicated to sort the returned accessors by getter name
+ * @return all public accessor methods defined by the class and all superclasses (never null, but may be empty)
+ */
+ public List<MethodMetadata> getPublicAccessors(boolean sortByAccessorName) {
+ // We keep these in a TreeMap so the methods are output in alphabetic order
+ /** key: string based method name, value: MethodMetadata */
+ TreeMap<String, MethodMetadata> map = new TreeMap<String, MethodMetadata>();
+ List<MethodMetadata> sortedByDetectionOrder = new ArrayList<MethodMetadata>();
+
+ for (MemberHoldingTypeDetails holder : memberHoldingTypeDetails) {
+ for (MethodMetadata method : holder.getDeclaredMethods()) {
+ String accessorName = method.getMethodName().getSymbolName();
+ if (Modifier.isPublic(method.getModifier()) && method.getParameterTypes().size() == 0 && (accessorName.startsWith("get") || accessorName.startsWith("is"))) {
+ // We've got a public, no parameter, get|is method
+ if (sortByAccessorName) {
+ map.put(accessorName, method);
+ } else {
+ sortedByDetectionOrder.add(method);
+ }
+ }
+ }
+ }
+ if (sortByAccessorName) {
+ return new ArrayList<MethodMetadata>(map.values());
+ }
+ return sortedByDetectionOrder;
+ }
+
+ /**
+ * @return all public mutator methods defined by the class and all superclasses, sorted alphabetically (never null, but may be empty)
+ */
+ public List<MethodMetadata> getPublicMutators() {
+ // We keep these in a TreeMap so the methods are output in alphabetic order
+ /** key: string based method name, value: MethodMetadata */
+ TreeMap<String, MethodMetadata> map = new TreeMap<String, MethodMetadata>();
+ for (MemberHoldingTypeDetails holder : memberHoldingTypeDetails) {
+ for (MethodMetadata method : holder.getDeclaredMethods()) {
+ String mutatorName = method.getMethodName().getSymbolName();
+ if (Modifier.isPublic(method.getModifier()) && method.getParameterTypes().size() == 1 && mutatorName.startsWith("set")) {
+ // We've got a public, single parameter, set method
+ map.put(mutatorName, method);
+ }
+ }
+ }
+ return new ArrayList<MethodMetadata>(map.values());
+ }
+
+ /**
+ * @return the Java type that this metadata represents information for (never returns null)
+ */
+ public JavaType getJavaBean() {
+ return governorTypeDetails.getName();
+ }
+
+ public String toString() {
+ ToStringCreator tsc = new ToStringCreator(this);
+ tsc.append("identifier", getId());
+ tsc.append("valid", valid);
+ tsc.append("aspectName", aspectName);
+ tsc.append("destinationType", destination);
+ tsc.append("governor", governorPhysicalTypeMetadata.getId());
+ tsc.append("itdTypeDetails", itdTypeDetails);
+ return tsc.toString();
+ }
+
+ public static final String getMetadataIdentiferType() {
+ return PROVIDES_TYPE;
+ }
+
+ public static final String createIdentifier(JavaType javaType, Path path) {
+ return PhysicalTypeIdentifierNamingUtils.createIdentifier(PROVIDES_TYPE_STRING, javaType, path);
+ }
+
+ public static final JavaType getJavaType(String metadataIdentificationString) {
+ return PhysicalTypeIdentifierNamingUtils.getJavaType(PROVIDES_TYPE_STRING, metadataIdentificationString);
+ }
+
+ public static final Path getPath(String metadataIdentificationString) {
+ return PhysicalTypeIdentifierNamingUtils.getPath(PROVIDES_TYPE_STRING, metadataIdentificationString);
+ }
+
+ public static boolean isValid(String metadataIdentificationString) {
+ return PhysicalTypeIdentifierNamingUtils.isValid(PROVIDES_TYPE_STRING, metadataIdentificationString);
+ }
+
+
+}
@@ -0,0 +1,116 @@
+package org.springframework.roo.addon.beaninfo;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.springframework.roo.classpath.PhysicalTypeIdentifier;
+import org.springframework.roo.classpath.PhysicalTypeMetadata;
+import org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails;
+import org.springframework.roo.classpath.details.MemberHoldingTypeDetails;
+import org.springframework.roo.classpath.itd.AbstractItdMetadataProvider;
+import org.springframework.roo.classpath.itd.ItdMetadataProvider;
+import org.springframework.roo.classpath.itd.ItdProviderRole;
+import org.springframework.roo.classpath.itd.ItdRoleAwareMetadataProvider;
+import org.springframework.roo.classpath.itd.ItdTypeDetailsProvidingMetadataItem;
+import org.springframework.roo.metadata.MetadataDependencyRegistry;
+import org.springframework.roo.metadata.MetadataIdentificationUtils;
+import org.springframework.roo.metadata.MetadataItem;
+import org.springframework.roo.metadata.MetadataProvider;
+import org.springframework.roo.metadata.MetadataService;
+import org.springframework.roo.model.JavaType;
+import org.springframework.roo.process.manager.FileManager;
+import org.springframework.roo.project.Path;
+import org.springframework.roo.support.lifecycle.ScopeDevelopment;
+import org.springframework.roo.support.util.Assert;
+
+/**
+ * Provides {@link BeanInfoMetadata}.
+ *
+ * @author Ben Alex
+ * @since 1.0
+ *
+ */
+@ScopeDevelopment
+public final class BeanInfoMetadataProvider extends AbstractItdMetadataProvider {
+
+ public BeanInfoMetadataProvider(MetadataService metadataService, MetadataDependencyRegistry metadataDependencyRegistry, FileManager fileManager) {
+ super(metadataService, metadataDependencyRegistry, fileManager);
+ addMetadataTrigger(new JavaType(RooBeanInfo.class.getName()));
+ }
+
+ protected ItdTypeDetailsProvidingMetadataItem getMetadata(String metadataIdentificationString, JavaType aspectName, PhysicalTypeMetadata governorPhysicalTypeMetadata, String itdFilename) {
+ // Create a list of metadata which the metadata should look for accessors within
+ List<MemberHoldingTypeDetails> memberHoldingTypeDetails = new ArrayList<MemberHoldingTypeDetails>();
+
+ ClassOrInterfaceTypeDetails cid = (ClassOrInterfaceTypeDetails) governorPhysicalTypeMetadata.getPhysicalTypeDetails();
+
+ // Build a List representing the class hierarchy, where the first element is the absolute superclass
+ List<ClassOrInterfaceTypeDetails> cidHierarchy = new ArrayList<ClassOrInterfaceTypeDetails>();
+ while (cid != null) {
+ cidHierarchy.add(0, cid); // note to the top of the list
+ cid = cid.getSuperclass();
+ }
+
+ // Now we add this governor, plus all of its superclasses
+ for (ClassOrInterfaceTypeDetails currentClass : cidHierarchy) {
+ memberHoldingTypeDetails.add(currentClass);
+
+ // Add metadata representing accessors offered by other ITDs
+ for (MetadataProvider provider : metadataService.getRegisteredProviders()) {
+ // We're only interested in ITD providers which provide accessors
+ if (this.equals(provider) || !(provider instanceof ItdRoleAwareMetadataProvider) || !((ItdRoleAwareMetadataProvider)provider).getRoles().contains(ItdProviderRole.ACCESSOR_MUTATOR)) {
+ continue;
+ }
+
+ // Determine the key the ITD provider uses for this particular type
+ String key = ((ItdMetadataProvider)provider).getIdForPhysicalJavaType(currentClass.getDeclaredByMetadataId());
+ Assert.isTrue(MetadataIdentificationUtils.isIdentifyingInstance(key), "ITD metadata provider '" + provider + "' returned an illegal key ('" + key + "'");
+
+ // Register a dependency, as we need to know whenever an ITD changes its contents
+ // Only need to bother for our governor, though - superclasses trickle down to governor anyway, so we find out that way
+ if (currentClass.equals(governorPhysicalTypeMetadata.getPhysicalTypeDetails())) {
+ // Dealing with governor at the moment, so we should register
+ metadataDependencyRegistry.registerDependency(key, metadataIdentificationString);
+ }
+
+ // Get the metadata and ensure we have ITD type details available
+ MetadataItem metadataItem = metadataService.get(key);
+ if (metadataItem == null || !metadataItem.isValid()) {
+ continue;
+ }
+ Assert.isInstanceOf(ItdTypeDetailsProvidingMetadataItem.class, metadataItem, "ITD metadata provider '" + provider + "' failed to return the correct metadata type");
+ ItdTypeDetailsProvidingMetadataItem itdTypeDetailsMd = (ItdTypeDetailsProvidingMetadataItem) metadataItem;
+ if (itdTypeDetailsMd.getItdTypeDetails() == null) {
+ continue;
+ }
+
+ metadataDependencyRegistry.registerDependency(key, metadataIdentificationString);
+
+ // Include its accessors
+ memberHoldingTypeDetails.add(itdTypeDetailsMd.getItdTypeDetails());
+ }
+ }
+
+ return new BeanInfoMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, memberHoldingTypeDetails);
+ }
+
+ public String getItdUniquenessFilenameSuffix() {
+ return "BeanInfo";
+ }
+
+ protected String getGovernorPhysicalTypeIdentifier(String metadataIdentificationString) {
+ JavaType javaType = BeanInfoMetadata.getJavaType(metadataIdentificationString);
+ Path path = BeanInfoMetadata.getPath(metadataIdentificationString);
+ String physicalTypeIdentifier = PhysicalTypeIdentifier.createIdentifier(javaType, path);
+ return physicalTypeIdentifier;
+ }
+
+ protected String createLocalIdentifier(JavaType javaType, Path path) {
+ return BeanInfoMetadata.createIdentifier(javaType, path);
+ }
+
+ public String getProvidesType() {
+ return BeanInfoMetadata.getMetadataIdentiferType();
+ }
+
+}
@@ -0,0 +1,26 @@
+
+package org.springframework.roo.addon.beaninfo;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Provides basic information about JavaBeans.
+ *
+ * @author Ben Alex
+ * @since 1.0
+ *
+ */
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.SOURCE)
+public @interface RooBeanInfo {
+
+ /**
+ * @return the plural name to use when working with this object (defaults to an empty string, which means to compute
+ * dynamically)
+ */
+ String plural() default "";
+
+}
Oops, something went wrong.

0 comments on commit 42ff85d

Please sign in to comment.