< prev index next >

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

Print this page




  56 import javax.tools.FileObject;
  57 import javax.tools.JavaFileManager;
  58 import javax.tools.JavaFileManager.Location;
  59 import javax.tools.JavaFileObject;
  60 import javax.tools.JavaFileObject.Kind;
  61 import javax.tools.StandardJavaFileManager;
  62 
  63 import com.sun.source.util.TaskEvent;
  64 import com.sun.source.util.TaskListener;
  65 import com.sun.tools.javac.util.ClientCodeException;
  66 import com.sun.tools.javac.util.Context;
  67 import com.sun.tools.javac.util.DefinedBy;
  68 import com.sun.tools.javac.util.DefinedBy.Api;
  69 import com.sun.tools.javac.util.JCDiagnostic;
  70 
  71 /**
  72  *  Wrap objects to enable unchecked exceptions to be caught and handled.
  73  *
  74  *  For each method, exceptions are handled as follows:
  75  *  <ul>
  76  *  <li>Checked exceptions are left alone and propogate upwards in the
  77  *      obvious way, since they are an expected aspect of the method's
  78  *      specification.
  79  *  <li>Unchecked exceptions which have already been caught and wrapped in
  80  *      ClientCodeException are left alone to continue propogating upwards.
  81  *  <li>All other unchecked exceptions (i.e. subtypes of RuntimeException
  82  *      and Error) and caught, and rethrown as a ClientCodeException with
  83  *      its cause set to the original exception.
  84  *  </ul>
  85  *
  86  *  The intent is that ClientCodeException can be caught at an appropriate point
  87  *  in the program and can be distinguished from any unanticipated unchecked
  88  *  exceptions arising in the main body of the code (i.e. bugs.) When the
  89  *  ClientCodeException has been caught, either a suitable message can be
  90  *  generated, or if appropriate, the original cause can be rethrown.
  91  *
  92  *  <p><b>This is NOT part of any supported API.
  93  *  If you write code that depends on this, you do so at your own risk.
  94  *  This code and its internal interfaces are subject to change or
  95  *  deletion without notice.</b>
  96  */
  97 public class ClientCodeWrapper {
  98     @Retention(RetentionPolicy.RUNTIME)
  99     @Target(ElementType.TYPE)
 100     public @interface Trusted { }


 140     public JavaFileObject wrap(JavaFileObject fo) {
 141         if (fo == null || isTrusted(fo))
 142             return fo;
 143         return new WrappedJavaFileObject(fo);
 144     }
 145 
 146     public Iterable<JavaFileObject> wrapJavaFileObjects(Iterable<? extends JavaFileObject> list) {
 147         List<JavaFileObject> wrapped = new ArrayList<>();
 148         for (JavaFileObject fo : list)
 149             wrapped.add(wrap(fo));
 150         return Collections.unmodifiableList(wrapped);
 151     }
 152 
 153     JavaFileObject unwrap(JavaFileObject fo) {
 154         if (fo instanceof WrappedJavaFileObject)
 155             return ((JavaFileObject) ((WrappedJavaFileObject) fo).clientFileObject);
 156         else
 157             return fo;
 158     }
 159 
 160     public <T /*super JavaFileOject*/> DiagnosticListener<T> wrap(DiagnosticListener<T> dl) {
 161         if (isTrusted(dl))
 162             return dl;
 163         return new WrappedDiagnosticListener<>(dl);
 164     }
 165 
 166     TaskListener wrap(TaskListener tl) {
 167         if (isTrusted(tl))
 168             return tl;
 169         return new WrappedTaskListener(tl);
 170     }
 171 
 172     TaskListener unwrap(TaskListener l) {
 173         if (l instanceof WrappedTaskListener)
 174             return ((WrappedTaskListener) l).clientTaskListener;
 175         else
 176             return l;
 177     }
 178 
 179     Collection<TaskListener> unwrap(Collection<? extends TaskListener> listeners) {
 180         Collection<TaskListener> c = new ArrayList<>(listeners.size());




  56 import javax.tools.FileObject;
  57 import javax.tools.JavaFileManager;
  58 import javax.tools.JavaFileManager.Location;
  59 import javax.tools.JavaFileObject;
  60 import javax.tools.JavaFileObject.Kind;
  61 import javax.tools.StandardJavaFileManager;
  62 
  63 import com.sun.source.util.TaskEvent;
  64 import com.sun.source.util.TaskListener;
  65 import com.sun.tools.javac.util.ClientCodeException;
  66 import com.sun.tools.javac.util.Context;
  67 import com.sun.tools.javac.util.DefinedBy;
  68 import com.sun.tools.javac.util.DefinedBy.Api;
  69 import com.sun.tools.javac.util.JCDiagnostic;
  70 
  71 /**
  72  *  Wrap objects to enable unchecked exceptions to be caught and handled.
  73  *
  74  *  For each method, exceptions are handled as follows:
  75  *  <ul>
  76  *  <li>Checked exceptions are left alone and propagate upwards in the
  77  *      obvious way, since they are an expected aspect of the method's
  78  *      specification.
  79  *  <li>Unchecked exceptions which have already been caught and wrapped in
  80  *      ClientCodeException are left alone to continue propagating upwards.
  81  *  <li>All other unchecked exceptions (i.e. subtypes of RuntimeException
  82  *      and Error) and caught, and rethrown as a ClientCodeException with
  83  *      its cause set to the original exception.
  84  *  </ul>
  85  *
  86  *  The intent is that ClientCodeException can be caught at an appropriate point
  87  *  in the program and can be distinguished from any unanticipated unchecked
  88  *  exceptions arising in the main body of the code (i.e. bugs.) When the
  89  *  ClientCodeException has been caught, either a suitable message can be
  90  *  generated, or if appropriate, the original cause can be rethrown.
  91  *
  92  *  <p><b>This is NOT part of any supported API.
  93  *  If you write code that depends on this, you do so at your own risk.
  94  *  This code and its internal interfaces are subject to change or
  95  *  deletion without notice.</b>
  96  */
  97 public class ClientCodeWrapper {
  98     @Retention(RetentionPolicy.RUNTIME)
  99     @Target(ElementType.TYPE)
 100     public @interface Trusted { }


 140     public JavaFileObject wrap(JavaFileObject fo) {
 141         if (fo == null || isTrusted(fo))
 142             return fo;
 143         return new WrappedJavaFileObject(fo);
 144     }
 145 
 146     public Iterable<JavaFileObject> wrapJavaFileObjects(Iterable<? extends JavaFileObject> list) {
 147         List<JavaFileObject> wrapped = new ArrayList<>();
 148         for (JavaFileObject fo : list)
 149             wrapped.add(wrap(fo));
 150         return Collections.unmodifiableList(wrapped);
 151     }
 152 
 153     JavaFileObject unwrap(JavaFileObject fo) {
 154         if (fo instanceof WrappedJavaFileObject)
 155             return ((JavaFileObject) ((WrappedJavaFileObject) fo).clientFileObject);
 156         else
 157             return fo;
 158     }
 159 
 160     public <T /*super JavaFileObject*/> DiagnosticListener<T> wrap(DiagnosticListener<T> dl) {
 161         if (isTrusted(dl))
 162             return dl;
 163         return new WrappedDiagnosticListener<>(dl);
 164     }
 165 
 166     TaskListener wrap(TaskListener tl) {
 167         if (isTrusted(tl))
 168             return tl;
 169         return new WrappedTaskListener(tl);
 170     }
 171 
 172     TaskListener unwrap(TaskListener l) {
 173         if (l instanceof WrappedTaskListener)
 174             return ((WrappedTaskListener) l).clientTaskListener;
 175         else
 176             return l;
 177     }
 178 
 179     Collection<TaskListener> unwrap(Collection<? extends TaskListener> listeners) {
 180         Collection<TaskListener> c = new ArrayList<>(listeners.size());


< prev index next >