src/share/classes/com/sun/tools/javac/api/ClientCodeWrapper.java

changeset 1210
62e611704863
parent 1077
ec27e5befa53
child 1413
bdcef2ef52d2
equal deleted inserted replaced
1209:c4d6a8884ed8 1210:62e611704863
1 /* 1 /*
2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 2 * Copyright (c) 2011, 2012, 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
29 import java.io.IOException; 29 import java.io.IOException;
30 import java.io.InputStream; 30 import java.io.InputStream;
31 import java.io.OutputStream; 31 import java.io.OutputStream;
32 import java.io.Reader; 32 import java.io.Reader;
33 import java.io.Writer; 33 import java.io.Writer;
34 import java.lang.annotation.ElementType;
35 import java.lang.annotation.Retention;
36 import java.lang.annotation.RetentionPolicy;
37 import java.lang.annotation.Target;
34 import java.net.URI; 38 import java.net.URI;
35 import java.util.ArrayList; 39 import java.util.ArrayList;
40 import java.util.Collection;
36 import java.util.Collections; 41 import java.util.Collections;
37 import java.util.HashMap; 42 import java.util.HashMap;
38 import java.util.Iterator; 43 import java.util.Iterator;
39 import java.util.List; 44 import java.util.List;
40 import java.util.Locale; 45 import java.util.Locale;
41 import java.util.Map; 46 import java.util.Map;
42 import java.util.Set; 47 import java.util.Set;
43 48
49 import javax.lang.model.element.Modifier;
44 import javax.lang.model.element.NestingKind; 50 import javax.lang.model.element.NestingKind;
45 import javax.tools.Diagnostic; 51 import javax.tools.Diagnostic;
52 import javax.tools.DiagnosticListener;
46 import javax.tools.FileObject; 53 import javax.tools.FileObject;
47 import javax.tools.JavaFileManager; 54 import javax.tools.JavaFileManager;
48 import javax.tools.JavaFileManager.Location; 55 import javax.tools.JavaFileManager.Location;
49 import javax.tools.JavaFileObject; 56 import javax.tools.JavaFileObject;
57 import javax.tools.JavaFileObject.Kind;
50 58
51 import com.sun.source.util.TaskEvent; 59 import com.sun.source.util.TaskEvent;
52 import com.sun.source.util.TaskListener; 60 import com.sun.source.util.TaskListener;
53 import com.sun.tools.javac.util.ClientCodeException; 61 import com.sun.tools.javac.util.ClientCodeException;
54 import com.sun.tools.javac.util.Context; 62 import com.sun.tools.javac.util.Context;
55 import com.sun.tools.javac.util.JCDiagnostic; 63 import com.sun.tools.javac.util.JCDiagnostic;
56 import java.lang.annotation.ElementType;
57 import java.lang.annotation.Retention;
58 import java.lang.annotation.RetentionPolicy;
59 import java.lang.annotation.Target;
60 import javax.lang.model.element.Modifier;
61 import javax.tools.DiagnosticListener;
62 import javax.tools.JavaFileObject.Kind;
63 64
64 /** 65 /**
65 * Wrap objects to enable unchecked exceptions to be caught and handled. 66 * Wrap objects to enable unchecked exceptions to be caught and handled.
66 * 67 *
67 * For each method, exceptions are handled as follows: 68 * For each method, exceptions are handled as follows:
158 if (isTrusted(tl)) 159 if (isTrusted(tl))
159 return tl; 160 return tl;
160 return new WrappedTaskListener(tl); 161 return new WrappedTaskListener(tl);
161 } 162 }
162 163
164 TaskListener unwrap(TaskListener l) {
165 if (l instanceof WrappedTaskListener)
166 return ((WrappedTaskListener) l).clientTaskListener;
167 else
168 return l;
169 }
170
171 Collection<TaskListener> unwrap(Collection<? extends TaskListener> listeners) {
172 Collection<TaskListener> c = new ArrayList<TaskListener>(listeners.size());
173 for (TaskListener l: listeners)
174 c.add(unwrap(l));
175 return c;
176 }
177
163 @SuppressWarnings("unchecked") 178 @SuppressWarnings("unchecked")
164 private <T> Diagnostic<T> unwrap(final Diagnostic<T> diagnostic) { 179 private <T> Diagnostic<T> unwrap(final Diagnostic<T> diagnostic) {
165 if (diagnostic instanceof JCDiagnostic) { 180 if (diagnostic instanceof JCDiagnostic) {
166 JCDiagnostic d = (JCDiagnostic) diagnostic; 181 JCDiagnostic d = (JCDiagnostic) diagnostic;
167 return (Diagnostic<T>) new DiagnosticSourceUnwrapper(d); 182 return (Diagnostic<T>) new DiagnosticSourceUnwrapper(d);
179 trustedClasses.put(c, trusted); 194 trustedClasses.put(c, trusted);
180 } 195 }
181 return trusted; 196 return trusted;
182 } 197 }
183 198
199 private String wrappedToString(Class<?> wrapperClass, Object wrapped) {
200 return wrapperClass.getSimpleName() + "[" + wrapped + "]";
201 }
202
184 // <editor-fold defaultstate="collapsed" desc="Wrapper classes"> 203 // <editor-fold defaultstate="collapsed" desc="Wrapper classes">
185 204
186 // FIXME: all these classes should be converted to use multi-catch when 205 // FIXME: all these classes should be converted to use multi-catch when
187 // that is available in the bootstrap compiler. 206 // that is available in the bootstrap compiler.
188 207
359 throw new ClientCodeException(e); 378 throw new ClientCodeException(e);
360 } catch (Error e) { 379 } catch (Error e) {
361 throw new ClientCodeException(e); 380 throw new ClientCodeException(e);
362 } 381 }
363 } 382 }
383
384 @Override
385 public String toString() {
386 return wrappedToString(getClass(), clientJavaFileManager);
387 }
364 } 388 }
365 389
366 protected class WrappedFileObject implements FileObject { 390 protected class WrappedFileObject implements FileObject {
367 protected FileObject clientFileObject; 391 protected FileObject clientFileObject;
368 WrappedFileObject(FileObject clientFileObject) { 392 WrappedFileObject(FileObject clientFileObject) {
484 throw new ClientCodeException(e); 508 throw new ClientCodeException(e);
485 } catch (Error e) { 509 } catch (Error e) {
486 throw new ClientCodeException(e); 510 throw new ClientCodeException(e);
487 } 511 }
488 } 512 }
513
514 @Override
515 public String toString() {
516 return wrappedToString(getClass(), clientFileObject);
517 }
489 } 518 }
490 519
491 protected class WrappedJavaFileObject extends WrappedFileObject implements JavaFileObject { 520 protected class WrappedJavaFileObject extends WrappedFileObject implements JavaFileObject {
492 WrappedJavaFileObject(JavaFileObject clientJavaFileObject) { 521 WrappedJavaFileObject(JavaFileObject clientJavaFileObject) {
493 super(clientJavaFileObject); 522 super(clientJavaFileObject);
541 } catch (RuntimeException e) { 570 } catch (RuntimeException e) {
542 throw new ClientCodeException(e); 571 throw new ClientCodeException(e);
543 } catch (Error e) { 572 } catch (Error e) {
544 throw new ClientCodeException(e); 573 throw new ClientCodeException(e);
545 } 574 }
575 }
576
577 @Override
578 public String toString() {
579 return wrappedToString(getClass(), clientFileObject);
546 } 580 }
547 } 581 }
548 582
549 protected class WrappedDiagnosticListener<T /*super JavaFileObject*/> implements DiagnosticListener<T> { 583 protected class WrappedDiagnosticListener<T /*super JavaFileObject*/> implements DiagnosticListener<T> {
550 protected DiagnosticListener<T> clientDiagnosticListener; 584 protected DiagnosticListener<T> clientDiagnosticListener;
563 throw new ClientCodeException(e); 597 throw new ClientCodeException(e);
564 } catch (Error e) { 598 } catch (Error e) {
565 throw new ClientCodeException(e); 599 throw new ClientCodeException(e);
566 } 600 }
567 } 601 }
602
603 @Override
604 public String toString() {
605 return wrappedToString(getClass(), clientDiagnosticListener);
606 }
568 } 607 }
569 608
570 public class DiagnosticSourceUnwrapper implements Diagnostic<JavaFileObject> { 609 public class DiagnosticSourceUnwrapper implements Diagnostic<JavaFileObject> {
571 public final JCDiagnostic d; 610 public final JCDiagnostic d;
572 611
608 647
609 public String getMessage(Locale locale) { 648 public String getMessage(Locale locale) {
610 return d.getMessage(locale); 649 return d.getMessage(locale);
611 } 650 }
612 651
652 @Override
613 public String toString() { 653 public String toString() {
614 return d.toString(); 654 return d.toString();
615 } 655 }
616 } 656 }
617 657
645 throw new ClientCodeException(e); 685 throw new ClientCodeException(e);
646 } catch (Error e) { 686 } catch (Error e) {
647 throw new ClientCodeException(e); 687 throw new ClientCodeException(e);
648 } 688 }
649 } 689 }
690
691 @Override
692 public String toString() {
693 return wrappedToString(getClass(), clientTaskListener);
694 }
650 } 695 }
651 696
652 // </editor-fold> 697 // </editor-fold>
653 } 698 }

mercurial