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

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javac/api/ClientCodeWrapper.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,698 @@
     1.4 +/*
     1.5 + * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +
    1.30 +package com.sun.tools.javac.api;
    1.31 +
    1.32 +import java.io.IOException;
    1.33 +import java.io.InputStream;
    1.34 +import java.io.OutputStream;
    1.35 +import java.io.Reader;
    1.36 +import java.io.Writer;
    1.37 +import java.lang.annotation.ElementType;
    1.38 +import java.lang.annotation.Retention;
    1.39 +import java.lang.annotation.RetentionPolicy;
    1.40 +import java.lang.annotation.Target;
    1.41 +import java.net.URI;
    1.42 +import java.util.ArrayList;
    1.43 +import java.util.Collection;
    1.44 +import java.util.Collections;
    1.45 +import java.util.HashMap;
    1.46 +import java.util.Iterator;
    1.47 +import java.util.List;
    1.48 +import java.util.Locale;
    1.49 +import java.util.Map;
    1.50 +import java.util.Set;
    1.51 +
    1.52 +import javax.lang.model.element.Modifier;
    1.53 +import javax.lang.model.element.NestingKind;
    1.54 +import javax.tools.Diagnostic;
    1.55 +import javax.tools.DiagnosticListener;
    1.56 +import javax.tools.FileObject;
    1.57 +import javax.tools.JavaFileManager;
    1.58 +import javax.tools.JavaFileManager.Location;
    1.59 +import javax.tools.JavaFileObject;
    1.60 +import javax.tools.JavaFileObject.Kind;
    1.61 +
    1.62 +import com.sun.source.util.TaskEvent;
    1.63 +import com.sun.source.util.TaskListener;
    1.64 +import com.sun.tools.javac.util.ClientCodeException;
    1.65 +import com.sun.tools.javac.util.Context;
    1.66 +import com.sun.tools.javac.util.JCDiagnostic;
    1.67 +
    1.68 +/**
    1.69 + *  Wrap objects to enable unchecked exceptions to be caught and handled.
    1.70 + *
    1.71 + *  For each method, exceptions are handled as follows:
    1.72 + *  <ul>
    1.73 + *  <li>Checked exceptions are left alone and propogate upwards in the
    1.74 + *      obvious way, since they are an expected aspect of the method's
    1.75 + *      specification.
    1.76 + *  <li>Unchecked exceptions which have already been caught and wrapped in
    1.77 + *      ClientCodeException are left alone to continue propogating upwards.
    1.78 + *  <li>All other unchecked exceptions (i.e. subtypes of RuntimeException
    1.79 + *      and Error) and caught, and rethrown as a ClientCodeException with
    1.80 + *      its cause set to the original exception.
    1.81 + *  </ul>
    1.82 + *
    1.83 + *  The intent is that ClientCodeException can be caught at an appropriate point
    1.84 + *  in the program and can be distinguished from any unanticipated unchecked
    1.85 + *  exceptions arising in the main body of the code (i.e. bugs.) When the
    1.86 + *  ClientCodeException has been caught, either a suitable message can be
    1.87 + *  generated, or if appropriate, the original cause can be rethrown.
    1.88 + *
    1.89 + *  <p><b>This is NOT part of any supported API.
    1.90 + *  If you write code that depends on this, you do so at your own risk.
    1.91 + *  This code and its internal interfaces are subject to change or
    1.92 + *  deletion without notice.</b>
    1.93 + */
    1.94 +public class ClientCodeWrapper {
    1.95 +    @Retention(RetentionPolicy.RUNTIME)
    1.96 +    @Target(ElementType.TYPE)
    1.97 +    public @interface Trusted { }
    1.98 +
    1.99 +    public static ClientCodeWrapper instance(Context context) {
   1.100 +        ClientCodeWrapper instance = context.get(ClientCodeWrapper.class);
   1.101 +        if (instance == null)
   1.102 +            instance = new ClientCodeWrapper(context);
   1.103 +        return instance;
   1.104 +    }
   1.105 +
   1.106 +    /**
   1.107 +     * A map to cache the results of whether or not a specific classes can
   1.108 +     * be "trusted", and thus does not need to be wrapped.
   1.109 +     */
   1.110 +    Map<Class<?>, Boolean> trustedClasses;
   1.111 +
   1.112 +    protected ClientCodeWrapper(Context context) {
   1.113 +        trustedClasses = new HashMap<Class<?>, Boolean>();
   1.114 +    }
   1.115 +
   1.116 +    public JavaFileManager wrap(JavaFileManager fm) {
   1.117 +        if (isTrusted(fm))
   1.118 +            return fm;
   1.119 +        return new WrappedJavaFileManager(fm);
   1.120 +    }
   1.121 +
   1.122 +    public FileObject wrap(FileObject fo) {
   1.123 +        if (isTrusted(fo))
   1.124 +            return fo;
   1.125 +        return new WrappedFileObject(fo);
   1.126 +    }
   1.127 +
   1.128 +    FileObject unwrap(FileObject fo) {
   1.129 +        if (fo instanceof WrappedFileObject)
   1.130 +            return ((WrappedFileObject) fo).clientFileObject;
   1.131 +        else
   1.132 +            return fo;
   1.133 +    }
   1.134 +
   1.135 +    public JavaFileObject wrap(JavaFileObject fo) {
   1.136 +        if (isTrusted(fo))
   1.137 +            return fo;
   1.138 +        return new WrappedJavaFileObject(fo);
   1.139 +    }
   1.140 +
   1.141 +    public Iterable<JavaFileObject> wrapJavaFileObjects(Iterable<? extends JavaFileObject> list) {
   1.142 +        List<JavaFileObject> wrapped = new ArrayList<JavaFileObject>();
   1.143 +        for (JavaFileObject fo : list)
   1.144 +            wrapped.add(wrap(fo));
   1.145 +        return Collections.unmodifiableList(wrapped);
   1.146 +    }
   1.147 +
   1.148 +    JavaFileObject unwrap(JavaFileObject fo) {
   1.149 +        if (fo instanceof WrappedJavaFileObject)
   1.150 +            return ((JavaFileObject) ((WrappedJavaFileObject) fo).clientFileObject);
   1.151 +        else
   1.152 +            return fo;
   1.153 +    }
   1.154 +
   1.155 +    public <T /*super JavaFileOject*/> DiagnosticListener<T> wrap(DiagnosticListener<T> dl) {
   1.156 +        if (isTrusted(dl))
   1.157 +            return dl;
   1.158 +        return new WrappedDiagnosticListener<T>(dl);
   1.159 +    }
   1.160 +
   1.161 +    TaskListener wrap(TaskListener tl) {
   1.162 +        if (isTrusted(tl))
   1.163 +            return tl;
   1.164 +        return new WrappedTaskListener(tl);
   1.165 +    }
   1.166 +
   1.167 +    TaskListener unwrap(TaskListener l) {
   1.168 +        if (l instanceof WrappedTaskListener)
   1.169 +            return ((WrappedTaskListener) l).clientTaskListener;
   1.170 +        else
   1.171 +            return l;
   1.172 +    }
   1.173 +
   1.174 +    Collection<TaskListener> unwrap(Collection<? extends TaskListener> listeners) {
   1.175 +        Collection<TaskListener> c = new ArrayList<TaskListener>(listeners.size());
   1.176 +        for (TaskListener l: listeners)
   1.177 +            c.add(unwrap(l));
   1.178 +        return c;
   1.179 +    }
   1.180 +
   1.181 +    @SuppressWarnings("unchecked")
   1.182 +    private <T> Diagnostic<T> unwrap(final Diagnostic<T> diagnostic) {
   1.183 +        if (diagnostic instanceof JCDiagnostic) {
   1.184 +            JCDiagnostic d = (JCDiagnostic) diagnostic;
   1.185 +            return (Diagnostic<T>) new DiagnosticSourceUnwrapper(d);
   1.186 +        } else {
   1.187 +            return diagnostic;
   1.188 +        }
   1.189 +    }
   1.190 +
   1.191 +    protected boolean isTrusted(Object o) {
   1.192 +        Class<?> c = o.getClass();
   1.193 +        Boolean trusted = trustedClasses.get(c);
   1.194 +        if (trusted == null) {
   1.195 +            trusted = c.getName().startsWith("com.sun.tools.javac.")
   1.196 +                    || c.isAnnotationPresent(Trusted.class);
   1.197 +            trustedClasses.put(c, trusted);
   1.198 +        }
   1.199 +        return trusted;
   1.200 +    }
   1.201 +
   1.202 +    private String wrappedToString(Class<?> wrapperClass, Object wrapped) {
   1.203 +        return wrapperClass.getSimpleName() + "[" + wrapped + "]";
   1.204 +    }
   1.205 +
   1.206 +    // <editor-fold defaultstate="collapsed" desc="Wrapper classes">
   1.207 +
   1.208 +    // FIXME: all these classes should be converted to use multi-catch when
   1.209 +    // that is available in the bootstrap compiler.
   1.210 +
   1.211 +    protected class WrappedJavaFileManager implements JavaFileManager {
   1.212 +        protected JavaFileManager clientJavaFileManager;
   1.213 +        WrappedJavaFileManager(JavaFileManager clientJavaFileManager) {
   1.214 +            clientJavaFileManager.getClass(); // null check
   1.215 +            this.clientJavaFileManager = clientJavaFileManager;
   1.216 +        }
   1.217 +
   1.218 +        @Override
   1.219 +        public ClassLoader getClassLoader(Location location) {
   1.220 +            try {
   1.221 +                return clientJavaFileManager.getClassLoader(location);
   1.222 +            } catch (ClientCodeException e) {
   1.223 +                throw e;
   1.224 +            } catch (RuntimeException e) {
   1.225 +                throw new ClientCodeException(e);
   1.226 +            } catch (Error e) {
   1.227 +                throw new ClientCodeException(e);
   1.228 +            }
   1.229 +        }
   1.230 +
   1.231 +        @Override
   1.232 +        public Iterable<JavaFileObject> list(Location location, String packageName, Set<Kind> kinds, boolean recurse) throws IOException {
   1.233 +            try {
   1.234 +                return wrapJavaFileObjects(clientJavaFileManager.list(location, packageName, kinds, recurse));
   1.235 +            } catch (ClientCodeException e) {
   1.236 +                throw e;
   1.237 +            } catch (RuntimeException e) {
   1.238 +                throw new ClientCodeException(e);
   1.239 +            } catch (Error e) {
   1.240 +                throw new ClientCodeException(e);
   1.241 +            }
   1.242 +        }
   1.243 +
   1.244 +        @Override
   1.245 +        public String inferBinaryName(Location location, JavaFileObject file) {
   1.246 +            try {
   1.247 +                return clientJavaFileManager.inferBinaryName(location, unwrap(file));
   1.248 +            } catch (ClientCodeException e) {
   1.249 +                throw e;
   1.250 +            } catch (RuntimeException e) {
   1.251 +                throw new ClientCodeException(e);
   1.252 +            } catch (Error e) {
   1.253 +                throw new ClientCodeException(e);
   1.254 +            }
   1.255 +        }
   1.256 +
   1.257 +        @Override
   1.258 +        public boolean isSameFile(FileObject a, FileObject b) {
   1.259 +            try {
   1.260 +                return clientJavaFileManager.isSameFile(unwrap(a), unwrap(b));
   1.261 +            } catch (ClientCodeException e) {
   1.262 +                throw e;
   1.263 +            } catch (RuntimeException e) {
   1.264 +                throw new ClientCodeException(e);
   1.265 +            } catch (Error e) {
   1.266 +                throw new ClientCodeException(e);
   1.267 +            }
   1.268 +        }
   1.269 +
   1.270 +        @Override
   1.271 +        public boolean handleOption(String current, Iterator<String> remaining) {
   1.272 +            try {
   1.273 +                return clientJavaFileManager.handleOption(current, remaining);
   1.274 +            } catch (ClientCodeException e) {
   1.275 +                throw e;
   1.276 +            } catch (RuntimeException e) {
   1.277 +                throw new ClientCodeException(e);
   1.278 +            } catch (Error e) {
   1.279 +                throw new ClientCodeException(e);
   1.280 +            }
   1.281 +        }
   1.282 +
   1.283 +        @Override
   1.284 +        public boolean hasLocation(Location location) {
   1.285 +            try {
   1.286 +                return clientJavaFileManager.hasLocation(location);
   1.287 +            } catch (ClientCodeException e) {
   1.288 +                throw e;
   1.289 +            } catch (RuntimeException e) {
   1.290 +                throw new ClientCodeException(e);
   1.291 +            } catch (Error e) {
   1.292 +                throw new ClientCodeException(e);
   1.293 +            }
   1.294 +        }
   1.295 +
   1.296 +        @Override
   1.297 +        public JavaFileObject getJavaFileForInput(Location location, String className, Kind kind) throws IOException {
   1.298 +            try {
   1.299 +                return wrap(clientJavaFileManager.getJavaFileForInput(location, className, kind));
   1.300 +            } catch (ClientCodeException e) {
   1.301 +                throw e;
   1.302 +            } catch (RuntimeException e) {
   1.303 +                throw new ClientCodeException(e);
   1.304 +            } catch (Error e) {
   1.305 +                throw new ClientCodeException(e);
   1.306 +            }
   1.307 +        }
   1.308 +
   1.309 +        @Override
   1.310 +        public JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling) throws IOException {
   1.311 +            try {
   1.312 +                return wrap(clientJavaFileManager.getJavaFileForOutput(location, className, kind, unwrap(sibling)));
   1.313 +            } catch (ClientCodeException e) {
   1.314 +                throw e;
   1.315 +            } catch (RuntimeException e) {
   1.316 +                throw new ClientCodeException(e);
   1.317 +            } catch (Error e) {
   1.318 +                throw new ClientCodeException(e);
   1.319 +            }
   1.320 +        }
   1.321 +
   1.322 +        @Override
   1.323 +        public FileObject getFileForInput(Location location, String packageName, String relativeName) throws IOException {
   1.324 +            try {
   1.325 +                return wrap(clientJavaFileManager.getFileForInput(location, packageName, relativeName));
   1.326 +            } catch (ClientCodeException e) {
   1.327 +                throw e;
   1.328 +            } catch (RuntimeException e) {
   1.329 +                throw new ClientCodeException(e);
   1.330 +            } catch (Error e) {
   1.331 +                throw new ClientCodeException(e);
   1.332 +            }
   1.333 +        }
   1.334 +
   1.335 +        @Override
   1.336 +        public FileObject getFileForOutput(Location location, String packageName, String relativeName, FileObject sibling) throws IOException {
   1.337 +            try {
   1.338 +                return wrap(clientJavaFileManager.getFileForOutput(location, packageName, relativeName, unwrap(sibling)));
   1.339 +            } catch (ClientCodeException e) {
   1.340 +                throw e;
   1.341 +            } catch (RuntimeException e) {
   1.342 +                throw new ClientCodeException(e);
   1.343 +            } catch (Error e) {
   1.344 +                throw new ClientCodeException(e);
   1.345 +            }
   1.346 +        }
   1.347 +
   1.348 +        @Override
   1.349 +        public void flush() throws IOException {
   1.350 +            try {
   1.351 +                clientJavaFileManager.flush();
   1.352 +            } catch (ClientCodeException e) {
   1.353 +                throw e;
   1.354 +            } catch (RuntimeException e) {
   1.355 +                throw new ClientCodeException(e);
   1.356 +            } catch (Error e) {
   1.357 +                throw new ClientCodeException(e);
   1.358 +            }
   1.359 +        }
   1.360 +
   1.361 +        @Override
   1.362 +        public void close() throws IOException {
   1.363 +            try {
   1.364 +                clientJavaFileManager.close();
   1.365 +            } catch (ClientCodeException e) {
   1.366 +                throw e;
   1.367 +            } catch (RuntimeException e) {
   1.368 +                throw new ClientCodeException(e);
   1.369 +            } catch (Error e) {
   1.370 +                throw new ClientCodeException(e);
   1.371 +            }
   1.372 +        }
   1.373 +
   1.374 +        @Override
   1.375 +        public int isSupportedOption(String option) {
   1.376 +            try {
   1.377 +                return clientJavaFileManager.isSupportedOption(option);
   1.378 +            } catch (ClientCodeException e) {
   1.379 +                throw e;
   1.380 +            } catch (RuntimeException e) {
   1.381 +                throw new ClientCodeException(e);
   1.382 +            } catch (Error e) {
   1.383 +                throw new ClientCodeException(e);
   1.384 +            }
   1.385 +        }
   1.386 +
   1.387 +        @Override
   1.388 +        public String toString() {
   1.389 +            return wrappedToString(getClass(), clientJavaFileManager);
   1.390 +        }
   1.391 +    }
   1.392 +
   1.393 +    protected class WrappedFileObject implements FileObject {
   1.394 +        protected FileObject clientFileObject;
   1.395 +        WrappedFileObject(FileObject clientFileObject) {
   1.396 +            clientFileObject.getClass(); // null check
   1.397 +            this.clientFileObject = clientFileObject;
   1.398 +        }
   1.399 +
   1.400 +        @Override
   1.401 +        public URI toUri() {
   1.402 +            try {
   1.403 +                return clientFileObject.toUri();
   1.404 +            } catch (ClientCodeException e) {
   1.405 +                throw e;
   1.406 +            } catch (RuntimeException e) {
   1.407 +                throw new ClientCodeException(e);
   1.408 +            } catch (Error e) {
   1.409 +                throw new ClientCodeException(e);
   1.410 +            }
   1.411 +        }
   1.412 +
   1.413 +        @Override
   1.414 +        public String getName() {
   1.415 +            try {
   1.416 +                return clientFileObject.getName();
   1.417 +            } catch (ClientCodeException e) {
   1.418 +                throw e;
   1.419 +            } catch (RuntimeException e) {
   1.420 +                throw new ClientCodeException(e);
   1.421 +            } catch (Error e) {
   1.422 +                throw new ClientCodeException(e);
   1.423 +            }
   1.424 +        }
   1.425 +
   1.426 +        @Override
   1.427 +        public InputStream openInputStream() throws IOException {
   1.428 +            try {
   1.429 +                return clientFileObject.openInputStream();
   1.430 +            } catch (ClientCodeException e) {
   1.431 +                throw e;
   1.432 +            } catch (RuntimeException e) {
   1.433 +                throw new ClientCodeException(e);
   1.434 +            } catch (Error e) {
   1.435 +                throw new ClientCodeException(e);
   1.436 +            }
   1.437 +        }
   1.438 +
   1.439 +        @Override
   1.440 +        public OutputStream openOutputStream() throws IOException {
   1.441 +            try {
   1.442 +                return clientFileObject.openOutputStream();
   1.443 +            } catch (ClientCodeException e) {
   1.444 +                throw e;
   1.445 +            } catch (RuntimeException e) {
   1.446 +                throw new ClientCodeException(e);
   1.447 +            } catch (Error e) {
   1.448 +                throw new ClientCodeException(e);
   1.449 +            }
   1.450 +        }
   1.451 +
   1.452 +        @Override
   1.453 +        public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
   1.454 +            try {
   1.455 +                return clientFileObject.openReader(ignoreEncodingErrors);
   1.456 +            } catch (ClientCodeException e) {
   1.457 +                throw e;
   1.458 +            } catch (RuntimeException e) {
   1.459 +                throw new ClientCodeException(e);
   1.460 +            } catch (Error e) {
   1.461 +                throw new ClientCodeException(e);
   1.462 +            }
   1.463 +        }
   1.464 +
   1.465 +        @Override
   1.466 +        public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
   1.467 +            try {
   1.468 +                return clientFileObject.getCharContent(ignoreEncodingErrors);
   1.469 +            } catch (ClientCodeException e) {
   1.470 +                throw e;
   1.471 +            } catch (RuntimeException e) {
   1.472 +                throw new ClientCodeException(e);
   1.473 +            } catch (Error e) {
   1.474 +                throw new ClientCodeException(e);
   1.475 +            }
   1.476 +        }
   1.477 +
   1.478 +        @Override
   1.479 +        public Writer openWriter() throws IOException {
   1.480 +            try {
   1.481 +                return clientFileObject.openWriter();
   1.482 +            } catch (ClientCodeException e) {
   1.483 +                throw e;
   1.484 +            } catch (RuntimeException e) {
   1.485 +                throw new ClientCodeException(e);
   1.486 +            } catch (Error e) {
   1.487 +                throw new ClientCodeException(e);
   1.488 +            }
   1.489 +        }
   1.490 +
   1.491 +        @Override
   1.492 +        public long getLastModified() {
   1.493 +            try {
   1.494 +                return clientFileObject.getLastModified();
   1.495 +            } catch (ClientCodeException e) {
   1.496 +                throw e;
   1.497 +            } catch (RuntimeException e) {
   1.498 +                throw new ClientCodeException(e);
   1.499 +            } catch (Error e) {
   1.500 +                throw new ClientCodeException(e);
   1.501 +            }
   1.502 +        }
   1.503 +
   1.504 +        @Override
   1.505 +        public boolean delete() {
   1.506 +            try {
   1.507 +                return clientFileObject.delete();
   1.508 +            } catch (ClientCodeException e) {
   1.509 +                throw e;
   1.510 +            } catch (RuntimeException e) {
   1.511 +                throw new ClientCodeException(e);
   1.512 +            } catch (Error e) {
   1.513 +                throw new ClientCodeException(e);
   1.514 +            }
   1.515 +        }
   1.516 +
   1.517 +        @Override
   1.518 +        public String toString() {
   1.519 +            return wrappedToString(getClass(), clientFileObject);
   1.520 +        }
   1.521 +    }
   1.522 +
   1.523 +    protected class WrappedJavaFileObject extends WrappedFileObject implements JavaFileObject {
   1.524 +        WrappedJavaFileObject(JavaFileObject clientJavaFileObject) {
   1.525 +            super(clientJavaFileObject);
   1.526 +        }
   1.527 +
   1.528 +        @Override
   1.529 +        public Kind getKind() {
   1.530 +            try {
   1.531 +                return ((JavaFileObject)clientFileObject).getKind();
   1.532 +            } catch (ClientCodeException e) {
   1.533 +                throw e;
   1.534 +            } catch (RuntimeException e) {
   1.535 +                throw new ClientCodeException(e);
   1.536 +            } catch (Error e) {
   1.537 +                throw new ClientCodeException(e);
   1.538 +            }
   1.539 +        }
   1.540 +
   1.541 +        @Override
   1.542 +        public boolean isNameCompatible(String simpleName, Kind kind) {
   1.543 +            try {
   1.544 +                return ((JavaFileObject)clientFileObject).isNameCompatible(simpleName, kind);
   1.545 +            } catch (ClientCodeException e) {
   1.546 +                throw e;
   1.547 +            } catch (RuntimeException e) {
   1.548 +                throw new ClientCodeException(e);
   1.549 +            } catch (Error e) {
   1.550 +                throw new ClientCodeException(e);
   1.551 +            }
   1.552 +        }
   1.553 +
   1.554 +        @Override
   1.555 +        public NestingKind getNestingKind() {
   1.556 +            try {
   1.557 +                return ((JavaFileObject)clientFileObject).getNestingKind();
   1.558 +            } catch (ClientCodeException e) {
   1.559 +                throw e;
   1.560 +            } catch (RuntimeException e) {
   1.561 +                throw new ClientCodeException(e);
   1.562 +            } catch (Error e) {
   1.563 +                throw new ClientCodeException(e);
   1.564 +            }
   1.565 +        }
   1.566 +
   1.567 +        @Override
   1.568 +        public Modifier getAccessLevel() {
   1.569 +            try {
   1.570 +                return ((JavaFileObject)clientFileObject).getAccessLevel();
   1.571 +            } catch (ClientCodeException e) {
   1.572 +                throw e;
   1.573 +            } catch (RuntimeException e) {
   1.574 +                throw new ClientCodeException(e);
   1.575 +            } catch (Error e) {
   1.576 +                throw new ClientCodeException(e);
   1.577 +            }
   1.578 +        }
   1.579 +
   1.580 +        @Override
   1.581 +        public String toString() {
   1.582 +            return wrappedToString(getClass(), clientFileObject);
   1.583 +        }
   1.584 +    }
   1.585 +
   1.586 +    protected class WrappedDiagnosticListener<T /*super JavaFileObject*/> implements DiagnosticListener<T> {
   1.587 +        protected DiagnosticListener<T> clientDiagnosticListener;
   1.588 +        WrappedDiagnosticListener(DiagnosticListener<T> clientDiagnosticListener) {
   1.589 +            clientDiagnosticListener.getClass(); // null check
   1.590 +            this.clientDiagnosticListener = clientDiagnosticListener;
   1.591 +        }
   1.592 +
   1.593 +        @Override
   1.594 +        public void report(Diagnostic<? extends T> diagnostic) {
   1.595 +            try {
   1.596 +                clientDiagnosticListener.report(unwrap(diagnostic));
   1.597 +            } catch (ClientCodeException e) {
   1.598 +                throw e;
   1.599 +            } catch (RuntimeException e) {
   1.600 +                throw new ClientCodeException(e);
   1.601 +            } catch (Error e) {
   1.602 +                throw new ClientCodeException(e);
   1.603 +            }
   1.604 +        }
   1.605 +
   1.606 +        @Override
   1.607 +        public String toString() {
   1.608 +            return wrappedToString(getClass(), clientDiagnosticListener);
   1.609 +        }
   1.610 +    }
   1.611 +
   1.612 +    public class DiagnosticSourceUnwrapper implements Diagnostic<JavaFileObject> {
   1.613 +        public final JCDiagnostic d;
   1.614 +
   1.615 +        DiagnosticSourceUnwrapper(JCDiagnostic d) {
   1.616 +            this.d = d;
   1.617 +        }
   1.618 +
   1.619 +        public Diagnostic.Kind getKind() {
   1.620 +            return d.getKind();
   1.621 +        }
   1.622 +
   1.623 +        public JavaFileObject getSource() {
   1.624 +            return unwrap(d.getSource());
   1.625 +        }
   1.626 +
   1.627 +        public long getPosition() {
   1.628 +            return d.getPosition();
   1.629 +        }
   1.630 +
   1.631 +        public long getStartPosition() {
   1.632 +            return d.getStartPosition();
   1.633 +        }
   1.634 +
   1.635 +        public long getEndPosition() {
   1.636 +            return d.getEndPosition();
   1.637 +        }
   1.638 +
   1.639 +        public long getLineNumber() {
   1.640 +            return d.getLineNumber();
   1.641 +        }
   1.642 +
   1.643 +        public long getColumnNumber() {
   1.644 +            return d.getColumnNumber();
   1.645 +        }
   1.646 +
   1.647 +        public String getCode() {
   1.648 +            return d.getCode();
   1.649 +        }
   1.650 +
   1.651 +        public String getMessage(Locale locale) {
   1.652 +            return d.getMessage(locale);
   1.653 +        }
   1.654 +
   1.655 +        @Override
   1.656 +        public String toString() {
   1.657 +            return d.toString();
   1.658 +        }
   1.659 +    }
   1.660 +
   1.661 +    protected class WrappedTaskListener implements TaskListener {
   1.662 +        protected TaskListener clientTaskListener;
   1.663 +        WrappedTaskListener(TaskListener clientTaskListener) {
   1.664 +            clientTaskListener.getClass(); // null check
   1.665 +            this.clientTaskListener = clientTaskListener;
   1.666 +        }
   1.667 +
   1.668 +        @Override
   1.669 +        public void started(TaskEvent ev) {
   1.670 +            try {
   1.671 +                clientTaskListener.started(ev);
   1.672 +            } catch (ClientCodeException e) {
   1.673 +                throw e;
   1.674 +            } catch (RuntimeException e) {
   1.675 +                throw new ClientCodeException(e);
   1.676 +            } catch (Error e) {
   1.677 +                throw new ClientCodeException(e);
   1.678 +            }
   1.679 +        }
   1.680 +
   1.681 +        @Override
   1.682 +        public void finished(TaskEvent ev) {
   1.683 +            try {
   1.684 +                clientTaskListener.finished(ev);
   1.685 +            } catch (ClientCodeException e) {
   1.686 +                throw e;
   1.687 +            } catch (RuntimeException e) {
   1.688 +                throw new ClientCodeException(e);
   1.689 +            } catch (Error e) {
   1.690 +                throw new ClientCodeException(e);
   1.691 +            }
   1.692 +        }
   1.693 +
   1.694 +        @Override
   1.695 +        public String toString() {
   1.696 +            return wrappedToString(getClass(), clientTaskListener);
   1.697 +        }
   1.698 +    }
   1.699 +
   1.700 +    // </editor-fold>
   1.701 +}

mercurial