Search in sources :

Example 1 with CompilationUnitWithSource

use of org.apache.sling.commons.compiler.CompilationUnitWithSource in project sling by apache.

the class JDTCompiler method generateClass.

/**
     * Compile the servlet from .java file to .class file
     */
@Override
protected void generateClass(String[] smap) throws FileNotFoundException, JasperException, Exception {
    long t1 = 0;
    if (log.isDebugEnabled()) {
        t1 = System.currentTimeMillis();
    }
    final String sourceFile = ctxt.getServletJavaFileName();
    final String packageName = ctxt.getServletPackageName();
    final String targetClassName = ((packageName.length() != 0) ? (packageName + ".") : "") + ctxt.getServletClassName();
    final CompilationUnit unit = new CompilationUnitWithSource() {

        /**
             * @see org.apache.sling.commons.compiler.CompilationUnit#getLastModified()
             */
        public long getLastModified() {
            return -1;
        }

        /**
             * @see org.apache.sling.commons.compiler.CompilationUnit#getMainClassName()
             */
        public String getMainClassName() {
            return targetClassName;
        }

        /**
             * @see org.apache.sling.commons.compiler.CompilationUnit#getSource()
             */
        public Reader getSource() throws IOException {
            return new BufferedReader(new InputStreamReader(ctxt.getInputStream(sourceFile), ctxt.getOptions().getJavaEncoding()));
        }

        /**
             * @see org.apache.sling.commons.compiler.CompilationUnitWithSource#getFileName()
             */
        public String getFileName() {
            return sourceFile;
        }
    };
    final Options options = new Options();
    options.put(Options.KEY_CLASS_LOADER_WRITER, ctxt.getRuntimeContext().getIOProvider().getClassLoaderWriter());
    options.put(Options.KEY_GENERATE_DEBUG_INFO, ctxt.getOptions().getClassDebugInfo());
    // Source JVM
    if (ctxt.getOptions().getCompilerSourceVM() != null) {
        options.put(Options.KEY_SOURCE_VERSION, ctxt.getOptions().getCompilerSourceVM());
    } else {
        // Default to 1.6
        options.put(Options.KEY_SOURCE_VERSION, Options.VERSION_1_6);
    }
    // Target JVM
    if (ctxt.getOptions().getCompilerTargetVM() != null) {
        options.put(Options.KEY_TARGET_VERSION, ctxt.getOptions().getCompilerTargetVM());
    } else {
        // Default to 1.6
        options.put(Options.KEY_TARGET_VERSION, Options.VERSION_1_6);
    }
    final ArrayList<JavacErrorDetail> problemList = new ArrayList<JavacErrorDetail>();
    final CompilationResult result = this.ctxt.getRuntimeContext().getIOProvider().getJavaCompiler().compile(new CompilationUnit[] { unit }, options);
    if (result.getErrors() != null) {
        for (final CompilerMessage cm : result.getErrors()) {
            final String name = cm.getFile();
            try {
                problemList.add(ErrorDispatcher.createJavacError(name, pageNodes, new StringBuffer(cm.getMessage()), cm.getLine(), ctxt));
            } catch (JasperException e) {
                log.error("Error visiting node", e);
            }
        }
    }
    if (!ctxt.keepGenerated()) {
        ctxt.delete(ctxt.getServletJavaFileName());
    }
    if (!problemList.isEmpty()) {
        JavacErrorDetail[] jeds = problemList.toArray(new JavacErrorDetail[0]);
        errDispatcher.javacError(jeds);
    }
    if (log.isDebugEnabled()) {
        long t2 = System.currentTimeMillis();
        log.debug("Compiled " + ctxt.getServletJavaFileName() + " " + (t2 - t1) + "ms");
    }
    if (ctxt.isPrototypeMode()) {
        return;
    }
    // JSR45 Support
    if (!this.options.isSmapSuppressed()) {
        SmapUtil.installSmap(getCompilationContext(), smap);
    }
}
Also used : CompilationUnit(org.apache.sling.commons.compiler.CompilationUnit) Options(org.apache.sling.commons.compiler.Options) CompilationUnitWithSource(org.apache.sling.commons.compiler.CompilationUnitWithSource) CompilerMessage(org.apache.sling.commons.compiler.CompilerMessage) InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) JasperException(org.apache.sling.scripting.jsp.jasper.JasperException) BufferedReader(java.io.BufferedReader) CompilationResult(org.apache.sling.commons.compiler.CompilationResult)

Aggregations

BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1 CompilationResult (org.apache.sling.commons.compiler.CompilationResult)1 CompilationUnit (org.apache.sling.commons.compiler.CompilationUnit)1 CompilationUnitWithSource (org.apache.sling.commons.compiler.CompilationUnitWithSource)1 CompilerMessage (org.apache.sling.commons.compiler.CompilerMessage)1 Options (org.apache.sling.commons.compiler.Options)1 JasperException (org.apache.sling.scripting.jsp.jasper.JasperException)1