Skip to content

Commit

Permalink
eliminate ‘you are using private API’ warnings by streamlining all re…
Browse files Browse the repository at this point in the history
…flective access via a class that uses sun.misc.Unsafe to arrange access. From the nqzero permit-reflect library.
  • Loading branch information
rzwitserloot committed Oct 29, 2018
1 parent 182cb0c commit eca219e
Show file tree
Hide file tree
Showing 32 changed files with 276 additions and 188 deletions.
3 changes: 2 additions & 1 deletion src/core/lombok/core/AST.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

import lombok.core.configuration.ConfigurationKey;
import lombok.core.debug.HistogramTracker;
import lombok.permit.Permit;

/**
* Lombok wraps the AST produced by a target platform into its own AST system, mostly because both Eclipse and javac
Expand Down Expand Up @@ -252,7 +253,7 @@ private void getFields(Class<?> c, Collection<FieldAccess> fields) {
}

if (shouldDrill(c, t, f.getName())) {
f.setAccessible(true);
Permit.setAccessible(f);
fields.add(new FieldAccess(f, dim));
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/lombok/core/AnnotationProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import javax.tools.Diagnostic.Kind;

import lombok.patcher.ClassRootFinder;
import lombok.permit.Permit;

@SupportedAnnotationTypes("*")
public class AnnotationProcessor extends AbstractProcessor {
Expand Down Expand Up @@ -82,8 +83,7 @@ private static ProcessingEnvironment tryRecursivelyObtainJavacProcessingEnvironm

for (Class<?> procEnvClass = procEnv.getClass(); procEnvClass != null; procEnvClass = procEnvClass.getSuperclass()) {
try {
Field field = procEnvClass.getDeclaredField("delegate");
field.setAccessible(true);
Field field = Permit.getField(procEnvClass, "delegate");
Object delegate = field.get(procEnv);

return tryRecursivelyObtainJavacProcessingEnvironment((ProcessingEnvironment) delegate);
Expand Down Expand Up @@ -136,7 +136,7 @@ private ClassLoader findAndPatchClassLoader(ProcessingEnvironment procEnv) throw
ClassLoader environmentClassLoader = procEnv.getClass().getClassLoader();
if (environmentClassLoader != null && environmentClassLoader.getClass().getCanonicalName().equals("org.codehaus.plexus.compiler.javac.IsolatedClassLoader")) {
if (!ClassLoader_lombokAlreadyAddedTo.getAndSet(environmentClassLoader, true)) {
Method m = environmentClassLoader.getClass().getDeclaredMethod("addURL", URL.class);
Method m = Permit.getMethod(environmentClassLoader.getClass(), "addURL", URL.class);
URL selfUrl = new File(ClassRootFinder.findClassRootOfClass(AnnotationProcessor.class)).toURI().toURL();
m.invoke(environmentClassLoader, selfUrl);
}
Expand Down
3 changes: 2 additions & 1 deletion src/core/lombok/core/AnnotationValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.Map;

import lombok.core.AST.Kind;
import lombok.permit.Permit;

/**
* Represents a single annotation in a source file and can be used to query the parameters present on it.
Expand Down Expand Up @@ -210,7 +211,7 @@ public boolean getAsBoolean(String methodName) {

public <T> T getDefaultIf(String methodName, Class<T> type, T defaultValue) {
try {
return type.cast(type.getMethod(methodName).getDefaultValue());
return type.cast(Permit.getMethod(type, methodName).getDefaultValue());
} catch (Exception e) {
return defaultValue;
}
Expand Down
3 changes: 2 additions & 1 deletion src/core/lombok/eclipse/EclipseAST.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import lombok.core.AST;
import lombok.core.LombokImmutableList;
import lombok.eclipse.handlers.EclipseHandlerUtil;
import lombok.permit.Permit;

import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
Expand Down Expand Up @@ -497,7 +498,7 @@ private static class EcjReflectionCheck {
Throwable problem_ = null;
Method m = null;
try {
m = EclipseAstProblemView.class.getMethod("addProblemToCompilationResult", char[].class, Class.forName(COMPILATIONRESULT_TYPE), boolean.class, String.class, int.class, int.class);
m = Permit.getMethod(EclipseAstProblemView.class, "addProblemToCompilationResult", char[].class, Class.forName(COMPILATIONRESULT_TYPE), boolean.class, String.class, int.class, int.class);
} catch (Throwable t) {
// That's problematic, but as long as no local classes are used we don't actually need it.
// Better fail on local classes than crash altogether.
Expand Down
3 changes: 2 additions & 1 deletion src/core/lombok/eclipse/TransformEclipseAST.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import lombok.core.debug.DebugSnapshotStore;
import lombok.core.debug.HistogramTracker;
import lombok.patcher.Symbols;
import lombok.permit.Permit;

import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
import org.eclipse.jdt.internal.compiler.ast.Annotation;
Expand Down Expand Up @@ -90,7 +91,7 @@ public class TransformEclipseAST {
disableLombok = true;
}
try {
f = CompilationUnitDeclaration.class.getDeclaredField("$lombokAST");
f = Permit.getField(CompilationUnitDeclaration.class, "$lombokAST");
} catch (Throwable t) {
//I guess we're in an ecj environment; we'll just not cache stuff then.
}
Expand Down
9 changes: 4 additions & 5 deletions src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import lombok.eclipse.EclipseNode;
import lombok.experimental.Accessors;
import lombok.experimental.Tolerate;
import lombok.permit.Permit;

import org.eclipse.jdt.internal.compiler.ast.ASTNode;
import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
Expand Down Expand Up @@ -337,9 +338,7 @@ public static Object reflect(Field f, Object o) {

private static Field getField(Class<?> c, String fName) {
try {
Field f = c.getDeclaredField(fName);
f.setAccessible(true);
return f;
return Permit.getField(c, fName);
} catch (Exception e) {
return null;
}
Expand Down Expand Up @@ -1931,12 +1930,12 @@ public static IntLiteral makeIntLiteral(char[] token, ASTNode source) {
Constructor<IntLiteral> intLiteralConstructor_ = null;
Method intLiteralFactoryMethod_ = null;
try {
intLiteralConstructor_ = IntLiteral.class.getConstructor(parameterTypes);
intLiteralConstructor_ = Permit.getConstructor(IntLiteral.class, parameterTypes);
} catch (Throwable ignore) {
// probably eclipse 3.7++
}
try {
intLiteralFactoryMethod_ = IntLiteral.class.getMethod("buildIntLiteral", parameterTypes);
intLiteralFactoryMethod_ = Permit.getMethod(IntLiteral.class, "buildIntLiteral", parameterTypes);
} catch (Throwable ignore) {
// probably eclipse versions before 3.7
}
Expand Down
9 changes: 4 additions & 5 deletions src/core/lombok/javac/CompilerMessageSuppressor.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Log;

import lombok.permit.Permit;

/**
* During resolution, the resolver will emit resolution errors, but without appropriate file names and line numbers. If these resolution errors stick around
* then they will be generated AGAIN, this time with proper names and line numbers, at the end. Therefore, we want to suppress the logger.
Expand Down Expand Up @@ -89,11 +91,8 @@ enum Writers {

static Field getDeclaredField(Class<?> c, String fieldName) {
try {
Field field = c.getDeclaredField(fieldName);
field.setAccessible(true);
return field;
}
catch (Throwable t) {
return Permit.getField(c, fieldName);
} catch (Throwable t) {
return null;
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/core/lombok/javac/Javac6BasedLombokOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.lang.reflect.Method;

import lombok.Lombok;
import lombok.permit.Permit;

import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.Options;
Expand All @@ -36,8 +37,8 @@ public class Javac6BasedLombokOptions extends LombokOptions {
static {
try {
Class<?> optionNameClass = Class.forName("com.sun.tools.javac.main.OptionName");
optionName_valueOf = optionNameClass.getMethod("valueOf", String.class);
options_put = Class.forName("com.sun.tools.javac.util.Options").getMethod("put", optionNameClass, String.class);
optionName_valueOf = Permit.getMethod(optionNameClass, "valueOf", String.class);
options_put = Permit.getMethod(Class.forName("com.sun.tools.javac.util.Options"), "put", optionNameClass, String.class);
} catch (Exception e) {
throw new IllegalArgumentException("Can't initialize Javac6-based lombok options due to reflection issue.", e);
}
Expand Down
52 changes: 24 additions & 28 deletions src/core/lombok/javac/JavacAST.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

import com.sun.tools.javac.util.JCDiagnostic;
import lombok.core.AST;
import lombok.permit.Permit;

import com.sun.tools.javac.code.Source;
import com.sun.tools.javac.code.Symtab;
Expand Down Expand Up @@ -243,7 +244,7 @@ private JavacNode buildLocalVar(JCVariableDecl local, Kind kind) {
private static List<JCTree> getResourcesForTryNode(JCTry tryNode) {
if (!JCTRY_RESOURCES_FIELD_INITIALIZED) {
try {
JCTRY_RESOURCES_FIELD = JCTry.class.getField("resources");
JCTRY_RESOURCES_FIELD = Permit.getField(JCTry.class, "resources");
} catch (NoSuchFieldException ignore) {
// Java 1.6 or lower won't have this at all.
} catch (Exception ignore) {
Expand Down Expand Up @@ -343,7 +344,7 @@ private Method getBodyMethod(Class<?> c) {
return m;
}
try {
m = c.getMethod("getBody");
m = Permit.getMethod(c, "getBody");
} catch (NoSuchMethodException e) {
throw Javac.sneakyThrow(e);
}
Expand Down Expand Up @@ -501,12 +502,11 @@ private void increment(Field field) {
}

static ErrorLog create(Messager messager, Log log) {
Field errorCount = null;
try {
Field f = messager.getClass().getDeclaredField("errorCount");
f.setAccessible(true);
errorCount = f;
} catch (Throwable t) {}
Field errorCount; try {
errorCount = Permit.getField(messager.getClass(), "errorCount");
} catch (Throwable t) {
errorCount = null;
}
boolean hasMultipleErrors = false;
for (Field field : log.getClass().getFields()) {
if (field.getName().equals("multipleErrors")) {
Expand All @@ -515,14 +515,13 @@ static ErrorLog create(Messager messager, Log log) {
}
}
if (hasMultipleErrors) return new JdkBefore9(log, messager, errorCount);

Field warningCount = null;
try {
Field f = messager.getClass().getDeclaredField("warningCount");
f.setAccessible(true);
warningCount = f;
} catch (Throwable t) {}


Field warningCount; try {
warningCount = Permit.getField(messager.getClass(), "warningCount");
} catch (Throwable t) {
warningCount = null;
}

return new Jdk9Plus(log, messager, errorCount, warningCount);
}
}
Expand Down Expand Up @@ -577,21 +576,18 @@ private Jdk9Plus(Log log, Messager messager, Field errorCount, Field warningCoun
Class<?> noteCls = Class.forName(jcd + "$Note");

Class<?> lc = log.getClass();
this.errorMethod = lc.getMethod("error", df, DiagnosticPosition.class, errorCls);
this.warningMethod = lc.getMethod("warning", DiagnosticPosition.class, warningCls);
this.mandatoryWarningMethod = lc.getMethod("mandatoryWarning", DiagnosticPosition.class, warningCls);
this.noteMethod = lc.getMethod("note", DiagnosticPosition.class, noteCls);
this.errorMethod = Permit.getMethod(lc, "error", df, DiagnosticPosition.class, errorCls);
this.warningMethod = Permit.getMethod(lc, "warning", DiagnosticPosition.class, warningCls);
this.mandatoryWarningMethod = Permit.getMethod(lc, "mandatoryWarning", DiagnosticPosition.class, warningCls);
this.noteMethod = Permit.getMethod(lc, "note", DiagnosticPosition.class, noteCls);

Field diagsField = lc.getSuperclass().getDeclaredField("diags");
diagsField.setAccessible(true);
this.diags = (JCDiagnostic.Factory)diagsField.get(log);
Field diagsField = Permit.getField(lc.getSuperclass(), "diags");
this.diags = (JCDiagnostic.Factory) diagsField.get(log);

Class<?> dc = this.diags.getClass();
this.errorKey = dc.getMethod("errorKey", String.class, Object[].class);
this.warningKey = dc.getDeclaredMethod("warningKey", String.class, Object[].class);
this.warningKey.setAccessible(true);
this.noteKey = dc.getDeclaredMethod("noteKey", String.class, Object[].class);
this.noteKey.setAccessible(true);
this.errorKey = Permit.getMethod(dc, "errorKey", String.class, Object[].class);
this.warningKey = Permit.getMethod(dc, "warningKey", String.class, Object[].class);
this.noteKey = Permit.getMethod(dc, "noteKey", String.class, Object[].class);
} catch (Throwable t) {
//t.printStackTrace();
}
Expand Down
11 changes: 4 additions & 7 deletions src/core/lombok/javac/JavacResolution.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

import lombok.Lombok;
import lombok.core.debug.AssertionLogger;
import lombok.permit.Permit;

import com.sun.tools.javac.code.BoundKind;
import com.sun.tools.javac.code.Symbol.TypeSymbol;
Expand Down Expand Up @@ -162,14 +163,10 @@ public Map<JCTree, JCTree> resolveMethodMember(JavacNode node) {
private static Field getMemberEnterDotEnv() {
if (memberEnterDotEnv != null) return memberEnterDotEnv;
try {
Field f = MemberEnter.class.getDeclaredField("env");
f.setAccessible(true);
memberEnterDotEnv = f;
return memberEnterDotEnv = Permit.getField(MemberEnter.class, "env");
} catch (NoSuchFieldException e) {
return null;
}

return memberEnterDotEnv;
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -252,10 +249,10 @@ private static class ReflectiveAccess {
static {
Method upperBound = null;
try {
upperBound = Types.class.getMethod("upperBound", Type.class);
upperBound = Permit.getMethod(Types.class, "upperBound", Type.class);
} catch (Throwable ignore) {}
if (upperBound == null) try {
upperBound = Types.class.getMethod("wildUpperBound", Type.class);
upperBound = Permit.getMethod(Types.class, "wildUpperBound", Type.class);
} catch (Throwable ignore) {}

UPPER_BOUND = upperBound;
Expand Down
7 changes: 3 additions & 4 deletions src/core/lombok/javac/apt/LombokFileObjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import javax.tools.JavaFileObject.Kind;

import lombok.core.DiagnosticsReceiver;
import lombok.permit.Permit;

import com.sun.tools.javac.file.BaseFileManager;

Expand Down Expand Up @@ -87,16 +88,14 @@ interface Compiler {
}

static Method getDecoderMethod(String className) {
Method m = null;
try {
m = Class.forName(className).getDeclaredMethod("getDecoder", boolean.class);
m.setAccessible(true);
return Permit.getMethod(Class.forName(className), "getDecoder", boolean.class);
} catch (NoSuchMethodException e) {
// Intentional fallthrough - getDecoder(boolean) is not always present.
} catch (ClassNotFoundException e) {
// Intentional fallthrough - getDecoder(boolean) is not always present.
}
return m;
return null;
}

private LombokFileObjects() {}
Expand Down
Loading

0 comments on commit eca219e

Please sign in to comment.