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

changeset 1073
f85d980faaf8
parent 946
31e5cfc5a990
child 1077
ec27e5befa53
equal deleted inserted replaced
1072:d0257833498e 1073:f85d980faaf8
35 import java.util.ArrayList; 35 import java.util.ArrayList;
36 import java.util.Collections; 36 import java.util.Collections;
37 import java.util.HashMap; 37 import java.util.HashMap;
38 import java.util.Iterator; 38 import java.util.Iterator;
39 import java.util.List; 39 import java.util.List;
40 import java.util.Locale;
40 import java.util.Map; 41 import java.util.Map;
41 import java.util.Set; 42 import java.util.Set;
42 43
43 import javax.lang.model.element.NestingKind; 44 import javax.lang.model.element.NestingKind;
44 import javax.tools.Diagnostic; 45 import javax.tools.Diagnostic;
49 50
50 import com.sun.source.util.TaskEvent; 51 import com.sun.source.util.TaskEvent;
51 import com.sun.source.util.TaskListener; 52 import com.sun.source.util.TaskListener;
52 import com.sun.tools.javac.util.ClientCodeException; 53 import com.sun.tools.javac.util.ClientCodeException;
53 import com.sun.tools.javac.util.Context; 54 import com.sun.tools.javac.util.Context;
55 import com.sun.tools.javac.util.JCDiagnostic;
54 import java.lang.annotation.ElementType; 56 import java.lang.annotation.ElementType;
55 import java.lang.annotation.Retention; 57 import java.lang.annotation.Retention;
56 import java.lang.annotation.RetentionPolicy; 58 import java.lang.annotation.RetentionPolicy;
57 import java.lang.annotation.Target; 59 import java.lang.annotation.Target;
58 import javax.lang.model.element.Modifier; 60 import javax.lang.model.element.Modifier;
144 return ((JavaFileObject) ((WrappedJavaFileObject) fo).clientFileObject); 146 return ((JavaFileObject) ((WrappedJavaFileObject) fo).clientFileObject);
145 else 147 else
146 return fo; 148 return fo;
147 } 149 }
148 150
149 <T> DiagnosticListener<T> wrap(DiagnosticListener<T> dl) { 151 <T /*super JavaFileOject*/> DiagnosticListener<T> wrap(DiagnosticListener<T> dl) {
150 if (isTrusted(dl)) 152 if (isTrusted(dl))
151 return dl; 153 return dl;
152 return new WrappedDiagnosticListener<T>(dl); 154 return new WrappedDiagnosticListener<T>(dl);
153 } 155 }
154 156
155 TaskListener wrap(TaskListener tl) { 157 TaskListener wrap(TaskListener tl) {
156 if (isTrusted(tl)) 158 if (isTrusted(tl))
157 return tl; 159 return tl;
158 return new WrappedTaskListener(tl); 160 return new WrappedTaskListener(tl);
161 }
162
163 @SuppressWarnings("unchecked")
164 private <T> Diagnostic<T> unwrap(final Diagnostic<T> diagnostic) {
165 if (diagnostic instanceof JCDiagnostic) {
166 JCDiagnostic d = (JCDiagnostic) diagnostic;
167 return (Diagnostic<T>) new DiagnosticSourceUnwrapper(d);
168 } else {
169 return diagnostic;
170 }
159 } 171 }
160 172
161 protected boolean isTrusted(Object o) { 173 protected boolean isTrusted(Object o) {
162 Class<?> c = o.getClass(); 174 Class<?> c = o.getClass();
163 Boolean trusted = trustedClasses.get(c); 175 Boolean trusted = trustedClasses.get(c);
532 throw new ClientCodeException(e); 544 throw new ClientCodeException(e);
533 } 545 }
534 } 546 }
535 } 547 }
536 548
537 protected class WrappedDiagnosticListener<T> implements DiagnosticListener<T> { 549 protected class WrappedDiagnosticListener<T /*super JavaFileObject*/> implements DiagnosticListener<T> {
538 protected DiagnosticListener<T> clientDiagnosticListener; 550 protected DiagnosticListener<T> clientDiagnosticListener;
539 WrappedDiagnosticListener(DiagnosticListener<T> clientDiagnosticListener) { 551 WrappedDiagnosticListener(DiagnosticListener<T> clientDiagnosticListener) {
540 clientDiagnosticListener.getClass(); // null check 552 clientDiagnosticListener.getClass(); // null check
541 this.clientDiagnosticListener = clientDiagnosticListener; 553 this.clientDiagnosticListener = clientDiagnosticListener;
542 } 554 }
543 555
544 @Override 556 @Override
545 public void report(Diagnostic<? extends T> diagnostic) { 557 public void report(Diagnostic<? extends T> diagnostic) {
546 try { 558 try {
547 clientDiagnosticListener.report(diagnostic); 559 clientDiagnosticListener.report(unwrap(diagnostic));
548 } catch (ClientCodeException e) { 560 } catch (ClientCodeException e) {
549 throw e; 561 throw e;
550 } catch (RuntimeException e) { 562 } catch (RuntimeException e) {
551 throw new ClientCodeException(e); 563 throw new ClientCodeException(e);
552 } catch (Error e) { 564 } catch (Error e) {
553 throw new ClientCodeException(e); 565 throw new ClientCodeException(e);
554 } 566 }
567 }
568 }
569
570 public class DiagnosticSourceUnwrapper implements Diagnostic<JavaFileObject> {
571 public final JCDiagnostic d;
572
573 DiagnosticSourceUnwrapper(JCDiagnostic d) {
574 this.d = d;
575 }
576
577 public Diagnostic.Kind getKind() {
578 return d.getKind();
579 }
580
581 public JavaFileObject getSource() {
582 return unwrap(d.getSource());
583 }
584
585 public long getPosition() {
586 return d.getPosition();
587 }
588
589 public long getStartPosition() {
590 return d.getStartPosition();
591 }
592
593 public long getEndPosition() {
594 return d.getEndPosition();
595 }
596
597 public long getLineNumber() {
598 return d.getLineNumber();
599 }
600
601 public long getColumnNumber() {
602 return d.getColumnNumber();
603 }
604
605 public String getCode() {
606 return d.getCode();
607 }
608
609 public String getMessage(Locale locale) {
610 return d.getMessage(locale);
555 } 611 }
556 } 612 }
557 613
558 protected class WrappedTaskListener implements TaskListener { 614 protected class WrappedTaskListener implements TaskListener {
559 protected TaskListener clientTaskListener; 615 protected TaskListener clientTaskListener;

mercurial