Permalink
Browse files

Applied Forge 1.x changes

  • Loading branch information...
1 parent c7911fd commit ac6629a25c2c043490eda55750f97efa62900e74 @gastaldi gastaldi committed Apr 1, 2013
View
31 api/src/main/java/org/jboss/forge/parser/java/util/Refactory.java
@@ -15,9 +15,9 @@
import org.jboss.forge.parser.java.Method;
/**
- *
+ *
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
- *
+ *
*/
public class Refactory
{
@@ -36,7 +36,8 @@ public static void createGetterAndSetter(final JavaClass clazz, final Field<Java
.setPublic()
.setBody("return this." + fieldName + ";");
- if (!field.isFinal()) {
+ if (!field.isFinal())
+ {
clazz.addMethod().setReturnTypeVoid().setName("set" + methodNameSuffix).setPublic()
.setParameters("final " + field.getTypeInspector().toString() + " " + fieldName)
.setBody("this." + fieldName + " = " + fieldName + ";");
@@ -76,7 +77,9 @@ public static void createToStringFromFields(final JavaClass clazz, final Field<J
public static void createToStringFromFields(final JavaClass clazz, final List<Field<JavaClass>> fields)
{
- Method<JavaClass> method = clazz.addMethod().setName("toString").setReturnType(String.class).setPublic();
+ Method<JavaClass> method = clazz.addMethod().setName("toString").setReturnType(String.class)
+ .setPublic();
+ method.addAnnotation(Override.class);
List<String> list = new ArrayList<String>();
@@ -85,21 +88,29 @@ public static void createToStringFromFields(final JavaClass clazz, final List<Fi
{
if (clazz.hasField(field))
{
- String line = "";
+ StringBuilder line = new StringBuilder();
if (!field.isPrimitive())
if (field.isType(String.class))
- line += "if(" + field.getName() + " != null && !" + field.getName() + ".trim().isEmpty())\n";
+ {
+ line.append("if(").append(field.getName()).append(" != null && !").append(field.getName())
+ .append(".trim().isEmpty())\n");
+ }
else
- line += "if(" + field.getName() + " != null)\n";
+ {
+ line.append("if(").append(field.getName()).append(" != null)\n");
+ }
+
+ boolean isFirst = list.isEmpty();
- line += " result += " + (list.isEmpty() ? "" : "\" \" + ") + field.getName() + ";";
+ line.append(" result += ").append(isFirst ? "\"" : "\", ");
+ line.append(field.getName()).append(": \" + ").append(field.getName()).append(";");
- list.add(line);
+ list.add(line.toString());
}
}
- String body = "String result = \"\";\n" +
+ String body = "String result = getClass().getSimpleName()+\" \";\n" +
Strings.join(list, delimeter) + "\n" +
"return result;";
method.setBody(body);
View
7 api/src/main/java/org/jboss/forge/parser/java/util/Types.java
@@ -216,12 +216,7 @@ public static boolean isJavaLang(final String type)
{
return true;
}
- for (String t : LANG_TYPES)
- {
- if (type.endsWith(t))
- return true;
- }
- return false;
+ return LANG_TYPES.contains(type);
}
public static boolean isBasicType(String idType)
View
27 impl/src/main/java/org/jboss/forge/parser/java/impl/ImportImpl.java
@@ -105,26 +105,13 @@ public int hashCode()
public boolean equals(final Object obj)
{
if (this == obj)
- {
- return true;
- }
- if (obj == null)
- {
- return false;
- }
- if (getClass() != obj.getClass())
- {
- return false;
- }
- ImportImpl other = (ImportImpl) obj;
- if (imprt == null)
- {
- if (other.imprt != null)
- {
- return false;
- }
- }
- else if (!imprt.equals(other.imprt))
+ return true;
+
+ if (!(obj instanceof Import))
+ return false;
+
+ Import other = (Import) obj;
+ if (!getQualifiedName().equals(other.getQualifiedName()))
{
return false;
}
View
2 impl/src/main/java/org/jboss/forge/parser/java/impl/JavaEnumImpl.java
@@ -41,7 +41,7 @@ public JavaEnumImpl(JavaSource<?> enclosingType, final Document document, final
for (Object o : (((EnumDeclaration) getBodyDeclaration()).enumConstants()))
{
EnumConstantDeclaration field = (EnumConstantDeclaration) o;
- result.add(new EnumConstantImpl<JavaEnum>((JavaEnum) this, field));
+ result.add(new EnumConstantImpl<JavaEnum>(this, field));
}
return Collections.unmodifiableList(result);
View
3 impl/src/test/java/org/jboss/forge/test/parser/java/FieldTest.java
@@ -8,6 +8,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.InputStream;
@@ -38,7 +39,7 @@ public void reset()
@Test
public void testParse() throws Exception
{
- assertTrue(field instanceof Field);
+ assertNotNull(field);
assertEquals("field", field.getName());
assertEquals("String", field.getType());
}
View
108 impl/src/test/java/org/jboss/forge/test/parser/java/ImportEqualityTest.java
@@ -0,0 +1,108 @@
+package org.jboss.forge.test.parser.java;
+
+import static org.junit.Assert.*;
+
+import org.jboss.forge.parser.JavaParser;
+import org.jboss.forge.parser.java.Import;
+import org.jboss.forge.parser.java.impl.ImportImpl;
+import org.junit.Test;
+
+public class ImportEqualityTest {
+
+ private static final String DEFAULT_IMPORT_PACKAGE = "org.jboss.forge.parser.java";
+ private static final String DEFAULT_IMPORT_CLASS = "Import";
+ private static final String DEFAULT_IMPORT = DEFAULT_IMPORT_PACKAGE + "." + DEFAULT_IMPORT_CLASS;
+
+ @Test
+ public void testImportEqualsAnotherInstance() throws Exception {
+ Import firstImport = buildImport(DEFAULT_IMPORT);
+ Import secondImport = buildImport(DEFAULT_IMPORT);
+ assertEquals(firstImport, secondImport);
+ assertEquals(secondImport, firstImport);
+ }
+
+ @Test
+ public void testEqualsReturnsFalseForDifferentImport() throws Exception {
+ Import classImport = buildImport(DEFAULT_IMPORT);
+ Import interfaceImport = buildImport("org.jboss.forge.parser.java.impl.ImportImpl");
+ assertFalse(classImport.equals(interfaceImport));
+ }
+
+ @Test
+ public void testEqualsIsReflexive() throws Exception {
+ Import reflexiveImport = buildImport(DEFAULT_IMPORT);
+ assertEquals(reflexiveImport, reflexiveImport);
+ }
+
+ @Test
+ public void testEqualsIsTransitive() throws Exception {
+ Import firstImport = buildImport(DEFAULT_IMPORT);
+ Import secondImport = buildImport(DEFAULT_IMPORT);
+ Import thirdImport = buildImport(DEFAULT_IMPORT);
+ assertEquals(firstImport, secondImport);
+ assertEquals(secondImport, thirdImport);
+ assertEquals(firstImport, thirdImport);
+ }
+
+ @Test
+ public void testNotEqualToNull() throws Exception {
+ assertFalse(buildImport(DEFAULT_IMPORT).equals(null));
+ }
+
+ @Test
+ public void testNotEqualsToDifferentClass() throws Exception {
+ Import myImport = buildImport(DEFAULT_IMPORT);
+ assertFalse(myImport.equals(DEFAULT_IMPORT));
+ }
+
+ @Test
+ public void testEqualsToDifferentImportImplementation() throws Exception {
+ Import myImport = buildImport(DEFAULT_IMPORT);
+ Import mockImport = new MockImportImpl(DEFAULT_IMPORT_PACKAGE, DEFAULT_IMPORT_CLASS);
+ assertEquals(myImport, mockImport);
+ }
+
+ private class MockImportImpl implements Import {
+
+ private String packageName;
+ private String simpleName;
+
+ private MockImportImpl(String packageName, String simpleName) {
+ this.packageName = packageName;
+ this.simpleName = simpleName;
+ }
+
+ @Override
+ public Object getInternal() { return null; }
+
+ @Override
+ public String getPackage() { return packageName; }
+
+ @Override
+ public String getSimpleName() { return simpleName; }
+
+ @Override
+ public String getQualifiedName() { return packageName + "." + simpleName; }
+
+ @Override
+ public Import setName(String name) {
+ this.packageName = name.substring(0, name.lastIndexOf("."));
+ this.simpleName = name.substring(name.lastIndexOf(".") + 1);
+ return this;
+ }
+
+ @Override
+ public boolean isStatic() { return false; }
+
+ @Override
+ public Import setStatic(boolean value) { return this; }
+
+ @Override
+ public boolean isWildcard() { return false; }
+
+ }
+
+ private Import buildImport(String importName) {
+ return new ImportImpl(JavaParser.parse("public class MockClass {}")).setName(importName);
+ }
+}
View
3 impl/src/test/java/org/jboss/forge/test/parser/java/JavaEnumFieldTest.java
@@ -8,6 +8,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.InputStream;
@@ -38,7 +39,7 @@ public void reset()
@Test
public void testParse() throws Exception
{
- assertTrue(field instanceof Field);
+ assertNotNull(field);
assertEquals("field", field.getName());
assertEquals("String", field.getType());
}
View
12 impl/src/test/java/org/jboss/forge/test/parser/java/util/TypesTest.java
@@ -130,4 +130,16 @@ public void testGenerics()
assertEquals("java.lang.Class[]", Types.stripGenerics("java.lang.Class<E extends Enum<E>>[]"));
}
+ @Test
+ public void testStringIsJavaLang()
+ {
+ assertTrue(Types.isJavaLang("String"));
+ }
+
+ @Test
+ public void testAssertClassIsNotJavaLang()
+ {
+ assertFalse(Types.isJavaLang("AssertClass"));
+ }
+
}

0 comments on commit ac6629a

Please sign in to comment.