langtools/src/share/classes/com/sun/tools/javac/api/JavacTool.java
changeset 9071 88cd61b4e5aa
parent 9069 bcab4a29758f
child 11052 65b9fa7eaf55
equal deleted inserted replaced
9070:f847fe5cad3d 9071:88cd61b4e5aa
    47 import com.sun.tools.javac.main.JavacOption.OptionKind;
    47 import com.sun.tools.javac.main.JavacOption.OptionKind;
    48 import com.sun.tools.javac.main.JavacOption;
    48 import com.sun.tools.javac.main.JavacOption;
    49 import com.sun.tools.javac.main.Main;
    49 import com.sun.tools.javac.main.Main;
    50 import com.sun.tools.javac.main.RecognizedOptions.GrumpyHelper;
    50 import com.sun.tools.javac.main.RecognizedOptions.GrumpyHelper;
    51 import com.sun.tools.javac.main.RecognizedOptions;
    51 import com.sun.tools.javac.main.RecognizedOptions;
       
    52 import com.sun.tools.javac.util.ClientCodeException;
    52 import com.sun.tools.javac.util.Context;
    53 import com.sun.tools.javac.util.Context;
    53 import com.sun.tools.javac.util.Log;
    54 import com.sun.tools.javac.util.Log;
    54 import com.sun.tools.javac.util.Options;
    55 import com.sun.tools.javac.util.Options;
    55 import com.sun.tools.javac.util.Pair;
    56 import com.sun.tools.javac.util.Pair;
    56 
    57 
   160                              DiagnosticListener<? super JavaFileObject> diagnosticListener,
   161                              DiagnosticListener<? super JavaFileObject> diagnosticListener,
   161                              Iterable<String> options,
   162                              Iterable<String> options,
   162                              Iterable<String> classes,
   163                              Iterable<String> classes,
   163                              Iterable<? extends JavaFileObject> compilationUnits)
   164                              Iterable<? extends JavaFileObject> compilationUnits)
   164     {
   165     {
   165         final String kindMsg = "All compilation units must be of SOURCE kind";
   166         try {
   166         if (options != null)
   167             Context context = new Context();
   167             for (String option : options)
   168             ClientCodeWrapper ccw = ClientCodeWrapper.instance(context);
   168                 option.getClass(); // null check
   169 
   169         if (classes != null) {
   170             final String kindMsg = "All compilation units must be of SOURCE kind";
   170             for (String cls : classes)
   171             if (options != null)
   171                 if (!SourceVersion.isName(cls)) // implicit null check
   172                 for (String option : options)
   172                     throw new IllegalArgumentException("Not a valid class name: " + cls);
   173                     option.getClass(); // null check
   173         }
   174             if (classes != null) {
   174         if (compilationUnits != null) {
   175                 for (String cls : classes)
   175             for (JavaFileObject cu : compilationUnits) {
   176                     if (!SourceVersion.isName(cls)) // implicit null check
   176                 if (cu.getKind() != JavaFileObject.Kind.SOURCE) // implicit null check
   177                         throw new IllegalArgumentException("Not a valid class name: " + cls);
   177                     throw new IllegalArgumentException(kindMsg);
   178             }
   178             }
   179             if (compilationUnits != null) {
   179         }
   180                 compilationUnits = ccw.wrapJavaFileObjects(compilationUnits); // implicit null check
   180 
   181                 for (JavaFileObject cu : compilationUnits) {
   181         Context context = new Context();
   182                     if (cu.getKind() != JavaFileObject.Kind.SOURCE)
   182 
   183                         throw new IllegalArgumentException(kindMsg);
   183         if (diagnosticListener != null)
   184                 }
   184             context.put(DiagnosticListener.class, diagnosticListener);
   185             }
   185 
   186 
   186         if (out == null)
   187             if (diagnosticListener != null)
   187             context.put(Log.outKey, new PrintWriter(System.err, true));
   188                 context.put(DiagnosticListener.class, ccw.wrap(diagnosticListener));
   188         else
   189 
   189             context.put(Log.outKey, new PrintWriter(out, true));
   190             if (out == null)
   190 
   191                 context.put(Log.outKey, new PrintWriter(System.err, true));
   191         if (fileManager == null)
   192             else
   192             fileManager = getStandardFileManager(diagnosticListener, null, null);
   193                 context.put(Log.outKey, new PrintWriter(out, true));
   193         context.put(JavaFileManager.class, fileManager);
   194 
   194         processOptions(context, fileManager, options);
   195             if (fileManager == null)
   195         Main compiler = new Main("javacTask", context.get(Log.outKey));
   196                 fileManager = getStandardFileManager(diagnosticListener, null, null);
   196         return new JavacTaskImpl(this, compiler, options, context, classes, compilationUnits);
   197             fileManager = ccw.wrap(fileManager);
       
   198             context.put(JavaFileManager.class, fileManager);
       
   199             processOptions(context, fileManager, options);
       
   200             Main compiler = new Main("javacTask", context.get(Log.outKey));
       
   201             return new JavacTaskImpl(compiler, options, context, classes, compilationUnits);
       
   202         } catch (ClientCodeException ex) {
       
   203             throw new RuntimeException(ex.getCause());
       
   204         }
   197     }
   205     }
   198 
   206 
   199     private static void processOptions(Context context,
   207     private static void processOptions(Context context,
   200                                        JavaFileManager fileManager,
   208                                        JavaFileManager fileManager,
   201                                        Iterable<String> options)
   209                                        Iterable<String> options)