Permalink
Browse files

Format the Java sources upon file creation

All the formatting code is moved to the Formatter class. The AbstractJavaSource just uses it.
  • Loading branch information...
1 parent c9be5c8 commit 441f1ea3abffb73326520087e3dcc54e09e70d18 Ivan Ivanov committed May 2, 2012
@@ -119,4 +119,16 @@ public void assertEntityCreatedInPackageWhenWithinAPackage() throws Exception
assertEquals(entitySource.getPackage(), pkgName);
}
+
+ @Test
+ public void assertEntityClassDeclarationOnNewLine() throws Exception
+ {
+ Project project = getProject();
+ JavaClass javaClass = generateEntity(project);
+
+ String content = javaClass.toString();
+
+ assertTrue(content.contains("@Entity" + System.getProperty("line.separator") +
+ "public class"));
+ }
}
@@ -52,6 +52,7 @@
import org.jboss.forge.parser.java.ast.AnnotationAccessor;
import org.jboss.forge.parser.java.ast.ModifierAccessor;
import org.jboss.forge.parser.java.ast.TypeDeclarationFinderVisitor;
+import org.jboss.forge.parser.java.util.Formatter;
import org.jboss.forge.parser.java.util.Strings;
import org.jboss.forge.parser.java.util.Types;
import org.jboss.forge.parser.spi.WildcardImportResolver;
@@ -592,18 +593,8 @@ public String toString()
catch (Exception e) {
throw new ParserException("Could not modify source: " + unit.toString(), e);
}
-
- String documentString = document.get();
- return ensureCorrectNewLines(documentString);
- }
-
- private String ensureCorrectNewLines(String documentString) {
- String newLine = System.getProperty("line.separator");
-
- if (documentString.indexOf("\n") != -1 && documentString.indexOf(newLine) == -1)
- return documentString.replaceAll("\n", newLine);
-
- return documentString;
+
+ return Formatter.format(document.get());
}
@Override
@@ -41,19 +41,20 @@
{
public static String format(JavaClass javaClass)
{
- // TODO locate user's eclipse project settings, use those if we can.
- Properties options = readConfig("org.eclipse.jdt.core.prefs");
-
- final CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(options);
- String result = formatFile(javaClass, codeFormatter);
+ return format(javaClass.toString());
+ }
+
+ public static String format(String source)
+ {
+ // TODO locate user's eclipse project settings, use those if we can.
+ Properties options = readConfig("org.eclipse.jdt.core.prefs");
- return result;
+ final CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(options);
+ return ensureCorrectNewLines(formatFile(source, codeFormatter));
}
- private static String formatFile(JavaClass javaClass, CodeFormatter codeFormatter)
+ private static String formatFile(String contents, CodeFormatter codeFormatter)
{
- String contents = javaClass.toString();
-
IDocument doc = new Document(contents);
try
{
@@ -104,4 +105,16 @@ private static Properties readConfig(String filename)
}
}
}
+
+
+ private static String ensureCorrectNewLines(String documentString)
+ {
+ String newLine = System.getProperty("line.separator");
+
+ if (documentString.indexOf("\n") != -1 && documentString.indexOf(newLine) == -1)
+ return documentString.replaceAll("\n", newLine);
+
+ return documentString;
+ }
+
}
@@ -27,6 +27,8 @@
import static org.junit.Assert.assertTrue;
import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import org.jboss.forge.parser.java.Annotation;
import org.jboss.forge.parser.java.AnnotationTarget;
@@ -119,8 +121,9 @@ public void testCanAddAnnotationDuplicate() throws Exception
assertEquals(size + 2, annotations.size());
assertEquals(Test.class.getSimpleName(), anno1.getName());
assertEquals(Test.class.getSimpleName(), anno2.getName());
- String pattern = "@" + Test.class.getSimpleName() + " " + "@" + Test.class.getSimpleName();
- assertTrue(target.toString().contains(pattern));
+ Pattern pattern = Pattern.compile("@" + Test.class.getSimpleName() + "\\s*" + "@" + Test.class.getSimpleName());
+ Matcher matcher = pattern.matcher(target.toString());
+ assertTrue(matcher.find());
assertTrue(target.getOrigin().hasImport(Test.class));
}

0 comments on commit 441f1ea

Please sign in to comment.