Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.apache.jasper.compiler;
 
 import java.io.File;
 import java.io.Reader;
 import java.util.Map;
 
JDT class compiler. This compiler will load source dependencies from the context classloader, reducing dramatically disk access during the compilation process.

Author(s):
Cocoon2
Remy Maucherat
 
 public class JDTCompiler extends org.apache.jasper.compiler.Compiler {

    
    
Compile the servlet from .java file to .class file
 
     protected void generateClass(String[] smap)
         throws FileNotFoundExceptionJasperExceptionException {
 
         long t1 = 0;
         if (..isDebugEnabled()) {
             t1 = System.currentTimeMillis();
         }
         
         final String sourceFile = .getServletJavaFileName();
         final String outputDir = .getOptions().getScratchDir().getAbsolutePath();
         String packageName = .getServletPackageName();
         final String targetClassName = 
             ((packageName.length() != 0) ? (packageName + ".") : ""
                     + .getServletClassName();
         final ClassLoader classLoader = .getJspLoader();
         String[] fileNames = new String[] {sourceFile};
         String[] classNames = new String[] {targetClassName};
         final ArrayList problemList = new ArrayList();
         
         class CompilationUnit implements ICompilationUnit {
 
             String className;
             String sourceFile;
 
             CompilationUnit(String sourceFileString className) {
                 this. = className;
                 this. = sourceFile;
             }
 
             public char[] getFileName() {
                 return .toCharArray();
             }
            
            public char[] getContents() {
                char[] result = null;
                FileInputStream is = null;
                Reader reader = null;
                try {
                    is = new FileInputStream();
                    reader = new BufferedReader(new InputStreamReader(is.getOptions().getJavaEncoding()));
                    if (reader != null) {
                        char[] chars = new char[8192];
                        StringBuilder buf = new StringBuilder();
                        int count;
                        while ((count = reader.read(chars, 0, 
                                                    chars.length)) > 0) {
                            buf.append(chars, 0, count);
                        }
                        result = new char[buf.length()];
                        buf.getChars(0, result.lengthresult, 0);
                    }
                } catch (IOException e) {
                    ..errorReadingSourceFile(e);
                } finally {
                    if (reader != null) {
                        try {
                            reader.close();
                        } catch (IOException exc) {
                            // Ignore
                        }
                    }
                    if (is != null) {
                        try {
                            is.close();
                        } catch (IOException exc) {
                            // Ignore
                        }
                    }
                }
                return result;
            }
            
            public char[] getMainTypeName() {
                int dot = .lastIndexOf('.');
                if (dot > 0) {
                    return .substring(dot + 1).toCharArray();
                }
                return .toCharArray();
            }
            
            public char[][] getPackageName() {
                StringTokenizer izer = 
                    new StringTokenizer(".");
                char[][] result = new char[izer.countTokens()-1][];
                for (int i = 0; i < result.lengthi++) {
                    String tok = izer.nextToken();
                    result[i] = tok.toCharArray();
                }
                return result;
            }
            public boolean ignoreOptionalProblems() {
                return false;
            }
        }
        final INameEnvironment env = new INameEnvironment() {
                public NameEnvironmentAnswer 
                    findType(char[][] compoundTypeName) {
                    String result = "";
                    String sep = "";
                    for (int i = 0; i < compoundTypeName.lengthi++) {
                        result += sep;
                        result += new String(compoundTypeName[i]);
                        sep = ".";
                    }
                    return findType(result);
                }
                public NameEnvironmentAnswer 
                    findType(char[] typeName
                             char[][] packageName) {
                        String result = "";
                        String sep = "";
                        for (int i = 0; i < packageName.lengthi++) {
                            result += sep;
                            result += new String(packageName[i]);
                            sep = ".";
                        }
                        result += sep;
                        result += new String(typeName);
                        return findType(result);
                }
                
                private NameEnvironmentAnswer findType(String className) {
                    InputStream is = null;
                    try {
                        if (className.equals(targetClassName)) {
                            ICompilationUnit compilationUnit = 
                                new CompilationUnit(sourceFileclassName);
                            return 
                                new NameEnvironmentAnswer(compilationUnitnull);
                        }
                        String resourceName = 
                            className.replace('.''/') + ".class";
                        is = classLoader.getResourceAsStream(resourceName);
                        if (is != null) {
                            byte[] classBytes;
                            byte[] buf = new byte[8192];
                            ByteArrayOutputStream baos = 
                                new ByteArrayOutputStream(buf.length);
                            int count;
                            while ((count = is.read(buf, 0, buf.length)) > 0) {
                                baos.write(buf, 0, count);
                            }
                            baos.flush();
                            classBytes = baos.toByteArray();
                            char[] fileName = className.toCharArray();
                            ClassFileReader classFileReader = 
                                new ClassFileReader(classBytesfileName
                                                    true);
                            return 
                                new NameEnvironmentAnswer(classFileReadernull);
                        }
                    } catch (IOException exc) {
                        ..errorReadingClassFile(classNameexc);
                    } catch (org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException exc) {
                        ..errorReadingClassFile(classNameexc);
                    } finally {
                        if (is != null) {
                            try {
                                is.close();
                            } catch (IOException exc) {
                                // Ignore
                            }
                        }
                    }
                    return null;
                }
                private boolean isPackage(String result) {
                    if (result.equals(targetClassName)) {
                        return false;
                    }
                    String resourceName = result.replace('.''/') + ".class";
                    InputStream is = null;
                    try {
                        is = classLoader.getResourceAsStream(resourceName);
                    } finally {
                        if (is != null) {
                            try {
                                is.close();
                            } catch (IOException e) {
                                // Ignore
                            }
                        }
                    }
                    return is == null;
                }
                public boolean isPackage(char[][] parentPackageName
                                         char[] packageName) {
                    String result = "";
                    String sep = "";
                    if (parentPackageName != null) {
                        for (int i = 0; i < parentPackageName.lengthi++) {
                            result += sep;
                            String str = new String(parentPackageName[i]);
                            result += str;
                            sep = ".";
                        }
                    }
                    String str = new String(packageName);
                    if (Character.isUpperCase(str.charAt(0))) {
                        if (!isPackage(result)) {
                            return false;
                        }
                    }
                    result += sep;
                    result += str;
                    return isPackage(result);
                }
                public void cleanup() {
                }
            };
        final IErrorHandlingPolicy policy = 
            DefaultErrorHandlingPolicies.proceedWithAllProblems();
        final Map settings = new HashMap();
                     .);
                     .);
                     .);
        if (.getOptions().getJavaEncoding() != null) {
            settings.put(.,
                    .getOptions().getJavaEncoding());
        }
        if (.getOptions().getClassDebugInfo()) {
            settings.put(.,
                         .);
        }
        // Source JVM
        if(.getOptions().getCompilerSourceVM() != null) {
            String opt = .getOptions().getCompilerSourceVM();
            if(opt.equals("1.1")) {
                settings.put(.,
                             .);
            } else if(opt.equals("1.2")) {
                settings.put(.,
                             .);
            } else if(opt.equals("1.3")) { 
                settings.put(.,
                             .);
            } else if(opt.equals("1.4")) {
                settings.put(.,
                             .);
            } else if(opt.equals("1.5")) {
                settings.put(.,
                             .);
            } else if(opt.equals("1.6")) {
                settings.put(.,
                             .);
            } else if(opt.equals("1.7")) {
                settings.put(.,
                             .);
            } else {
                ..unknownSourceJvm(opt);
                settings.put(.,
                        .);
            }
        } else {
            // Default to 1.5
            settings.put(.,
                    .);
        }
        
        // Target JVM
        if(.getOptions().getCompilerTargetVM() != null) {
            String opt = .getOptions().getCompilerTargetVM();
            if(opt.equals("1.1")) {
                settings.put(.,
                             .);
            } else if(opt.equals("1.2")) {
                settings.put(.,
                             .);
            } else if(opt.equals("1.3")) { 
                settings.put(.,
                             .);
            } else if(opt.equals("1.4")) {
                settings.put(.,
                             .);
            } else if(opt.equals("1.5")) {
                settings.put(.,
                             .);
                settings.put(.,
                        .);
            } else if(opt.equals("1.6")) {
                settings.put(.,
                             .);
                settings.put(.,
                        .);
            } else if(opt.equals("1.7")) {
                settings.put(.,
                             .);
                settings.put(.,
                        .);
            } else {
                ..unknownTargetJvm(opt);
                settings.put(.,
                        .);
            }
        } else {
            // Default to 1.5
            settings.put(.,
                    .);
            settings.put(.,
                    .);
        }
        final IProblemFactory problemFactory = 
            new DefaultProblemFactory(Locale.getDefault());
        
        final ICompilerRequestor requestor = new ICompilerRequestor() {
                public void acceptResult(CompilationResult result) {
                    try {
                        if (result.hasProblems()) {
                            IProblem[] problems = result.getProblems();
                            for (int i = 0; i < problems.lengthi++) {
                                IProblem problem = problems[i];
                                if (problem.isError()) {
                                    String name = 
                                        new String(problems[i].getOriginatingFileName());
                                    try {
                                        problemList.add(ErrorDispatcher.createJavacError
                                                (namenew StringBuilder(problem.getMessage()), 
                                                        problem.getSourceLineNumber(), ));
                                    } catch (JasperException e) {
                                        ..errorCreatingCompilerReport(e);
                                    }
                                }
                            }
                        }
                        if (problemList.isEmpty()) {
                            ClassFile[] classFiles = result.getClassFiles();
                            for (int i = 0; i < classFiles.lengthi++) {
                                ClassFile classFile = classFiles[i];
                                char[][] compoundName = 
                                    classFile.getCompoundName();
                                String className = "";
                                String sep = "";
                                for (int j = 0; 
                                     j < compoundName.lengthj++) {
                                    className += sep;
                                    className += new String(compoundName[j]);
                                    sep = ".";
                                }
                                byte[] bytes = classFile.getBytes();
                                String outFile = outputDir + "/" + 
                                    className.replace('.''/') + ".class";
                                FileOutputStream fout = 
                                    new FileOutputStream(outFile);
                                BufferedOutputStream bos = 
                                    new BufferedOutputStream(fout);
                                bos.write(bytes);
                                bos.close();
                            }
                        }
                    } catch (IOException exc) {
                        ..errorCompiling(exc);
                    }
                }
            };
        ICompilationUnit[] compilationUnits = 
            new ICompilationUnit[classNames.length];
        for (int i = 0; i < compilationUnits.lengthi++) {
            String className = classNames[i];
            compilationUnits[i] = new CompilationUnit(fileNames[i], className);
        }
        Compiler compiler = new Compiler(env,
                                         policy,
                                         settings,
                                         requestor,
                                         problemFactory,
                                         true);
        compiler.compile(compilationUnits);
        if (!.keepGenerated()) {
            File javaFile = new File(.getServletJavaFileName());
            javaFile.delete();
        }
    
        if (!problemList.isEmpty()) {
            JavacErrorDetail[] jeds = 
                (JavacErrorDetail[]) problemList.toArray(new JavacErrorDetail[0]);
            .javacError(jeds);
        }
        
        if..isDebugEnabled() ) {
            long t2=System.currentTimeMillis();
            ..debug("Compiled " + .getServletJavaFileName() + " "
                      + (t2-t1) + "ms");
        }
        if (.isPrototypeMode()) {
            return;
        }
        // JSR45 Support
        if (! .isSmapSuppressed()) {
            SmapUtil.installSmap(smap);
        }
        
    }
    
    
New to GrepCode? Check out our FAQ X