Skip to content

Commit

Permalink
ROO-2745: Post 1.2.0.M1 code refactor and clean up - created constant…
Browse files Browse the repository at this point in the history
… JavaTypes, using entrySet vs keySet, removed duplicate code for generating unique field names
  • Loading branch information
Andrew Swan committed Sep 19, 2011
1 parent 7701cee commit 1b05429
Show file tree
Hide file tree
Showing 20 changed files with 247 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ private void addOtherFields(Table table) {

boolean hasEmbeddedIdField = isEmbeddedIdField(fieldName) && !isCompositeKeyField;
if (hasEmbeddedIdField) {
fieldName = getUniqueFieldName(fieldName);
fieldName = governorTypeDetails.getUniqueFieldName(fieldName.getSymbolName(), true);
}

field = getField(fieldName, column, table.getName(), table.isIncludeNonPortableAttributes());
Expand Down Expand Up @@ -726,26 +726,6 @@ private void addToBuilder(FieldMetadata field) {
}
}

private JavaSymbolName getUniqueFieldName(JavaSymbolName fieldName) {
int index = -1;
JavaSymbolName uniqueField = null;
while (true) {
// Compute the required field name
index++;
String uniqueFieldName = "";
for (int i = 0; i < index; i++) {
uniqueFieldName = uniqueFieldName + "_";
}
uniqueFieldName = uniqueFieldName + fieldName;
uniqueField = new JavaSymbolName(uniqueFieldName);
if (MemberFindingUtils.getField(governorTypeDetails, uniqueField) == null) {
// Found a usable field name
break;
}
}
return uniqueField;
}

private boolean hasField(FieldMetadata field) {
// Check governor for field
if (MemberFindingUtils.getField(governorTypeDetails, field.getFieldName()) != null) {
Expand Down Expand Up @@ -779,10 +759,14 @@ private boolean hasField(FieldMetadata field) {
return false;
}

private boolean hasFieldInItd(JavaSymbolName fieldName) {
// Check this ITD for field
List<FieldMetadataBuilder> declaredFields = builder.getDeclaredFields();
for (FieldMetadataBuilder declaredField : declaredFields) {
/**
* Indicates whether the ITD being built has a field of the given name
*
* @param fieldName
* @return
*/
private boolean hasFieldInItd(final JavaSymbolName fieldName) {
for (final FieldMetadataBuilder declaredField : builder.getDeclaredFields()) {
if (declaredField.getFieldName().equals(fieldName)) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.springframework.roo.addon.entity;

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_JAVA_BEAN;
import static org.springframework.roo.model.RooJavaType.ROO_SERIALIZABLE;
Expand Down Expand Up @@ -38,7 +39,6 @@ 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 JavaType GAE_DATASTORE_KEY = new JavaType("com.google.appengine.api.datastore.Key");

// Fields
@Reference private EntityOperations entityOperations;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.springframework.roo.addon.entity;

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.JdkJavaType.BIG_DECIMAL;
import static org.springframework.roo.model.JdkJavaType.CALENDAR;
Expand Down Expand Up @@ -244,32 +245,14 @@ private FieldMetadata getIdentifierField() {
return getIdentifierField(embeddedIdFields, EMBEDDED_ID);
}

final String identifierField = getIdentifierFieldName();

// Ensure there isn't already a field called "id"; if so, compute a unique name (it's not really a fatal situation at the end of the day)
int index= -1;
JavaSymbolName idField;
while (true) {
// Compute the required field name
index++;
String fieldName = "";
for (int i = 0; i < index; i++) {
fieldName = fieldName + "_";
}
fieldName = identifierField + fieldName;

idField = new JavaSymbolName(fieldName);
if (MemberFindingUtils.getField(governorTypeDetails, idField) == null) {
// Found a usable field name
break;
}
}
final JavaSymbolName idField = governorTypeDetails.getUniqueFieldName(getIdentifierFieldName(), false);

// We need to create one
final JavaType identifierType = getIdentifierType();

final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
final boolean hasIdClass = !(identifierType.getPackage().getFullyQualifiedPackageName().startsWith("java.") || identifierType.equals(new JavaType("com.google.appengine.api.datastore.Key")));
final boolean hasIdClass = !(identifierType.getPackage().getFullyQualifiedPackageName().startsWith("java.") || identifierType.equals(GAE_DATASTORE_KEY));
final JavaType annotationType = hasIdClass ? EMBEDDED_ID : ID;
annotations.add(new AnnotationMetadataBuilder(annotationType));

Expand Down Expand Up @@ -595,23 +578,7 @@ private FieldMetadata getVersionField() {
}

// Ensure there isn't already a field called "version"; if so, compute a unique name (it's not really a fatal situation at the end of the day)
int index= -1;
JavaSymbolName verField;
while (true) {
// Compute the required field name
index++;
String fieldName = "";
for (int i = 0; i < index; i++) {
fieldName = fieldName + "_";
}
fieldName = versionField + fieldName;

verField = new JavaSymbolName(fieldName);
if (MemberFindingUtils.getField(governorTypeDetails, verField) == null) {
// Found a usable field name
break;
}
}
final JavaSymbolName verField = governorTypeDetails.getUniqueFieldName(versionField, false);

// We're creating one
JavaType versionType = annotationValues.getVersionType();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package org.springframework.roo.addon.gwt;

import static org.springframework.roo.addon.gwt.GwtUtils.ACCEPTS_ONE_WIDGET;
import static org.springframework.roo.addon.gwt.GwtUtils.EVENT_BUS;
import static org.springframework.roo.addon.gwt.GwtUtils.PLACE;
import static org.springframework.roo.addon.gwt.GwtUtils.RECEIVER;
import static org.springframework.roo.model.JdkJavaType.COLLECTION;

import java.util.ArrayList;
Expand Down Expand Up @@ -135,14 +139,10 @@ public Map<JavaSymbolName, List<JavaType>> resolveMethodsToWatch(GwtType type) {
watchedMethods = new HashMap<JavaSymbolName, List<JavaType>>();
switch (type) {
case EDIT_ACTIVITY_WRAPPER:
List<JavaType> params = new ArrayList<JavaType>();
params.add(new JavaType("com.google.gwt.user.client.ui.AcceptsOneWidget"));
params.add(new JavaType("com.google.gwt.event.shared.EventBus"));
watchedMethods.put(new JavaSymbolName("start"), params);
watchedMethods.put(new JavaSymbolName("start"), Arrays.asList(ACCEPTS_ONE_WIDGET, EVENT_BUS));
break;
case DETAIL_ACTIVITY:
params = Arrays.asList(new JavaType("com.google.web.bindery.requestfactory.shared.Receiver", 0, DataType.TYPE, null, Collections.singletonList(GwtUtils.ENTITY_PROXY)));
watchedMethods.put(new JavaSymbolName("find"), params);
watchedMethods.put(new JavaSymbolName("find"), Arrays.asList(GwtUtils.getReceiverType(GwtUtils.ENTITY_PROXY)));
watchedMethods.put(new JavaSymbolName("deleteClicked"), new ArrayList<JavaType>());
break;
case MOBILE_LIST_VIEW:
Expand All @@ -152,14 +152,13 @@ public Map<JavaSymbolName, List<JavaType>> resolveMethodsToWatch(GwtType type) {
watchedMethods.put(new JavaSymbolName("init"), new ArrayList<JavaType>());
break;
case MASTER_ACTIVITIES:
watchedMethods.put(new JavaSymbolName("getActivity"), Collections.singletonList(new JavaType("com.google.gwt.place.shared.Place")));
watchedMethods.put(new JavaSymbolName("getActivity"), Collections.singletonList(PLACE));
break;
case DETAILS_ACTIVITIES:
watchedMethods.put(new JavaSymbolName("getActivity"), Collections.singletonList(new JavaType("com.google.gwt.place.shared.Place")));
watchedMethods.put(new JavaSymbolName("getActivity"), Collections.singletonList(PLACE));
break;
case LIST_ACTIVITY:
params = Arrays.asList(new JavaType("com.google.web.bindery.requestfactory.shared.Receiver"));
watchedMethods.put(new JavaSymbolName("fireCountRequest"), params);
watchedMethods.put(new JavaSymbolName("fireCountRequest"), Collections.singletonList(RECEIVER));
break;
}
return watchedMethods;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.springframework.roo.addon.gwt;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -13,6 +14,7 @@
import org.springframework.roo.classpath.details.annotations.BooleanAttributeValue;
import org.springframework.roo.classpath.details.annotations.ClassAttributeValue;
import org.springframework.roo.classpath.details.annotations.StringAttributeValue;
import org.springframework.roo.model.DataType;
import org.springframework.roo.model.JavaType;
import org.springframework.roo.model.RooJavaType;
import org.springframework.roo.project.ProjectMetadata;
Expand All @@ -26,23 +28,38 @@
public final class GwtUtils {

public static final String PROXY_REQUEST_WARNING = "// WARNING: THIS FILE IS MANAGED BY SPRING ROO.\n\n";
public static final JavaType LOCATOR = new JavaType("com.google.web.bindery.requestfactory.shared.Locator");

// JavaTypes
public static final JavaType ACCEPTS_ONE_WIDGET = new JavaType("com.google.gwt.user.client.ui.AcceptsOneWidget");
public static final JavaType ENTITY_PROXY = new JavaType("com.google.web.bindery.requestfactory.shared.EntityProxy");
public static final JavaType REQUEST_CONTEXT = new JavaType("com.google.web.bindery.requestfactory.shared.RequestContext");
public static final JavaType EVENT_BUS = new JavaType("com.google.gwt.event.shared.EventBus");
public static final JavaType INSTANCE_REQUEST = new JavaType("com.google.web.bindery.requestfactory.shared.InstanceRequest");
public static final JavaType LOCATOR = new JavaType("com.google.web.bindery.requestfactory.shared.Locator");
public static final JavaType OLD_ENTITY_PROXY = new JavaType("com.google.gwt.requestfactory.shared.EntityProxy");
public static final JavaType OLD_REQUEST_CONTEXT = new JavaType("com.google.gwt.requestfactory.shared.RequestContext");
public static final JavaType REQUEST = new JavaType("com.google.web.bindery.requestfactory.shared.Request");
public static final JavaType INSTANCE_REQUEST = new JavaType("com.google.web.bindery.requestfactory.shared.InstanceRequest");
public static final JavaType PROXY_FOR_NAME = new JavaType("com.google.web.bindery.requestfactory.shared.ProxyForName");
public static final JavaType PLACE = new JavaType("com.google.gwt.place.shared.Place");
public static final JavaType PROXY_FOR = new JavaType("com.google.web.bindery.requestfactory.shared.ProxyFor");
public static final JavaType SERVICE_NAME = new JavaType("com.google.web.bindery.requestfactory.shared.ServiceName");
public static final JavaType PROXY_FOR_NAME = new JavaType("com.google.web.bindery.requestfactory.shared.ProxyForName");
public static final JavaType RECEIVER = new JavaType("com.google.web.bindery.requestfactory.shared.Receiver");
public static final JavaType REQUEST = new JavaType("com.google.web.bindery.requestfactory.shared.Request");
public static final JavaType REQUEST_CONTEXT = new JavaType("com.google.web.bindery.requestfactory.shared.RequestContext");
public static final JavaType SERVICE = new JavaType("com.google.web.bindery.requestfactory.shared.Service");
public static final JavaType SERVICE_NAME = new JavaType("com.google.web.bindery.requestfactory.shared.ServiceName");

public static final JavaType[] PROXY_ANNOTATIONS = {PROXY_FOR, PROXY_FOR_NAME};
public static final JavaType[] REQUEST_ANNOTATIONS = {SERVICE, SERVICE_NAME};
public static final JavaType[] ROO_PROXY_REQUEST_ANNOTATIONS = {RooJavaType.ROO_GWT_PROXY, RooJavaType.ROO_GWT_REQUEST, RooJavaType.ROO_GWT_MIRRORED_FROM};

private GwtUtils() {
}

/**
* Returns the {@link #WEB_RECEIVER} Java type, generically typed to the
* given type.
*
* @param genericType (required)
* @return a non-<code>null</code> type
*/
public static JavaType getReceiverType(final JavaType genericType) {
return new JavaType(RECEIVER.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Collections.singletonList(genericType));
}

public static Map<GwtType, JavaType> getMirrorTypeMap(ProjectMetadata projectMetadata, JavaType governorType) {
Map<GwtType, JavaType> mirrorTypeMap = new HashMap<GwtType, JavaType>();
Expand Down Expand Up @@ -163,4 +180,9 @@ public static JavaType convertPrimitiveType(JavaType type, boolean convertVoid)
}
return type;
}

/**
* Constructor is private to prevent instantiation
*/
private GwtUtils() {}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.springframework.roo.addon.javabean;

import static org.springframework.roo.model.GoogleJavaType.GAE_DATASTORE_KEY;
import static org.springframework.roo.model.GoogleJavaType.GAE_DATASTORE_KEY_FACTORY;
import static org.springframework.roo.model.JavaType.LONG_OBJECT;
import static org.springframework.roo.model.JdkJavaType.ARRAY_LIST;
import static org.springframework.roo.model.JdkJavaType.HASH_SET;
Expand All @@ -23,7 +25,6 @@
import org.springframework.roo.classpath.details.DeclaredFieldAnnotationDetails;
import org.springframework.roo.classpath.details.FieldMetadata;
import org.springframework.roo.classpath.details.FieldMetadataBuilder;
import org.springframework.roo.classpath.details.MemberFindingUtils;
import org.springframework.roo.classpath.details.MethodMetadata;
import org.springframework.roo.classpath.details.MethodMetadataBuilder;
import org.springframework.roo.classpath.details.annotations.AnnotatedJavaType;
Expand Down Expand Up @@ -84,11 +85,11 @@ public JavaBeanMetadata(String identifier, JavaType aspectName, PhysicalTypeMeta
if (entry.getValue() != null) {
JavaSymbolName hiddenIdFieldName;
if (field.getFieldType().isCommonCollectionType()) {
hiddenIdFieldName = getFieldName(field.getFieldName().getSymbolName() + "Keys");
builder.getImportRegistrationResolver().addImport(new JavaType("com.google.appengine.api.datastore.KeyFactory"));
hiddenIdFieldName = governorTypeDetails.getUniqueFieldName(field.getFieldName().getSymbolName() + "Keys", true);
builder.getImportRegistrationResolver().addImport(GAE_DATASTORE_KEY_FACTORY);
builder.addField(getMultipleEntityIdField(hiddenIdFieldName));
} else {
hiddenIdFieldName = getFieldName(field.getFieldName().getSymbolName() + "Id");
hiddenIdFieldName = governorTypeDetails.getUniqueFieldName(field.getFieldName().getSymbolName() + "Id", true);
builder.addField(getSingularEntityIdField(hiddenIdFieldName));
}

Expand Down Expand Up @@ -217,33 +218,14 @@ private void processGaeAnnotations(FieldMetadata field) {
}
}

private JavaSymbolName getFieldName(String fieldName) {
int index = -1;
while (true) {
// Compute the required field name
index++;
String tempString = "";
for (int i = 0; i < index; i++) {
tempString = tempString + "_";
}
fieldName = tempString + fieldName;

JavaSymbolName field = new JavaSymbolName(fieldName);
if (MemberFindingUtils.getField(governorTypeDetails, field) == null) {
// Found a usable field name
return field;
}
}
}

private FieldMetadata getSingularEntityIdField(JavaSymbolName fieldName) {
FieldMetadataBuilder fieldMetadataBuilder = new FieldMetadataBuilder(getId(), Modifier.PRIVATE, fieldName, LONG_OBJECT, null);
return fieldMetadataBuilder.build();
}

private FieldMetadata getMultipleEntityIdField(JavaSymbolName fieldName) {
builder.getImportRegistrationResolver().addImport(HASH_SET);
FieldMetadataBuilder fieldMetadataBuilder = new FieldMetadataBuilder(getId(), Modifier.PRIVATE, fieldName, new JavaType(SET.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Collections.singletonList(new JavaType("com.google.appengine.api.datastore.Key"))), "new HashSet<Key>()");
FieldMetadataBuilder fieldMetadataBuilder = new FieldMetadataBuilder(getId(), Modifier.PRIVATE, fieldName, new JavaType(SET.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Collections.singletonList(GAE_DATASTORE_KEY)), "new HashSet<Key>()");
return fieldMetadataBuilder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ private void updatePersistenceXml(final OrmProvider ormProvider, final JdbcDatab
properties.appendChild(createPropertyElement("datanucleus.ConnectionPassword", password, persistence));
}

properties.appendChild(createPropertyElement("datanucleus.autoCreateTables", new Boolean(!isDbreProject).toString(), persistence));
properties.appendChild(createPropertyElement("datanucleus.autoCreateTables", Boolean.toString(!isDbreProject), persistence));
properties.appendChild(createPropertyElement("datanucleus.autoCreateColumns", "false", persistence));
properties.appendChild(createPropertyElement("datanucleus.autoCreateConstraints", "false", persistence));
properties.appendChild(createPropertyElement("datanucleus.validateTables", "false", persistence));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Properties;

import org.junit.Before;
import org.mockito.Mock;
Expand Down Expand Up @@ -40,7 +39,6 @@ public class JpaOperationsImplTest {

// Fixture
private JpaOperationsImpl jpaOperations;
private Properties dialects = new Properties(); // too hard to mock
@Mock private FileManager mockFileManager;
@Mock private PathResolver mockPathResolver;
@Mock private ProjectOperations mockProjectOperations;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.springframework.roo.addon.test;

import static org.springframework.roo.model.GoogleJavaType.GAE_LOCAL_SERVICE_TEST_HELPER;
import static org.springframework.roo.model.JdkJavaType.CALENDAR;
import static org.springframework.roo.model.JdkJavaType.DATE;
import static org.springframework.roo.model.JdkJavaType.GREGORIAN_CALENDAR;
Expand Down Expand Up @@ -145,7 +146,7 @@ private void addRequiredIntegrationTestClassIntroductions(JavaType dodGovernor)

private void addOptionalIntegrationTestClassIntroductions() {
// Add the GAE test helper field if the user did not define it on the governor directly
JavaType helperType = new JavaType("com.google.appengine.tools.development.testing.LocalServiceTestHelper");
final JavaType helperType = GAE_LOCAL_SERVICE_TEST_HELPER;
FieldMetadata helperField = MemberFindingUtils.getField(governorTypeDetails, new JavaSymbolName("helper"));
if (helperField != null) {
Assert.isTrue(helperField.getFieldType().getFullyQualifiedTypeName().equals(helperType.getFullyQualifiedTypeName()), "Field 'helper' on '" + destination.getFullyQualifiedTypeName() + "' must be of type '" + helperType.getFullyQualifiedTypeName() + "'");
Expand Down
Loading

0 comments on commit 1b05429

Please sign in to comment.