Last active
August 9, 2016 03:16
-
-
Save edinhojorge/65cb1c49dfe3f14094c0f0fd18a0ca5f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package csc; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.util.ArrayList; | |
import java.util.Comparator; | |
import java.util.List; | |
import org.jboss.forge.roaster.Roaster; | |
import org.jboss.forge.roaster.model.source.FieldSource; | |
import org.jboss.forge.roaster.model.source.JavaClassSource; | |
import org.jboss.forge.roaster.model.source.JavaSource; | |
import org.jboss.forge.roaster.model.source.MethodSource; | |
import org.jboss.forge.roaster.model.source.ParameterSource; | |
public class ReorganizeJavaClass { | |
private static JavaClassSource organizeClass(JavaClassSource javaClass){ | |
JavaClassSource javaClass2 = Roaster.create(JavaClassSource.class); | |
javaClass2.setPackage(javaClass.getPackage()).setName(javaClass.getName()); | |
List<FieldSource<JavaClassSource>> fields = javaClass.getFields(); | |
for (FieldSource<JavaClassSource> fieldSource : fields) { | |
FieldSource<JavaClassSource> newField = javaClass2.addField(); | |
newField.setName(fieldSource.getName()); | |
newField.setType(fieldSource.getType().toString()); | |
newField.setLiteralInitializer(fieldSource.getLiteralInitializer()); | |
} | |
List<MethodSource<JavaClassSource>> methods = javaClass.getMethods(); | |
List<MethodSource<JavaClassSource>> allMethods = new ArrayList<MethodSource<JavaClassSource>>(methods); | |
allMethods.sort(new Comparator<MethodSource<JavaClassSource>>() { | |
@Override | |
public int compare(MethodSource<JavaClassSource> o1, | |
MethodSource<JavaClassSource> o2) { | |
// TODO Auto-generated method stub | |
return o1.getName().compareTo(o2.getName()); | |
} | |
}); | |
// first put the constructors | |
for (MethodSource<JavaClassSource> methodSource : allMethods) { | |
if (!methodSource.isConstructor()){ | |
continue; | |
} | |
MethodSource<JavaClassSource> newMethod = javaClass2.addMethod(); | |
newMethod.setConstructor(true) | |
.setBody(methodSource.getBody()); | |
List<ParameterSource<JavaClassSource>> parameters = methodSource.getParameters(); | |
for (ParameterSource<JavaClassSource> parameterSource : parameters) { | |
newMethod.addParameter(parameterSource.getType().toString(), parameterSource.getName()); | |
} | |
} | |
// Then put other methods | |
for (MethodSource<JavaClassSource> methodSource : allMethods) { | |
if (methodSource.isConstructor()){ | |
continue; | |
} | |
MethodSource<JavaClassSource> newMethod = javaClass2.addMethod(); | |
newMethod.setName(methodSource.getName()); | |
newMethod.setBody(methodSource.getBody()); | |
newMethod.setReturnType(methodSource.getReturnType().toString()); | |
List<ParameterSource<JavaClassSource>> parameters = methodSource.getParameters(); | |
for (ParameterSource<JavaClassSource> parameterSource : parameters) { | |
newMethod.addParameter(parameterSource.getType().toString(), parameterSource.getName()); | |
} | |
} | |
List<JavaSource<?>> allNestedClasses = javaClass.getNestedTypes(); | |
List<JavaSource<?>> nestedClasses = new ArrayList<JavaSource<?>>(allNestedClasses); | |
nestedClasses.sort(new Comparator<JavaSource<?>>() { | |
@Override | |
public int compare(JavaSource<?> o1, | |
JavaSource<?> o2) { | |
// TODO Auto-generated method stub | |
return o1.getName().compareTo(o2.getName()); | |
} | |
}); | |
for (JavaSource<?> nestedClass : nestedClasses) { | |
JavaClassSource jcs = (JavaClassSource) nestedClass; | |
JavaClassSource newJcs = organizeClass(jcs); | |
javaClass2.addNestedType(newJcs); | |
} | |
return javaClass2; | |
} | |
public static void main(String[] args) { | |
try { | |
String javaClassFile = "/Users/eder/Documents/workspace-ggts-3.6.4.RELEASE/CompareSourceCodes/src/csc/FirstClass.java"; | |
JavaClassSource javaClass = | |
(JavaClassSource) Roaster.parse(new File(javaClassFile)); | |
JavaClassSource javaClass2 = organizeClass(javaClass); | |
System.out.println(javaClass2); | |
} catch (FileNotFoundException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment