945N/A/*
945N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
945N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
945N/A *
945N/A * This code is free software; you can redistribute it and/or modify it
945N/A * under the terms of the GNU General Public License version 2 only, as
945N/A * published by the Free Software Foundation. Oracle designates this
945N/A * particular file as subject to the "Classpath" exception as provided
945N/A * by Oracle in the LICENSE file that accompanied this code.
945N/A *
945N/A * This code is distributed in the hope that it will be useful, but WITHOUT
945N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
945N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
945N/A * version 2 for more details (a copy is included in the LICENSE file that
945N/A * accompanied this code).
945N/A *
945N/A * You should have received a copy of the GNU General Public License version
945N/A * 2 along with this work; if not, write to the Free Software Foundation,
945N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
945N/A *
945N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
945N/A * or visit www.oracle.com if you need additional information or have any
945N/A * questions.
945N/A */
945N/A
945N/A
945N/Apackage com.sun.tools.javac.api;
945N/A
945N/Aimport java.io.IOException;
945N/Aimport java.io.InputStream;
945N/Aimport java.io.OutputStream;
945N/Aimport java.io.Reader;
945N/Aimport java.io.Writer;
945N/Aimport java.net.URI;
945N/Aimport java.util.ArrayList;
945N/Aimport java.util.Collections;
945N/Aimport java.util.HashMap;
945N/Aimport java.util.Iterator;
945N/Aimport java.util.List;
1058N/Aimport java.util.Locale;
945N/Aimport java.util.Map;
945N/Aimport java.util.Set;
945N/A
945N/Aimport javax.lang.model.element.NestingKind;
945N/Aimport javax.tools.Diagnostic;
945N/Aimport javax.tools.FileObject;
945N/Aimport javax.tools.JavaFileManager;
945N/Aimport javax.tools.JavaFileManager.Location;
945N/Aimport javax.tools.JavaFileObject;
945N/A
945N/Aimport com.sun.source.util.TaskEvent;
945N/Aimport com.sun.source.util.TaskListener;
945N/Aimport com.sun.tools.javac.util.ClientCodeException;
945N/Aimport com.sun.tools.javac.util.Context;
1058N/Aimport com.sun.tools.javac.util.JCDiagnostic;
945N/Aimport java.lang.annotation.ElementType;
945N/Aimport java.lang.annotation.Retention;
945N/Aimport java.lang.annotation.RetentionPolicy;
945N/Aimport java.lang.annotation.Target;
945N/Aimport javax.lang.model.element.Modifier;
945N/Aimport javax.tools.DiagnosticListener;
945N/Aimport javax.tools.JavaFileObject.Kind;
945N/A
945N/A/**
945N/A * Wrap objects to enable unchecked exceptions to be caught and handled.
945N/A *
945N/A * For each method, exceptions are handled as follows:
945N/A * <ul>
945N/A * <li>Checked exceptions are left alone and propogate upwards in the
945N/A * obvious way, since they are an expected aspect of the method's
945N/A * specification.
945N/A * <li>Unchecked exceptions which have already been caught and wrapped in
945N/A * ClientCodeException are left alone to continue propogating upwards.
945N/A * <li>All other unchecked exceptions (i.e. subtypes of RuntimeException
945N/A * and Error) and caught, and rethrown as a ClientCodeException with
945N/A * its cause set to the original exception.
945N/A * </ul>
945N/A *
945N/A * The intent is that ClientCodeException can be caught at an appropriate point
945N/A * in the program and can be distinguished from any unanticipated unchecked
945N/A * exceptions arising in the main body of the code (i.e. bugs.) When the
945N/A * ClientCodeException has been caught, either a suitable message can be
945N/A * generated, or if appropriate, the original cause can be rethrown.
945N/A *
945N/A * <p><b>This is NOT part of any supported API.
945N/A * If you write code that depends on this, you do so at your own risk.
945N/A * This code and its internal interfaces are subject to change or
945N/A * deletion without notice.</b>
945N/A */
945N/Apublic class ClientCodeWrapper {
945N/A @Retention(RetentionPolicy.RUNTIME)
945N/A @Target(ElementType.TYPE)
945N/A public @interface Trusted { }
945N/A
945N/A public static ClientCodeWrapper instance(Context context) {
945N/A ClientCodeWrapper instance = context.get(ClientCodeWrapper.class);
945N/A if (instance == null)
945N/A instance = new ClientCodeWrapper(context);
945N/A return instance;
945N/A }
945N/A
945N/A /**
945N/A * A map to cache the results of whether or not a specific classes can
945N/A * be "trusted", and thus does not need to be wrapped.
945N/A */
945N/A Map<Class<?>, Boolean> trustedClasses;
945N/A
945N/A protected ClientCodeWrapper(Context context) {
945N/A trustedClasses = new HashMap<Class<?>, Boolean>();
945N/A }
945N/A
945N/A public JavaFileManager wrap(JavaFileManager fm) {
945N/A if (isTrusted(fm))
945N/A return fm;
945N/A return new WrappedJavaFileManager(fm);
945N/A }
945N/A
945N/A public FileObject wrap(FileObject fo) {
945N/A if (isTrusted(fo))
945N/A return fo;
945N/A return new WrappedFileObject(fo);
945N/A }
945N/A
945N/A FileObject unwrap(FileObject fo) {
945N/A if (fo instanceof WrappedFileObject)
945N/A return ((WrappedFileObject) fo).clientFileObject;
945N/A else
945N/A return fo;
945N/A }
945N/A
945N/A public JavaFileObject wrap(JavaFileObject fo) {
945N/A if (isTrusted(fo))
945N/A return fo;
945N/A return new WrappedJavaFileObject(fo);
945N/A }
945N/A
945N/A public Iterable<JavaFileObject> wrapJavaFileObjects(Iterable<? extends JavaFileObject> list) {
945N/A List<JavaFileObject> wrapped = new ArrayList<JavaFileObject>();
945N/A for (JavaFileObject fo : list)
945N/A wrapped.add(wrap(fo));
945N/A return Collections.unmodifiableList(wrapped);
945N/A }
945N/A
945N/A JavaFileObject unwrap(JavaFileObject fo) {
945N/A if (fo instanceof WrappedJavaFileObject)
945N/A return ((JavaFileObject) ((WrappedJavaFileObject) fo).clientFileObject);
945N/A else
945N/A return fo;
945N/A }
945N/A
1058N/A <T /*super JavaFileOject*/> DiagnosticListener<T> wrap(DiagnosticListener<T> dl) {
945N/A if (isTrusted(dl))
945N/A return dl;
945N/A return new WrappedDiagnosticListener<T>(dl);
945N/A }
945N/A
945N/A TaskListener wrap(TaskListener tl) {
945N/A if (isTrusted(tl))
945N/A return tl;
945N/A return new WrappedTaskListener(tl);
945N/A }
945N/A
1058N/A @SuppressWarnings("unchecked")
1058N/A private <T> Diagnostic<T> unwrap(final Diagnostic<T> diagnostic) {
1058N/A if (diagnostic instanceof JCDiagnostic) {
1058N/A JCDiagnostic d = (JCDiagnostic) diagnostic;
1058N/A return (Diagnostic<T>) new DiagnosticSourceUnwrapper(d);
1058N/A } else {
1058N/A return diagnostic;
1058N/A }
1058N/A }
1058N/A
945N/A protected boolean isTrusted(Object o) {
945N/A Class<?> c = o.getClass();
945N/A Boolean trusted = trustedClasses.get(c);
945N/A if (trusted == null) {
945N/A trusted = c.getName().startsWith("com.sun.tools.javac.")
945N/A || c.isAnnotationPresent(Trusted.class);
945N/A trustedClasses.put(c, trusted);
945N/A }
945N/A return trusted;
945N/A }
945N/A
945N/A // <editor-fold defaultstate="collapsed" desc="Wrapper classes">
945N/A
945N/A // FIXME: all these classes should be converted to use multi-catch when
945N/A // that is available in the bootstrap compiler.
945N/A
945N/A protected class WrappedJavaFileManager implements JavaFileManager {
945N/A protected JavaFileManager clientJavaFileManager;
945N/A WrappedJavaFileManager(JavaFileManager clientJavaFileManager) {
945N/A clientJavaFileManager.getClass(); // null check
945N/A this.clientJavaFileManager = clientJavaFileManager;
945N/A }
945N/A
945N/A @Override
945N/A public ClassLoader getClassLoader(Location location) {
945N/A try {
945N/A return clientJavaFileManager.getClassLoader(location);
945N/A } catch (ClientCodeException e) {
945N/A throw e;
945N/A } catch (RuntimeException e) {
945N/A throw new ClientCodeException(e);
945N/A } catch (Error e) {
945N/A throw new ClientCodeException(e);
945N/A }
945N/A }
945N/A
945N/A @Override
945N/A public Iterable<JavaFileObject> list(Location location, String packageName, Set<Kind> kinds, boolean recurse) throws IOException {
945N/A try {
945N/A return wrapJavaFileObjects(clientJavaFileManager.list(location, packageName, kinds, recurse));
945N/A } catch (ClientCodeException e) {
945N/A throw e;
945N/A } catch (RuntimeException e) {
945N/A throw new ClientCodeException(e);
945N/A } catch (Error e) {
945N/A throw new ClientCodeException(e);
945N/A }
945N/A }
945N/A
945N/A @Override
945N/A public String inferBinaryName(Location location, JavaFileObject file) {
945N/A try {
945N/A return clientJavaFileManager.inferBinaryName(location, unwrap(file));
945N/A } catch (ClientCodeException e) {
945N/A throw e;
945N/A } catch (RuntimeException e) {
945N/A throw new ClientCodeException(e);
945N/A } catch (Error e) {
945N/A throw new ClientCodeException(e);
945N/A }
945N/A }
945N/A
945N/A @Override
945N/A public boolean isSameFile(FileObject a, FileObject b) {
945N/A try {
945N/A return clientJavaFileManager.isSameFile(unwrap(a), unwrap(b));
945N/A } catch (ClientCodeException e) {
945N/A throw e;
945N/A } catch (RuntimeException e) {
945N/A throw new ClientCodeException(e);
945N/A } catch (Error e) {
945N/A throw new ClientCodeException(e);
945N/A }
945N/A }
945N/A
945N/A @Override
945N/A public boolean handleOption(String current, Iterator<String> remaining) {
945N/A try {
945N/A return clientJavaFileManager.handleOption(current, remaining);
945N/A } catch (ClientCodeException e) {
945N/A throw e;
945N/A } catch (RuntimeException e) {
945N/A throw new ClientCodeException(e);
945N/A } catch (Error e) {
945N/A throw new ClientCodeException(e);
945N/A }
945N/A }
945N/A
945N/A @Override
945N/A public boolean hasLocation(Location location) {
945N/A try {
945N/A return clientJavaFileManager.hasLocation(location);
945N/A } catch (ClientCodeException e) {
945N/A throw e;
945N/A } catch (RuntimeException e) {
945N/A throw new ClientCodeException(e);
945N/A } catch (Error e) {
945N/A throw new ClientCodeException(e);
945N/A }
945N/A }
945N/A
945N/A @Override
945N/A public JavaFileObject getJavaFileForInput(Location location, String className, Kind kind) throws IOException {
945N/A try {
945N/A return wrap(clientJavaFileManager.getJavaFileForInput(location, className, kind));
945N/A } catch (ClientCodeException e) {
945N/A throw e;
945N/A } catch (RuntimeException e) {
945N/A throw new ClientCodeException(e);
945N/A } catch (Error e) {
945N/A throw new ClientCodeException(e);
945N/A }
945N/A }
945N/A
945N/A @Override
945N/A public JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling) throws IOException {
945N/A try {
945N/A return wrap(clientJavaFileManager.getJavaFileForOutput(location, className, kind, unwrap(sibling)));
945N/A } catch (ClientCodeException e) {
945N/A throw e;
945N/A } catch (RuntimeException e) {
945N/A throw new ClientCodeException(e);
945N/A } catch (Error e) {
945N/A throw new ClientCodeException(e);
945N/A }
945N/A }
945N/A
945N/A @Override
945N/A public FileObject getFileForInput(Location location, String packageName, String relativeName) throws IOException {
945N/A try {
945N/A return wrap(clientJavaFileManager.getFileForInput(location, packageName, relativeName));
945N/A } catch (ClientCodeException e) {
945N/A throw e;
945N/A } catch (RuntimeException e) {
945N/A throw new ClientCodeException(e);
945N/A } catch (Error e) {
945N/A throw new ClientCodeException(e);
945N/A }
945N/A }
945N/A
945N/A @Override
945N/A public FileObject getFileForOutput(Location location, String packageName, String relativeName, FileObject sibling) throws IOException {
945N/A try {
945N/A return wrap(clientJavaFileManager.getFileForOutput(location, packageName, relativeName, unwrap(sibling)));
945N/A } catch (ClientCodeException e) {
945N/A throw e;
945N/A } catch (RuntimeException e) {
945N/A throw new ClientCodeException(e);
945N/A } catch (Error e) {
945N/A throw new ClientCodeException(e);
945N/A }
945N/A }
945N/A
945N/A @Override
945N/A public void flush() throws IOException {
945N/A try {
945N/A clientJavaFileManager.flush();
945N/A } catch (ClientCodeException e) {
945N/A throw e;
945N/A } catch (RuntimeException e) {
945N/A throw new ClientCodeException(e);
945N/A } catch (Error e) {
945N/A throw new ClientCodeException(e);
945N/A }
945N/A }
945N/A
945N/A @Override
945N/A public void close() throws IOException {
945N/A try {
945N/A clientJavaFileManager.close();
945N/A } catch (ClientCodeException e) {
945N/A throw e;
945N/A } catch (RuntimeException e) {
945N/A throw new ClientCodeException(e);
945N/A } catch (Error e) {
945N/A throw new ClientCodeException(e);
945N/A }
945N/A }
945N/A
945N/A @Override
945N/A public int isSupportedOption(String option) {
945N/A try {
945N/A return clientJavaFileManager.isSupportedOption(option);
945N/A } catch (ClientCodeException e) {
945N/A throw e;
945N/A } catch (RuntimeException e) {
945N/A throw new ClientCodeException(e);
945N/A } catch (Error e) {
945N/A throw new ClientCodeException(e);
945N/A }
945N/A }
945N/A }
945N/A
945N/A protected class WrappedFileObject implements FileObject {
945N/A protected FileObject clientFileObject;
945N/A WrappedFileObject(FileObject clientFileObject) {
945N/A clientFileObject.getClass(); // null check
945N/A this.clientFileObject = clientFileObject;
945N/A }
945N/A
945N/A @Override
945N/A public URI toUri() {
945N/A try {
945N/A return clientFileObject.toUri();
945N/A } catch (ClientCodeException e) {
945N/A throw e;
945N/A } catch (RuntimeException e) {
945N/A throw new ClientCodeException(e);
945N/A } catch (Error e) {
945N/A throw new ClientCodeException(e);
945N/A }
945N/A }
945N/A
945N/A @Override
945N/A public String getName() {
945N/A try {
945N/A return clientFileObject.getName();
945N/A } catch (ClientCodeException e) {
945N/A throw e;
945N/A } catch (RuntimeException e) {
945N/A throw new ClientCodeException(e);
945N/A } catch (Error e) {
945N/A throw new ClientCodeException(e);
945N/A }
945N/A }
945N/A
945N/A @Override
945N/A public InputStream openInputStream() throws IOException {
945N/A try {
945N/A return clientFileObject.openInputStream();
945N/A } catch (ClientCodeException e) {
945N/A throw e;
945N/A } catch (RuntimeException e) {
945N/A throw new ClientCodeException(e);
945N/A } catch (Error e) {
945N/A throw new ClientCodeException(e);
945N/A }
945N/A }
945N/A
945N/A @Override
945N/A public OutputStream openOutputStream() throws IOException {
945N/A try {
945N/A return clientFileObject.openOutputStream();
945N/A } catch (ClientCodeException e) {
945N/A throw e;
945N/A } catch (RuntimeException e) {
945N/A throw new ClientCodeException(e);
945N/A } catch (Error e) {
945N/A throw new ClientCodeException(e);
945N/A }
945N/A }
945N/A
945N/A @Override
945N/A public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
945N/A try {
945N/A return clientFileObject.openReader(ignoreEncodingErrors);
945N/A } catch (ClientCodeException e) {
945N/A throw e;
945N/A } catch (RuntimeException e) {
945N/A throw new ClientCodeException(e);
945N/A } catch (Error e) {
945N/A throw new ClientCodeException(e);
945N/A }
945N/A }
945N/A
945N/A @Override
945N/A public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
945N/A try {
945N/A return clientFileObject.getCharContent(ignoreEncodingErrors);
945N/A } catch (ClientCodeException e) {
945N/A throw e;
945N/A } catch (RuntimeException e) {
945N/A throw new ClientCodeException(e);
945N/A } catch (Error e) {
945N/A throw new ClientCodeException(e);
945N/A }
945N/A }
945N/A
945N/A @Override
945N/A public Writer openWriter() throws IOException {
945N/A try {
945N/A return clientFileObject.openWriter();
945N/A } catch (ClientCodeException e) {
945N/A throw e;
945N/A } catch (RuntimeException e) {
945N/A throw new ClientCodeException(e);
945N/A } catch (Error e) {
945N/A throw new ClientCodeException(e);
945N/A }
945N/A }
945N/A
945N/A @Override
945N/A public long getLastModified() {
945N/A try {
945N/A return clientFileObject.getLastModified();
945N/A } catch (ClientCodeException e) {
945N/A throw e;
945N/A } catch (RuntimeException e) {
945N/A throw new ClientCodeException(e);
945N/A } catch (Error e) {
945N/A throw new ClientCodeException(e);
945N/A }
945N/A }
945N/A
945N/A @Override
945N/A public boolean delete() {
945N/A try {
945N/A return clientFileObject.delete();
945N/A } catch (ClientCodeException e) {
945N/A throw e;
945N/A } catch (RuntimeException e) {
945N/A throw new ClientCodeException(e);
945N/A } catch (Error e) {
945N/A throw new ClientCodeException(e);
945N/A }
945N/A }
945N/A }
945N/A
945N/A protected class WrappedJavaFileObject extends WrappedFileObject implements JavaFileObject {
945N/A WrappedJavaFileObject(JavaFileObject clientJavaFileObject) {
945N/A super(clientJavaFileObject);
945N/A }
945N/A
945N/A @Override
945N/A public Kind getKind() {
945N/A try {
945N/A return ((JavaFileObject)clientFileObject).getKind();
945N/A } catch (ClientCodeException e) {
945N/A throw e;
945N/A } catch (RuntimeException e) {
945N/A throw new ClientCodeException(e);
945N/A } catch (Error e) {
945N/A throw new ClientCodeException(e);
945N/A }
945N/A }
945N/A
945N/A @Override
945N/A public boolean isNameCompatible(String simpleName, Kind kind) {
945N/A try {
945N/A return ((JavaFileObject)clientFileObject).isNameCompatible(simpleName, kind);
945N/A } catch (ClientCodeException e) {
945N/A throw e;
945N/A } catch (RuntimeException e) {
945N/A throw new ClientCodeException(e);
945N/A } catch (Error e) {
945N/A throw new ClientCodeException(e);
945N/A }
945N/A }
945N/A
945N/A @Override
945N/A public NestingKind getNestingKind() {
945N/A try {
945N/A return ((JavaFileObject)clientFileObject).getNestingKind();
945N/A } catch (ClientCodeException e) {
945N/A throw e;
945N/A } catch (RuntimeException e) {
945N/A throw new ClientCodeException(e);
945N/A } catch (Error e) {
945N/A throw new ClientCodeException(e);
945N/A }
945N/A }
945N/A
945N/A @Override
945N/A public Modifier getAccessLevel() {
945N/A try {
945N/A return ((JavaFileObject)clientFileObject).getAccessLevel();
945N/A } catch (ClientCodeException e) {
945N/A throw e;
945N/A } catch (RuntimeException e) {
945N/A throw new ClientCodeException(e);
945N/A } catch (Error e) {
945N/A throw new ClientCodeException(e);
945N/A }
945N/A }
945N/A }
945N/A
1058N/A protected class WrappedDiagnosticListener<T /*super JavaFileObject*/> implements DiagnosticListener<T> {
945N/A protected DiagnosticListener<T> clientDiagnosticListener;
945N/A WrappedDiagnosticListener(DiagnosticListener<T> clientDiagnosticListener) {
945N/A clientDiagnosticListener.getClass(); // null check
945N/A this.clientDiagnosticListener = clientDiagnosticListener;
945N/A }
945N/A
945N/A @Override
945N/A public void report(Diagnostic<? extends T> diagnostic) {
945N/A try {
1058N/A clientDiagnosticListener.report(unwrap(diagnostic));
945N/A } catch (ClientCodeException e) {
945N/A throw e;
945N/A } catch (RuntimeException e) {
945N/A throw new ClientCodeException(e);
945N/A } catch (Error e) {
945N/A throw new ClientCodeException(e);
945N/A }
945N/A }
945N/A }
945N/A
1058N/A public class DiagnosticSourceUnwrapper implements Diagnostic<JavaFileObject> {
1058N/A public final JCDiagnostic d;
1058N/A
1058N/A DiagnosticSourceUnwrapper(JCDiagnostic d) {
1058N/A this.d = d;
1058N/A }
1058N/A
1058N/A public Diagnostic.Kind getKind() {
1058N/A return d.getKind();
1058N/A }
1058N/A
1058N/A public JavaFileObject getSource() {
1058N/A return unwrap(d.getSource());
1058N/A }
1058N/A
1058N/A public long getPosition() {
1058N/A return d.getPosition();
1058N/A }
1058N/A
1058N/A public long getStartPosition() {
1058N/A return d.getStartPosition();
1058N/A }
1058N/A
1058N/A public long getEndPosition() {
1058N/A return d.getEndPosition();
1058N/A }
1058N/A
1058N/A public long getLineNumber() {
1058N/A return d.getLineNumber();
1058N/A }
1058N/A
1058N/A public long getColumnNumber() {
1058N/A return d.getColumnNumber();
1058N/A }
1058N/A
1058N/A public String getCode() {
1058N/A return d.getCode();
1058N/A }
1058N/A
1058N/A public String getMessage(Locale locale) {
1058N/A return d.getMessage(locale);
1058N/A }
1059N/A
1059N/A public String toString() {
1059N/A return d.toString();
1059N/A }
1058N/A }
1058N/A
945N/A protected class WrappedTaskListener implements TaskListener {
945N/A protected TaskListener clientTaskListener;
945N/A WrappedTaskListener(TaskListener clientTaskListener) {
945N/A clientTaskListener.getClass(); // null check
945N/A this.clientTaskListener = clientTaskListener;
945N/A }
945N/A
945N/A @Override
945N/A public void started(TaskEvent ev) {
945N/A try {
945N/A clientTaskListener.started(ev);
945N/A } catch (ClientCodeException e) {
945N/A throw e;
945N/A } catch (RuntimeException e) {
945N/A throw new ClientCodeException(e);
945N/A } catch (Error e) {
945N/A throw new ClientCodeException(e);
945N/A }
945N/A }
945N/A
945N/A @Override
945N/A public void finished(TaskEvent ev) {
945N/A try {
945N/A clientTaskListener.finished(ev);
945N/A } catch (ClientCodeException e) {
945N/A throw e;
945N/A } catch (RuntimeException e) {
945N/A throw new ClientCodeException(e);
945N/A } catch (Error e) {
945N/A throw new ClientCodeException(e);
945N/A }
945N/A }
945N/A }
945N/A
945N/A // </editor-fold>
945N/A}