langtools/src/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java
changeset 8844 18170a311747
parent 6716 71df48777dd1
child 9069 bcab4a29758f
equal deleted inserted replaced
8843:2602bd5c9c5f 8844:18170a311747
     1 /*
     1 /*
     2  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
   127         return result.toList();
   127         return result.toList();
   128     }
   128     }
   129 
   129 
   130     public Boolean call() {
   130     public Boolean call() {
   131         if (!used.getAndSet(true)) {
   131         if (!used.getAndSet(true)) {
   132             beginContext();
   132             initContext();
   133             notYetEntered = new HashMap<JavaFileObject, JCCompilationUnit>();
   133             notYetEntered = new HashMap<JavaFileObject, JCCompilationUnit>();
   134             try {
   134             compilerMain.setFatalErrors(true);
   135                 compilerMain.setFatalErrors(true);
   135             result = compilerMain.compile(args, context, fileObjects, processors);
   136                 result = compilerMain.compile(args, context, fileObjects, processors);
   136             cleanup();
   137             } finally {
       
   138                 endContext();
       
   139             }
       
   140             compilerMain = null;
       
   141             args = null;
       
   142             context = null;
       
   143             fileObjects = null;
       
   144             notYetEntered = null;
       
   145             return result == 0;
   137             return result == 0;
   146         } else {
   138         } else {
   147             throw new IllegalStateException("multiple calls to method 'call'");
   139             throw new IllegalStateException("multiple calls to method 'call'");
   148         }
   140         }
   149     }
   141     }
   161             throw new IllegalStateException();
   153             throw new IllegalStateException();
   162         this.locale = locale;
   154         this.locale = locale;
   163     }
   155     }
   164 
   156 
   165     private void prepareCompiler() throws IOException {
   157     private void prepareCompiler() throws IOException {
   166         if (!used.getAndSet(true)) {
   158         if (used.getAndSet(true)) {
   167             beginContext();
   159             if (compiler == null)
       
   160                 throw new IllegalStateException();
       
   161         } else {
       
   162             initContext();
   168             compilerMain.setOptions(Options.instance(context));
   163             compilerMain.setOptions(Options.instance(context));
   169             compilerMain.filenames = new ListBuffer<File>();
   164             compilerMain.filenames = new ListBuffer<File>();
   170             List<File> filenames = compilerMain.processArgs(CommandLine.parse(args));
   165             List<File> filenames = compilerMain.processArgs(CommandLine.parse(args));
   171             if (!filenames.isEmpty())
   166             if (!filenames.isEmpty())
   172                 throw new IllegalArgumentException("Malformed arguments " + filenames.toString(" "));
   167                 throw new IllegalArgumentException("Malformed arguments " + filenames.toString(" "));
   183             // TODO: should handle the case after each phase if errors have occurred
   178             // TODO: should handle the case after each phase if errors have occurred
   184             args = null;
   179             args = null;
   185         }
   180         }
   186     }
   181     }
   187 
   182 
   188     private void beginContext() {
   183     private void initContext() {
   189         context.put(JavacTaskImpl.class, this);
   184         context.put(JavacTaskImpl.class, this);
   190         if (context.get(TaskListener.class) != null)
   185         if (context.get(TaskListener.class) != null)
   191             context.put(TaskListener.class, (TaskListener)null);
   186             context.put(TaskListener.class, (TaskListener)null);
   192         if (taskListener != null)
   187         if (taskListener != null)
   193             context.put(TaskListener.class, wrap(taskListener));
   188             context.put(TaskListener.class, wrap(taskListener));
   194         tool.beginContext(context);
       
   195         //initialize compiler's default locale
   189         //initialize compiler's default locale
   196         JavacMessages.instance(context).setCurrentLocale(locale);
   190         JavacMessages.instance(context).setCurrentLocale(locale);
   197     }
   191     }
   198     // where
   192     // where
   199     private TaskListener wrap(final TaskListener tl) {
   193     private TaskListener wrap(final TaskListener tl) {
   216             }
   210             }
   217 
   211 
   218         };
   212         };
   219     }
   213     }
   220 
   214 
   221     private void endContext() {
   215     void cleanup() {
   222         tool.endContext();
   216         if (compiler != null)
       
   217             compiler.close();
       
   218         compiler = null;
       
   219         compilerMain = null;
       
   220         args = null;
       
   221         context = null;
       
   222         fileObjects = null;
       
   223         notYetEntered = null;
   223     }
   224     }
   224 
   225 
   225     /**
   226     /**
   226      * Construct a JavaFileObject from the given file.
   227      * Construct a JavaFileObject from the given file.
   227      *
   228      *
   444                     };
   445                     };
   445                 f.run(genList, classes);
   446                 f.run(genList, classes);
   446             }
   447             }
   447             if (genList.isEmpty()) {
   448             if (genList.isEmpty()) {
   448                 compiler.reportDeferredDiagnostics();
   449                 compiler.reportDeferredDiagnostics();
       
   450                 cleanup();
       
   451             }
       
   452         }
       
   453         finally {
       
   454             if (compiler != null)
   449                 compiler.log.flush();
   455                 compiler.log.flush();
   450                 endContext();
       
   451             }
       
   452         }
       
   453         finally {
       
   454             compiler.log.flush();
       
   455         }
   456         }
   456         return results;
   457         return results;
   457     }
   458     }
   458 
   459 
   459     public TypeMirror getTypeMirror(Iterable<? extends Tree> path) {
   460     public TypeMirror getTypeMirror(Iterable<? extends Tree> path) {