14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.tools.javac.processing;
27
28 import java.io.Closeable;
29 import java.io.File;
30 import java.io.IOException;
31 import java.io.PrintWriter;
32 import java.io.StringWriter;
33 import java.lang.reflect.Method;
34 import java.net.MalformedURLException;
35 import java.net.URL;
36 import java.nio.file.Path;
37 import java.util.*;
38 import java.util.regex.*;
39 import java.util.stream.Collectors;
40
41 import javax.annotation.processing.*;
42 import javax.lang.model.SourceVersion;
43 import javax.lang.model.element.*;
44 import javax.lang.model.util.*;
45 import javax.tools.JavaFileManager;
46 import javax.tools.JavaFileObject;
47 import javax.tools.JavaFileObject.Kind;
48 import javax.tools.StandardJavaFileManager;
49
50 import static javax.tools.StandardLocation.*;
51
52 import com.sun.source.util.TaskEvent;
53 import com.sun.tools.javac.api.MultiTaskListener;
265 processorClassLoader = fileManager.hasLocation(ANNOTATION_PROCESSOR_PATH)
266 ? fileManager.getClassLoader(ANNOTATION_PROCESSOR_PATH)
267 : fileManager.getClassLoader(CLASS_PATH);
268
269 moduleHelper.addExports(processorClassLoader);
270
271 if (processorClassLoader != null && processorClassLoader instanceof Closeable) {
272 compiler.closeables = compiler.closeables.prepend((Closeable) processorClassLoader);
273 }
274 }
275 } catch (SecurityException e) {
276 processorLoaderException = e;
277 }
278 }
279
280 private void initProcessorIterator(Iterable<? extends Processor> processors) {
281 Iterator<? extends Processor> processorIterator;
282
283 if (options.isSet(XPRINT)) {
284 try {
285 @SuppressWarnings("deprecation")
286 Processor processor = PrintingProcessor.class.newInstance();
287 processorIterator = List.of(processor).iterator();
288 } catch (Throwable t) {
289 AssertionError assertError =
290 new AssertionError("Problem instantiating PrintingProcessor.");
291 assertError.initCause(t);
292 throw assertError;
293 }
294 } else if (processors != null) {
295 processorIterator = processors.iterator();
296 } else {
297 if (processorLoaderException == null) {
298 /*
299 * If the "-processor" option is used, search the appropriate
300 * path for the named class. Otherwise, use a service
301 * provider mechanism to create the processor iterator.
302 */
303 String processorNames = options.get(PROCESSOR);
304 if (fileManager.hasLocation(ANNOTATION_PROCESSOR_MODULE_PATH)) {
305 processorIterator = (processorNames == null) ?
306 new ServiceIterator(serviceLoader, log) :
307 new NameServiceIterator(serviceLoader, log, processorNames);
523 }
524 }
525 }
526
527 private static class NameProcessIterator implements Iterator<Processor> {
528 Processor nextProc = null;
529 Iterator<String> names;
530 ClassLoader processorCL;
531 Log log;
532
533 NameProcessIterator(String names, ClassLoader processorCL, Log log) {
534 this.names = Arrays.asList(names.split(",")).iterator();
535 this.processorCL = processorCL;
536 this.log = log;
537 }
538
539 public boolean hasNext() {
540 if (nextProc != null)
541 return true;
542 else {
543 if (!names.hasNext())
544 return false;
545 else {
546 String processorName = names.next();
547
548 Processor processor;
549 try {
550 try {
551 Class<?> processorClass = processorCL.loadClass(processorName);
552 ensureReadable(processorClass);
553 @SuppressWarnings("deprecation")
554 Object tmp = processorClass.newInstance();
555 processor = (Processor) tmp;
556 } catch (ClassNotFoundException cnfe) {
557 log.error("proc.processor.not.found", processorName);
558 return false;
559 } catch (ClassCastException cce) {
560 log.error("proc.processor.wrong.type", processorName);
561 return false;
562 } catch (Exception e ) {
563 log.error("proc.processor.cant.instantiate", processorName);
564 return false;
565 }
566 } catch(ClientCodeException e) {
567 throw e;
568 } catch(Throwable t) {
569 throw new AnnotationProcessingError(t);
570 }
571 nextProc = processor;
572 return true;
573 }
574
575 }
576 }
577
578 public Processor next() {
579 if (hasNext()) {
580 Processor p = nextProc;
581 nextProc = null;
582 return p;
583 } else
584 throw new NoSuchElementException();
585 }
586
587 public void remove () {
588 throw new UnsupportedOperationException();
589 }
590
591 /**
592 * Ensures that the module of the given class is readable to this
593 * module.
594 */
|
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.tools.javac.processing;
27
28 import java.io.Closeable;
29 import java.io.File;
30 import java.io.IOException;
31 import java.io.PrintWriter;
32 import java.io.StringWriter;
33 import java.lang.reflect.Method;
34 import java.lang.reflect.Constructor;
35 import java.net.MalformedURLException;
36 import java.net.URL;
37 import java.nio.file.Path;
38 import java.util.*;
39 import java.util.regex.*;
40 import java.util.stream.Collectors;
41
42 import javax.annotation.processing.*;
43 import javax.lang.model.SourceVersion;
44 import javax.lang.model.element.*;
45 import javax.lang.model.util.*;
46 import javax.tools.JavaFileManager;
47 import javax.tools.JavaFileObject;
48 import javax.tools.JavaFileObject.Kind;
49 import javax.tools.StandardJavaFileManager;
50
51 import static javax.tools.StandardLocation.*;
52
53 import com.sun.source.util.TaskEvent;
54 import com.sun.tools.javac.api.MultiTaskListener;
266 processorClassLoader = fileManager.hasLocation(ANNOTATION_PROCESSOR_PATH)
267 ? fileManager.getClassLoader(ANNOTATION_PROCESSOR_PATH)
268 : fileManager.getClassLoader(CLASS_PATH);
269
270 moduleHelper.addExports(processorClassLoader);
271
272 if (processorClassLoader != null && processorClassLoader instanceof Closeable) {
273 compiler.closeables = compiler.closeables.prepend((Closeable) processorClassLoader);
274 }
275 }
276 } catch (SecurityException e) {
277 processorLoaderException = e;
278 }
279 }
280
281 private void initProcessorIterator(Iterable<? extends Processor> processors) {
282 Iterator<? extends Processor> processorIterator;
283
284 if (options.isSet(XPRINT)) {
285 try {
286 processorIterator = List.of(new PrintingProcessor()).iterator();
287 } catch (Throwable t) {
288 AssertionError assertError =
289 new AssertionError("Problem instantiating PrintingProcessor.");
290 assertError.initCause(t);
291 throw assertError;
292 }
293 } else if (processors != null) {
294 processorIterator = processors.iterator();
295 } else {
296 if (processorLoaderException == null) {
297 /*
298 * If the "-processor" option is used, search the appropriate
299 * path for the named class. Otherwise, use a service
300 * provider mechanism to create the processor iterator.
301 */
302 String processorNames = options.get(PROCESSOR);
303 if (fileManager.hasLocation(ANNOTATION_PROCESSOR_MODULE_PATH)) {
304 processorIterator = (processorNames == null) ?
305 new ServiceIterator(serviceLoader, log) :
306 new NameServiceIterator(serviceLoader, log, processorNames);
522 }
523 }
524 }
525
526 private static class NameProcessIterator implements Iterator<Processor> {
527 Processor nextProc = null;
528 Iterator<String> names;
529 ClassLoader processorCL;
530 Log log;
531
532 NameProcessIterator(String names, ClassLoader processorCL, Log log) {
533 this.names = Arrays.asList(names.split(",")).iterator();
534 this.processorCL = processorCL;
535 this.log = log;
536 }
537
538 public boolean hasNext() {
539 if (nextProc != null)
540 return true;
541 else {
542 if (!names.hasNext()) {
543 return false;
544 } else {
545 Processor processor = getNextProcessor(names.next());
546 if (processor == null) {
547 return false;
548 } else {
549 nextProc = processor;
550 return true;
551 }
552 }
553 }
554 }
555
556 private Processor getNextProcessor(String processorName) {
557 try {
558 try {
559 Class<?> processorClass = processorCL.loadClass(processorName);
560 ensureReadable(processorClass);
561 return (Processor) processorClass.getConstructor().newInstance();
562 } catch (ClassNotFoundException cnfe) {
563 log.error("proc.processor.not.found", processorName);
564 return null;
565 } catch (ClassCastException cce) {
566 log.error("proc.processor.wrong.type", processorName);
567 return null;
568 } catch (Exception e ) {
569 log.error("proc.processor.cant.instantiate", processorName);
570 return null;
571 }
572 } catch (ClientCodeException e) {
573 throw e;
574 } catch (Throwable t) {
575 throw new AnnotationProcessingError(t);
576 }
577 }
578
579 public Processor next() {
580 if (hasNext()) {
581 Processor p = nextProc;
582 nextProc = null;
583 return p;
584 } else
585 throw new NoSuchElementException();
586 }
587
588 public void remove () {
589 throw new UnsupportedOperationException();
590 }
591
592 /**
593 * Ensures that the module of the given class is readable to this
594 * module.
595 */
|