Search in sources :

Example 1 with JavaProjectBuilder

use of com.thoughtworks.qdox.JavaProjectBuilder in project maven-plugins by apache.

the class QDoxModuleInfoParser method getModuleDescriptor.

@Override
public org.apache.maven.plugin.compiler.module.JavaModuleDescriptor getModuleDescriptor(File modulePath) throws IOException {
    File moduleDescriptor = new File(modulePath, "module-info.java");
    org.apache.maven.plugin.compiler.module.JavaModuleDescriptor.Builder builder;
    if (moduleDescriptor.exists()) {
        JavaModuleDescriptor descriptor = new JavaProjectBuilder().addSourceFolder(modulePath).getDescriptor();
        builder = org.apache.maven.plugin.compiler.module.JavaModuleDescriptor.newModule(descriptor.getName());
        for (JavaModuleDescriptor.JavaRequires requires : descriptor.getRequires()) {
            builder.requires(requires.getModule().getName());
        }
        for (JavaModuleDescriptor.JavaExports exports : descriptor.getExports()) {
            builder.exports(exports.getSource().getName());
        }
    } else {
        builder = org.apache.maven.plugin.compiler.module.JavaModuleDescriptor.newAutomaticModule(null);
    }
    return builder.build();
}
Also used : JavaModuleDescriptor(com.thoughtworks.qdox.model.JavaModuleDescriptor) JavaProjectBuilder(com.thoughtworks.qdox.JavaProjectBuilder) File(java.io.File)

Example 2 with JavaProjectBuilder

use of com.thoughtworks.qdox.JavaProjectBuilder in project zeppelin by apache.

the class JavaSourceFromString method execute.

public static String execute(String generatedClassName, String code) throws Exception {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
    // Java parasing
    JavaProjectBuilder builder = new JavaProjectBuilder();
    JavaSource src = builder.addSource(new StringReader(code));
    // get all classes in code (paragraph)
    List<JavaClass> classes = src.getClasses();
    String mainClassName = null;
    // Searching for class containing Main method
    for (int i = 0; i < classes.size(); i++) {
        boolean hasMain = false;
        for (int j = 0; j < classes.get(i).getMethods().size(); j++) {
            if (classes.get(i).getMethods().get(j).getName().equals("main") && classes.get(i).getMethods().get(j).isStatic()) {
                mainClassName = classes.get(i).getName();
                hasMain = true;
                break;
            }
        }
        if (hasMain == true) {
            break;
        }
    }
    // if there isn't Main method, will retuen error
    if (mainClassName == null) {
        logger.error("Exception for Main method", "There isn't any class " + "containing static main method.");
        throw new Exception("There isn't any class containing static main method.");
    }
    // replace name of class containing Main method with generated name
    code = code.replace(mainClassName, generatedClassName);
    JavaFileObject file = new JavaSourceFromString(generatedClassName, code.toString());
    Iterable<? extends JavaFileObject> compilationUnits = Arrays.asList(file);
    ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
    ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
    // Creating new stream to get the output data
    PrintStream newOut = new PrintStream(baosOut);
    PrintStream newErr = new PrintStream(baosErr);
    // Save the old System.out!
    PrintStream oldOut = System.out;
    PrintStream oldErr = System.err;
    // Tell Java to use your special stream
    System.setOut(newOut);
    System.setErr(newErr);
    CompilationTask task = compiler.getTask(null, null, diagnostics, null, null, compilationUnits);
    // executing the compilation process
    boolean success = task.call();
    // if success is false will get error
    if (!success) {
        for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
            if (diagnostic.getLineNumber() == -1) {
                continue;
            }
            System.err.println("line " + diagnostic.getLineNumber() + " : " + diagnostic.getMessage(null));
        }
        System.out.flush();
        System.err.flush();
        System.setOut(oldOut);
        System.setErr(oldErr);
        logger.error("Exception in Interpreter while compilation", baosErr.toString());
        throw new Exception(baosErr.toString());
    } else {
        try {
            // creating new class loader
            URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { new File("").toURI().toURL() });
            // execute the Main method
            Class.forName(generatedClassName, true, classLoader).getDeclaredMethod("main", new Class[] { String[].class }).invoke(null, new Object[] { null });
            System.out.flush();
            System.err.flush();
            // set the stream to old stream
            System.setOut(oldOut);
            System.setErr(oldErr);
            return baosOut.toString();
        } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            logger.error("Exception in Interpreter while execution", e);
            System.err.println(e);
            e.printStackTrace(newErr);
            throw new Exception(baosErr.toString(), e);
        } finally {
            System.out.flush();
            System.err.flush();
            System.setOut(oldOut);
            System.setErr(oldErr);
        }
    }
}
Also used : Diagnostic(javax.tools.Diagnostic) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) JavaSource(com.thoughtworks.qdox.model.JavaSource) StringReader(java.io.StringReader) DiagnosticCollector(javax.tools.DiagnosticCollector) JavaProjectBuilder(com.thoughtworks.qdox.JavaProjectBuilder) PrintStream(java.io.PrintStream) JavaCompiler(javax.tools.JavaCompiler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InvocationTargetException(java.lang.reflect.InvocationTargetException) CompilationTask(javax.tools.JavaCompiler.CompilationTask) InvocationTargetException(java.lang.reflect.InvocationTargetException) JavaClass(com.thoughtworks.qdox.model.JavaClass) URLClassLoader(java.net.URLClassLoader) JavaClass(com.thoughtworks.qdox.model.JavaClass) File(java.io.File)

Aggregations

JavaProjectBuilder (com.thoughtworks.qdox.JavaProjectBuilder)2 File (java.io.File)2 JavaClass (com.thoughtworks.qdox.model.JavaClass)1 JavaModuleDescriptor (com.thoughtworks.qdox.model.JavaModuleDescriptor)1 JavaSource (com.thoughtworks.qdox.model.JavaSource)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PrintStream (java.io.PrintStream)1 StringReader (java.io.StringReader)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 URLClassLoader (java.net.URLClassLoader)1 Diagnostic (javax.tools.Diagnostic)1 DiagnosticCollector (javax.tools.DiagnosticCollector)1 JavaCompiler (javax.tools.JavaCompiler)1 CompilationTask (javax.tools.JavaCompiler.CompilationTask)1 JavaFileObject (javax.tools.JavaFileObject)1 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)1