Programmatic Compilation with Java
public class TestClass {
public static void main(String[] args) {
TestClass testClass = new TestClass();
testClass.processString("String");
}
private void processString(String str){
for(int count=0; count<10;count++){
String myStr = "";
myStr = str;
}
System.out.println("Finished." + str);
}
}
CodeCompiler.java
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import javax.tools.JavaCompiler.CompilationTask;
import sun.tools.java.ClassFile;
public class CodeCompiler {
public static void main(String[] args) {
try{
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(
null, null, null);
Iterable compilationUnits1 = fileManager
.getJavaFileObjects("D:\\TestClass.java");
CompilationTask task = compiler.getTask(null, fileManager, null, null,
null, compilationUnits1);
// Perform the compilation task.
task.call();
fileManager.close();
ClassFile cf = new ClassFile(new File("TestClass.class"));
validateDefaultMethods(cf);
}catch(IOException ioe){
ioe.printStackTrace();
}
}
protected static void validateDefaultMethods(ClassFile clazzInfo) {
boolean hasEquals = false;
boolean hasHashCode = false;
boolean hasToString = false;
for (Method method : clazzInfo.getClass().getMethods()) {
String methodName = method.getName();
Class[] types = method.getParameterTypes();
if ("equals".equals(methodName) && types.length == 1) {
if ("java.lang.Object".equals(types[0])) {
hasEquals = true;
}
} else if ("hashCode".equals(methodName) &&
types.length == 0) {
hasHashCode = true;
}else if("toString".equals(methodName)&&
types.length == 0){
hasToString = true;
}
}
if(hasEquals&&hasHashCode&&hasToString){
System.out.println("Valid Code");
}else{
System.out.println("Invalid Code");
}
}
}
You may have to add tools.jar explicitly to your class path to use ClassFile class.
The class file will be generated in class path with same package structure if the destination is not specified.
What Else Can be Done?
1. Generate a java file on file server and compile it.
2. Generate a java file string and compile it.
3. Generate java files, compile and generate a jar file out of such class files.
4. You can write your own ide using this feature.
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.
