Pessoal,
Alguem ja trabalhou com o JavaCompiler, pois estou tento o seguinte problema, eu gero uma classe dinamica e funciona blz, so que se eu peço para gerar a classe novamente no msm processamento, eu vejo que gerou uma nova classe, so que na hora da execução está sendo utilizado o fonte da classe anterior. Segue um exemplo.
package teste;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.List;
import javax.tools.JavaCompiler;
import javax.tools.SimpleJavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.StandardLocation;
import javax.tools.ToolProvider;
public class Compiler {
private Compiler(){}
public static Compiler getInstance(){
return new Compiler();
}
public void compilar1() {
try {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File("./bin")));
String classeGerada = gerarClasse1();
List<JavaFile> lista = Arrays.asList(new JavaFile("Soma", classeGerada));
compiler.getTask(null, fileManager, null, null, null, lista).call();
} catch (Exception e) {
e.printStackTrace();
}
}
private String gerarClasse1() {
StringBuilder source = new StringBuilder();
source.append("package teste; ");
source.append("public class Soma implements Operador{ ");
source.append(" @Override ");
source.append(" public int soma(int a, int b) { ");
source.append(" return a+b;");
source.append(" }");
source.append("}");
return source.toString();
}
public void compilar2() {
try {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File("./bin")));
String classeGerada = gerarClasse2();
List<JavaFile> lista = Arrays.asList(new JavaFile("Soma", classeGerada));
compiler.getTask(null, fileManager, null, null, null, lista).call();
} catch (Exception e) {
e.printStackTrace();
}
}
private String gerarClasse2() {
StringBuilder source = new StringBuilder();
source.append("package teste; ");
source.append("public class Soma implements Operador{ ");
source.append(" @Override ");
source.append(" public int soma(int a, int b) { ");
source.append(" return a-b;");
source.append(" }");
source.append("}");
return source.toString();
}
class JavaFile extends SimpleJavaFileObject {
private String source;
public JavaFile(String classe, String source) throws URISyntaxException {
super(new URI(classe + ".java"), Kind.SOURCE);
this.source = source;
}
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
return source;
}
}
public static void main (String[] args){
try {
Compiler.getInstance().compilar1();
Operador o1 = (Operador) Class.forName("teste.Soma").newInstance();
System.out.println(o1.soma(1, 1));
Compiler.getInstance().compilar2();
Operador o2 = (Operador) Class.forName("teste.Soma").newInstance();
System.out.println(o2.soma(1, 1));
} catch (Exception e) {
e.printStackTrace();
}
}
}
package teste;
public interface Operador {
public int soma(int a, int b);
}
Obrigado!!!