diff --git a/src/core/lombok/core/AST.java b/src/core/lombok/core/AST.java
index fe7a4330..afbba1e8 100644
--- a/src/core/lombok/core/AST.java
+++ b/src/core/lombok/core/AST.java
@@ -39,6 +39,7 @@ import java.util.concurrent.ConcurrentMap;
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
@@ -252,7 +253,7 @@ public abstract class AST, L extends LombokNode,
}
if (shouldDrill(c, t, f.getName())) {
- f.setAccessible(true);
+ Permit.setAccessible(f);
fields.add(new FieldAccess(f, dim));
}
}
diff --git a/src/core/lombok/core/AnnotationProcessor.java b/src/core/lombok/core/AnnotationProcessor.java
index 293bfef6..363952a4 100644
--- a/src/core/lombok/core/AnnotationProcessor.java
+++ b/src/core/lombok/core/AnnotationProcessor.java
@@ -46,6 +46,7 @@ import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic.Kind;
import lombok.patcher.ClassRootFinder;
+import lombok.permit.Permit;
@SupportedAnnotationTypes("*")
public class AnnotationProcessor extends AbstractProcessor {
@@ -82,8 +83,7 @@ public class AnnotationProcessor extends AbstractProcessor {
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);
@@ -136,7 +136,7 @@ public class AnnotationProcessor extends AbstractProcessor {
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);
}
diff --git a/src/core/lombok/core/AnnotationValues.java b/src/core/lombok/core/AnnotationValues.java
index a24330fa..eec5abd8 100644
--- a/src/core/lombok/core/AnnotationValues.java
+++ b/src/core/lombok/core/AnnotationValues.java
@@ -33,6 +33,7 @@ import java.util.List;
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.
@@ -210,7 +211,7 @@ public class AnnotationValues {
public T getDefaultIf(String methodName, Class type, T defaultValue) {
try {
- return type.cast(type.getMethod(methodName).getDefaultValue());
+ return type.cast(Permit.getMethod(type, methodName).getDefaultValue());
} catch (Exception e) {
return defaultValue;
}
diff --git a/src/core/lombok/eclipse/EclipseAST.java b/src/core/lombok/eclipse/EclipseAST.java
index 7cd2e400..1ba26338 100644
--- a/src/core/lombok/eclipse/EclipseAST.java
+++ b/src/core/lombok/eclipse/EclipseAST.java
@@ -34,6 +34,7 @@ import lombok.Lombok;
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;
@@ -497,7 +498,7 @@ public class EclipseAST extends AST {
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.
diff --git a/src/core/lombok/eclipse/TransformEclipseAST.java b/src/core/lombok/eclipse/TransformEclipseAST.java
index 323fc171..e5edba64 100644
--- a/src/core/lombok/eclipse/TransformEclipseAST.java
+++ b/src/core/lombok/eclipse/TransformEclipseAST.java
@@ -30,6 +30,7 @@ import lombok.core.LombokConfiguration;
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;
@@ -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.
}
diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
index 5d582aad..c69b3d0f 100644
--- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
+++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
@@ -56,6 +56,7 @@ import lombok.eclipse.EclipseAST;
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;
@@ -337,9 +338,7 @@ public class EclipseHandlerUtil {
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;
}
@@ -1931,12 +1930,12 @@ public class EclipseHandlerUtil {
Constructor 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
}
diff --git a/src/core/lombok/javac/CompilerMessageSuppressor.java b/src/core/lombok/javac/CompilerMessageSuppressor.java
index 391ec64a..02dc6c26 100644
--- a/src/core/lombok/javac/CompilerMessageSuppressor.java
+++ b/src/core/lombok/javac/CompilerMessageSuppressor.java
@@ -39,6 +39,8 @@ import com.sun.tools.javac.util.JCDiagnostic;
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.
@@ -89,11 +91,8 @@ public final class CompilerMessageSuppressor {
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;
}
}
diff --git a/src/core/lombok/javac/Javac6BasedLombokOptions.java b/src/core/lombok/javac/Javac6BasedLombokOptions.java
index cefb89ff..fc73181c 100644
--- a/src/core/lombok/javac/Javac6BasedLombokOptions.java
+++ b/src/core/lombok/javac/Javac6BasedLombokOptions.java
@@ -25,6 +25,7 @@ import java.lang.reflect.InvocationTargetException;
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;
@@ -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);
}
diff --git a/src/core/lombok/javac/JavacAST.java b/src/core/lombok/javac/JavacAST.java
index 091612cc..9a5305a6 100644
--- a/src/core/lombok/javac/JavacAST.java
+++ b/src/core/lombok/javac/JavacAST.java
@@ -37,6 +37,7 @@ import javax.tools.JavaFileObject;
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;
@@ -243,7 +244,7 @@ public class JavacAST extends AST {
private static List 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) {
@@ -343,7 +344,7 @@ public class JavacAST extends AST {
return m;
}
try {
- m = c.getMethod("getBody");
+ m = Permit.getMethod(c, "getBody");
} catch (NoSuchMethodException e) {
throw Javac.sneakyThrow(e);
}
@@ -501,12 +502,11 @@ public class JavacAST extends AST {
}
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")) {
@@ -515,14 +515,13 @@ public class JavacAST extends AST {
}
}
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);
}
}
@@ -577,21 +576,18 @@ public class JavacAST extends AST {
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();
}
diff --git a/src/core/lombok/javac/JavacResolution.java b/src/core/lombok/javac/JavacResolution.java
index 8cc239e1..abbf6726 100644
--- a/src/core/lombok/javac/JavacResolution.java
+++ b/src/core/lombok/javac/JavacResolution.java
@@ -35,6 +35,7 @@ import javax.tools.JavaFileObject;
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;
@@ -162,14 +163,10 @@ public class JavacResolution {
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")
@@ -252,10 +249,10 @@ public class JavacResolution {
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;
diff --git a/src/core/lombok/javac/apt/LombokFileObjects.java b/src/core/lombok/javac/apt/LombokFileObjects.java
index aba10540..28d3c9fa 100644
--- a/src/core/lombok/javac/apt/LombokFileObjects.java
+++ b/src/core/lombok/javac/apt/LombokFileObjects.java
@@ -39,6 +39,7 @@ import javax.tools.JavaFileObject;
import javax.tools.JavaFileObject.Kind;
import lombok.core.DiagnosticsReceiver;
+import lombok.permit.Permit;
import com.sun.tools.javac.file.BaseFileManager;
@@ -87,16 +88,14 @@ final class LombokFileObjects {
}
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() {}
diff --git a/src/core/lombok/javac/apt/LombokProcessor.java b/src/core/lombok/javac/apt/LombokProcessor.java
index 247d0560..a3d1dfcf 100644
--- a/src/core/lombok/javac/apt/LombokProcessor.java
+++ b/src/core/lombok/javac/apt/LombokProcessor.java
@@ -52,6 +52,7 @@ import javax.tools.JavaFileObject;
import lombok.Lombok;
import lombok.core.DiagnosticsReceiver;
import lombok.javac.JavacTransformer;
+import lombok.permit.Permit;
import com.sun.source.util.TreePath;
import com.sun.source.util.Trees;
@@ -112,10 +113,7 @@ public class LombokProcessor extends AbstractProcessor {
private static final Field getFieldAccessor(String typeName, String fieldName) {
try {
- Class> c = Class.forName(typeName);
- Field f = c.getDeclaredField(fieldName);
- f.setAccessible(true);
- return f;
+ return Permit.getField(Class.forName(typeName), fieldName);
} catch (ClassNotFoundException e) {
return null;
} catch (NoSuchFieldException e) {
@@ -156,11 +154,9 @@ public class LombokProcessor extends AbstractProcessor {
Context context = javacProcessingEnv.getContext();
disablePartialReparseInNetBeansEditor(context);
try {
- Method keyMethod = Context.class.getDeclaredMethod("key", Class.class);
- keyMethod.setAccessible(true);
+ Method keyMethod = Permit.getMethod(Context.class, "key", Class.class);
Object key = keyMethod.invoke(context, JavaFileManager.class);
- Field htField = Context.class.getDeclaredField("ht");
- htField.setAccessible(true);
+ Field htField = Permit.getField(Context.class, "ht");
@SuppressWarnings("unchecked")
Map