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 CompilationTaks.

Parameters:
compilationTask CompilationTask
Returns:
CompilationUnitTree
Throws:
JavaCompilerUtils.JavacParsingException if problem during javac parsing
 
     public static CompilationUnitTree compile(JavaCompiler.CompilationTask compilationTask) {
         JavacTask javacTask = (JavacTaskcompilationTask;
 
         try {
             return javacTask.parse().iterator().next();
         }
         catch (IOException e) {
             throw new JavacParsingException(e);
         }
     }

    
Compiles given JavaFileObject.

Parameters:
javaFileObject JavaFileObject
Returns:
CompilationUnitTree
Throws:
JavaCompilerUtils.JavacParsingException if problem during javac parsing
 
     public static CompilationUnitTree compile(JavaFileObject javaFileObject) {
         CompilationTask compilationTask = getCompilationTaskFromJavaFileObject(javaFileObject);
        return compile(compilationTask);
    }
   //-----------------------------------------------------------------------------------------------------------------
    // 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);
        }
    }
New to GrepCode? Check out our FAQ X