Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012 Martin Skurla
   *
   * Licensed 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.crazyjavahacking.astvisualizer.impl.javac.util;
 
 
 import java.util.List;
 
 
Java compiler utility class.

Author(s):
Martin
 
 public final class JavaCompilerUtils {
 
     //-----------------------------------------------------------------------------------------------------------------
     // Constructors.
     //-----------------------------------------------------------------------------------------------------------------
     private JavaCompilerUtils() {}
 
 
     //-----------------------------------------------------------------------------------------------------------------
     // Public Methods.
     //-----------------------------------------------------------------------------------------------------------------
     
Returns CompilationTask from given JavaFileObject.

Parameters:
javaFileObject JavaFileObject
Returns:
CompilationTask
 
         List<JavaFileObjectjavaSources = Arrays.asList(javaFileObject);
 
         JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
 
         JavaCompiler.CompilationTask compilationTask = javaCompiler.getTask(
                 new OutputStreamWriter(.),
                 null,  // default file manager
                 null,  // default diagnostic listener
                 null,  // no compiler options
                 null,  // no classes for annotation processing
                 javaSources);
 
         return compilationTask;
     }

    
Compiles given CompilationTask.

Parameters:
compilationTask CompilationTask
Returns:
CompilationUnitTree
Throws:
JavaCompilerUtils.JavacParsingException if problem during javac parsing
 
     public static CompilationUnitTree compile(JavaCompiler.CompilationTask compilationTask) {
         JavacTask javacTask = (JavacTaskcompilationTask;
 
         ASTRememberingTaskListener astRememberingTaskListener = new ASTRememberingTaskListener();
         javacTask.setTaskListener(astRememberingTaskListener);
 
         try {
             javacTask.analyze();
             return astRememberingTaskListener.compilationUnitTree;
         }
         catch (IOException e) {
             throw new JavacParsingException(e);
         }
     }
 
 
     //-----------------------------------------------------------------------------------------------------------------
     // Inner classes.
     //-----------------------------------------------------------------------------------------------------------------
     
Represents exceptional state during javac parsing.
    public static final class JavacParsingException extends RuntimeException {
        private static final long serialVersionUID = 1L;
        public JavacParsingException(Throwable cause) {
            super(cause);
        }
    }

    
Task listener for remembering AST - CompilationUnitTree.
    private static final class ASTRememberingTaskListener implements TaskListener {
        private CompilationUnitTree compilationUnitTree = null;
        @Override
        public void started(TaskEvent te) {
            if ( == null) {
                 = te.getCompilationUnit();
            }
        }
        @Override
        public void finished(TaskEvent te) {}
    }
New to GrepCode? Check out our FAQ X