[jdk9] Create FileObjects for jdk9

This commit is contained in:
Roel Spilker committed 2017-02-14 00:36:16 +01:00
1 parent 1d32894ca3
commit 05f93771e9
4 files changed
+189 -22

No files matched your search

@@ -0,0 +1,113 @@
/*
* Copyright (C) 2010-2017 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package lombok.javac.apt;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.net.URI;
import java.nio.file.Path;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.NestingKind;
import com.sun.tools.javac.file.BaseFileManager;
class Javac9BaseFileObjectWrapper extends com.sun.tools.javac.file.PathFileObject {
private final LombokFileObject delegate;
public Javac9BaseFileObjectWrapper(BaseFileManager fileManager, Path path, LombokFileObject delegate) {
super(fileManager, path);
this.delegate = delegate;
}
@Override public boolean isNameCompatible(String simpleName, Kind kind) {
return delegate.isNameCompatible(simpleName, kind);
}
@Override public URI toUri() {
return delegate.toUri();
}
@SuppressWarnings("all")
@Override public String getName() {
return delegate.getName();
}
@Override public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
return delegate.getCharContent(ignoreEncodingErrors);
}
@Override public InputStream openInputStream() throws IOException {
return delegate.openInputStream();
}
@Override public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
return delegate.openReader(ignoreEncodingErrors);
}
@Override public Writer openWriter() throws IOException {
return delegate.openWriter();
}
@Override public OutputStream openOutputStream() throws IOException {
return delegate.openOutputStream();
}
@Override public long getLastModified() {
return delegate.getLastModified();
}
@Override public boolean delete() {
return delegate.delete();
}
@Override public Kind getKind() {
return delegate.getKind();
}
@Override public NestingKind getNestingKind() {
return delegate.getNestingKind();
}
@Override public Modifier getAccessLevel() {
return delegate.getAccessLevel();
}
@Override public boolean equals(Object obj) {
if (!(obj instanceof Javac9BaseFileObjectWrapper)) {
return false;
}
return delegate.equals(((Javac9BaseFileObjectWrapper)obj).delegate);
}
@Override public int hashCode() {
return delegate.hashCode();
}
@Override public String toString() {
return delegate.toString();
}
}
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2010-2011 The Project Lombok Authors.
* Copyright (C) 2010-2017 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -23,19 +23,24 @@
package lombok.javac.apt;
import java.lang.reflect.Method;
import java.net.URI;
import java.nio.file.Paths;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import javax.tools.JavaFileObject.Kind;
import com.sun.tools.javac.file.BaseFileManager;
import lombok.core.DiagnosticsReceiver;
//Can't use SimpleJavaFileObject so we copy/paste most of its content here, because javac doesn't follow the interface,
//and casts to its own BaseFileObject type. D'oh!
final class LombokFileObjects {
enum Compiler {
JAVAC6 {
interface Compiler {
Compiler JAVAC6 = new Compiler() {
private Method decoderMethod = null;
private final AtomicBoolean decoderIsSet = new AtomicBoolean();
@@ -46,13 +51,13 @@ final class LombokFileObjects {
@Override public Method getDecoderMethod() {
synchronized (decoderIsSet) {
if (decoderIsSet.get()) return decoderMethod;
decoderMethod = getDecoderMethod("com.sun.tools.javac.util.BaseFileObject");
decoderMethod = LombokFileObjects.getDecoderMethod("com.sun.tools.javac.util.BaseFileObject");
decoderIsSet.set(true);
return decoderMethod;
}
}
},
JAVAC7 {
};
Compiler JAVAC7 = new Compiler() {
private Method decoderMethod = null;
private final AtomicBoolean decoderIsSet = new AtomicBoolean();
@@ -63,28 +68,28 @@ final class LombokFileObjects {
@Override public Method getDecoderMethod() {
synchronized (decoderIsSet) {
if (decoderIsSet.get()) return decoderMethod;
decoderMethod = getDecoderMethod("com.sun.tools.javac.file.BaseFileObject");
decoderMethod = LombokFileObjects.getDecoderMethod("com.sun.tools.javac.file.BaseFileObject");
decoderIsSet.set(true);
return decoderMethod;
}
}
};
static Method getDecoderMethod(String className) {
Method m = null;
try {
m = Class.forName(className).getDeclaredMethod("getDecoder", boolean.class);
m.setAccessible(true);
} catch (NoSuchMethodException e) {
// Intentional fallthrough - getDecoder(boolean) is not always present.
} catch (ClassNotFoundException e) {
// Intentional fallthrough - getDecoder(boolean) is not always present.
}
return m;
}
JavaFileObject wrap(LombokFileObject fileObject);
Method getDecoderMethod();
}
abstract JavaFileObject wrap(LombokFileObject fileObject);
abstract Method getDecoderMethod();
static Method getDecoderMethod(String className) {
Method m = null;
try {
m = Class.forName(className).getDeclaredMethod("getDecoder", boolean.class);
m.setAccessible(true);
} catch (NoSuchMethodException e) {
// Intentional fallthrough - getDecoder(boolean) is not always present.
} catch (ClassNotFoundException e) {
// Intentional fallthrough - getDecoder(boolean) is not always present.
}
return m;
}
private LombokFileObjects() {}
@@ -93,7 +98,16 @@ final class LombokFileObjects {
String jfmClassName = jfm != null ? jfm.getClass().getName() : "null";
if (jfmClassName.equals("com.sun.tools.javac.util.DefaultFileManager")) return Compiler.JAVAC6;
if (jfmClassName.equals("com.sun.tools.javac.util.JavacFileManager")) return Compiler.JAVAC6;
if (jfmClassName.equals("com.sun.tools.javac.file.JavacFileManager")) return Compiler.JAVAC7;
if (jfmClassName.equals("com.sun.tools.javac.file.JavacFileManager")) {
try {
Class<?> superType = Class.forName("com.sun.tools.javac.file.BaseFileManager");
if (superType.isInstance(jfm)) {
return new Java9Compiler(jfm);
}
}
catch (Exception e) {}
return Compiler.JAVAC7;
}
try {
if (Class.forName("com.sun.tools.javac.file.BaseFileObject") == null) throw new NullPointerException();
return Compiler.JAVAC7;
@@ -112,4 +126,24 @@ final class LombokFileObjects {
static JavaFileObject createIntercepting(Compiler compiler, JavaFileObject delegate, String fileName, DiagnosticsReceiver diagnostics) {
return compiler.wrap(new InterceptingJavaFileObject(delegate, fileName, diagnostics, compiler.getDecoderMethod()));
}
static class Java9Compiler implements Compiler {
private final BaseFileManager fileManager;
public Java9Compiler(JavaFileManager jfm) {
fileManager = (BaseFileManager) jfm;
}
@Override public JavaFileObject wrap(LombokFileObject fileObject) {
URI uri = fileObject.toUri();
if (uri.getScheme() == null) {
uri = URI.create("file://" + uri);
}
return new Javac9BaseFileObjectWrapper(fileManager, Paths.get(uri), fileObject);
}
@Override public Method getDecoderMethod() {
throw new UnsupportedOperationException();
}
}
}
@@ -0,0 +1,8 @@
/*
* These are stub versions of various bits of javac-internal API (for various different versions of javac). Lombok is compiled against these.
*/
package com.sun.tools.javac.file;
import javax.tools.JavaFileManager;
public abstract class BaseFileManager implements JavaFileManager{}
@@ -0,0 +1,12 @@
/*
* These are stub versions of various bits of javac-internal API (for various different versions of javac). Lombok is compiled against these.
*/
package com.sun.tools.javac.file;
import java.nio.file.Path;
import javax.tools.JavaFileObject;
public abstract class PathFileObject implements JavaFileObject {
protected PathFileObject(BaseFileManager fileManager, Path path) {}
}