diff options
author | Reinier Zwitserloot <reinier@zwitserloot.com> | 2018-10-29 23:13:52 +0100 |
---|---|---|
committer | Reinier Zwitserloot <reinier@zwitserloot.com> | 2018-10-29 23:13:59 +0100 |
commit | eca219ee6433cd964f0549a114a791ca4eb9f0fa (patch) | |
tree | 20f6fed449504fbf5dbc52bd15ff3f2458dd90f8 /src/core/lombok | |
parent | 182cb0cb9e8db6341fb4633c3849b5e90ba6d088 (diff) | |
download | lombok-eca219ee6433cd964f0549a114a791ca4eb9f0fa.tar.gz lombok-eca219ee6433cd964f0549a114a791ca4eb9f0fa.tar.bz2 lombok-eca219ee6433cd964f0549a114a791ca4eb9f0fa.zip |
eliminate ‘you are using private API’ warnings by streamlining all reflective access via a class that uses sun.misc.Unsafe to arrange access. From the nqzero permit-reflect library.
Diffstat (limited to 'src/core/lombok')
-rw-r--r-- | src/core/lombok/core/AST.java | 3 | ||||
-rw-r--r-- | src/core/lombok/core/AnnotationProcessor.java | 6 | ||||
-rw-r--r-- | src/core/lombok/core/AnnotationValues.java | 3 | ||||
-rw-r--r-- | src/core/lombok/eclipse/EclipseAST.java | 3 | ||||
-rw-r--r-- | src/core/lombok/eclipse/TransformEclipseAST.java | 3 | ||||
-rw-r--r-- | src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java | 9 | ||||
-rw-r--r-- | src/core/lombok/javac/CompilerMessageSuppressor.java | 9 | ||||
-rw-r--r-- | src/core/lombok/javac/Javac6BasedLombokOptions.java | 5 | ||||
-rw-r--r-- | src/core/lombok/javac/JavacAST.java | 52 | ||||
-rw-r--r-- | src/core/lombok/javac/JavacResolution.java | 11 | ||||
-rw-r--r-- | src/core/lombok/javac/apt/LombokFileObjects.java | 7 | ||||
-rw-r--r-- | src/core/lombok/javac/apt/LombokProcessor.java | 68 | ||||
-rw-r--r-- | src/core/lombok/javac/apt/Processor.java | 8 | ||||
-rw-r--r-- | src/core/lombok/javac/handlers/JavacHandlerUtil.java | 14 |
14 files changed, 89 insertions, 112 deletions
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<A extends AST<A, L, N>, L extends LombokNode<A, L, N>, } 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<A extends Annotation> { 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; } 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<EclipseAST, EclipseNode, ASTNode> { 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<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 } 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<JavacAST, JavacNode, JCTree> { 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) { @@ -343,7 +344,7 @@ public class JavacAST extends AST<JavacAST, JavacNode, JCTree> { 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<JavacAST, JavacNode, JCTree> { } 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<JavacAST, JavacNode, JCTree> { } } 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<JavacAST, JavacNode, JCTree> { 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<Object,Object> ht = (Map<Object,Object>) htField.get(context); final JavaFileManager originalFiler = (JavaFileManager) ht.get(key); @@ -170,8 +166,7 @@ public class LombokProcessor extends AbstractProcessor { JavaFileManager newFilerManager = new InterceptingJavaFileManager(originalFiler, receiver); ht.put(key, newFilerManager); - Field filerFileManagerField = JavacFiler.class.getDeclaredField("fileManager"); - filerFileManagerField.setAccessible(true); + Field filerFileManagerField = Permit.getField(JavacFiler.class, "fileManager"); filerFileManagerField.set(javacFiler, newFilerManager); if (lombok.javac.Javac.getJavaCompilerVersion() > 8 @@ -186,20 +181,17 @@ public class LombokProcessor extends AbstractProcessor { private void replaceFileManagerJdk9(Context context, JavaFileManager newFiler) { try { - JavaCompiler compiler = (JavaCompiler) JavaCompiler.class.getDeclaredMethod("instance", Context.class).invoke(null, context); + JavaCompiler compiler = (JavaCompiler) Permit.getMethod(JavaCompiler.class, "instance", Context.class).invoke(null, context); try { - Field fileManagerField = JavaCompiler.class.getDeclaredField("fileManager"); - fileManagerField.setAccessible(true); + Field fileManagerField = Permit.getField(JavaCompiler.class, "fileManager"); fileManagerField.set(compiler, newFiler); } catch (Exception e) {} try { - Field writerField = JavaCompiler.class.getDeclaredField("writer"); - writerField.setAccessible(true); + Field writerField = Permit.getField(JavaCompiler.class, "writer"); ClassWriter writer = (ClassWriter) writerField.get(compiler); - Field fileManagerField = ClassWriter.class.getDeclaredField("fileManager"); - fileManagerField.setAccessible(true); + Field fileManagerField = Permit.getField(ClassWriter.class, "fileManager"); fileManagerField.set(writer, newFiler); } catch (Exception e) {} @@ -210,8 +202,7 @@ public class LombokProcessor extends AbstractProcessor { private void forceMultipleRoundsInNetBeansEditor() { try { - Field f = JavacProcessingEnvironment.class.getDeclaredField("isBackgroundCompilation"); - f.setAccessible(true); + Field f = Permit.getField(JavacProcessingEnvironment.class, "isBackgroundCompilation"); f.set(javacProcessingEnv, true); } catch (NoSuchFieldException e) { // only NetBeans has it @@ -223,14 +214,12 @@ public class LombokProcessor extends AbstractProcessor { private void disablePartialReparseInNetBeansEditor(Context context) { try { Class<?> cancelServiceClass = Class.forName("com.sun.tools.javac.util.CancelService"); - Method cancelServiceInstace = cancelServiceClass.getDeclaredMethod("instance", Context.class); + Method cancelServiceInstace = Permit.getMethod(cancelServiceClass, "instance", Context.class); Object cancelService = cancelServiceInstace.invoke(null, context); if (cancelService == null) return; - Field parserField = cancelService.getClass().getDeclaredField("parser"); - parserField.setAccessible(true); + Field parserField = Permit.getField(cancelService.getClass(), "parser"); Object parser = parserField.get(cancelService); - Field supportsReparseField = parser.getClass().getDeclaredField("supportsReparse"); - supportsReparseField.setAccessible(true); + Field supportsReparseField = Permit.getField(parser.getClass(), "supportsReparse"); supportsReparseField.set(parser, false); } catch (ClassNotFoundException e) { // only NetBeans has it @@ -284,8 +273,7 @@ public class LombokProcessor extends AbstractProcessor { private void stopJavacProcessingEnvironmentFromClosingOurClassloader() { try { - Field f = JavacProcessingEnvironment.class.getDeclaredField("processorClassLoader"); - f.setAccessible(true); + Field f = Permit.getField(JavacProcessingEnvironment.class, "processorClassLoader"); ClassLoader unwrapped = (ClassLoader) f.get(javacProcessingEnv); if (unwrapped == null) return; ClassLoader wrapped = wrapClassLoader(unwrapped); @@ -377,7 +365,7 @@ public class LombokProcessor extends AbstractProcessor { } catch (Exception e) { e.printStackTrace(); processingEnv.getMessager().printMessage(Kind.WARNING, - "Can't force a new processing round. Lombok won't work."); + "Can't force a new processing round. Lombok won't work."); } } } @@ -414,16 +402,14 @@ public class LombokProcessor extends AbstractProcessor { @Override public SourceVersion getSupportedSourceVersion() { return SourceVersion.latest(); } - + /** * This class casts the given processing environment to a JavacProcessingEnvironment. In case of * gradle incremental compilation, the delegate ProcessingEnvironment of the gradle wrapper is returned. */ public JavacProcessingEnvironment getJavacProcessingEnvironment(Object procEnv) { - if (procEnv instanceof JavacProcessingEnvironment) { - return (JavacProcessingEnvironment) procEnv; - } - + if (procEnv instanceof JavacProcessingEnvironment) return (JavacProcessingEnvironment) procEnv; + // try to find a "delegate" field in the object, and use this to try to obtain a JavacProcessingEnvironment for (Class<?> procEnvClass = procEnv.getClass(); procEnvClass != null; procEnvClass = procEnvClass.getSuperclass()) { try { @@ -432,9 +418,9 @@ public class LombokProcessor extends AbstractProcessor { // delegate field was not found, try on superclass } } - + processingEnv.getMessager().printMessage(Kind.WARNING, - "Can't get the delegate of the gradle IncrementalProcessingEnvironment. Lombok won't work."); + "Can't get the delegate of the gradle IncrementalProcessingEnvironment. Lombok won't work."); return null; } @@ -444,10 +430,8 @@ public class LombokProcessor extends AbstractProcessor { * (directly or through a delegate field again) */ public JavacFiler getJavacFiler(Object filer) { - if (filer instanceof JavacFiler) { - return (JavacFiler) filer; - } - + if (filer instanceof JavacFiler) return (JavacFiler) filer; + // try to find a "delegate" field in the object, and use this to check for a JavacFiler for (Class<?> filerClass = filer.getClass(); filerClass != null; filerClass = filerClass.getSuperclass()) { try { @@ -456,15 +440,13 @@ public class LombokProcessor extends AbstractProcessor { // delegate field was not found, try on superclass } } - + processingEnv.getMessager().printMessage(Kind.WARNING, - "Can't get a JavacFiler from " + filer.getClass().getName() + ". Lombok won't work."); + "Can't get a JavacFiler from " + filer.getClass().getName() + ". Lombok won't work."); return null; } private Object tryGetDelegateField(Class<?> delegateClass, Object instance) throws Exception { - Field field = delegateClass.getDeclaredField("delegate"); - field.setAccessible(true); - return field.get(instance); + return Permit.getField(delegateClass, "delegate").get(instance); } } diff --git a/src/core/lombok/javac/apt/Processor.java b/src/core/lombok/javac/apt/Processor.java index 7a187148..f15bf74c 100644 --- a/src/core/lombok/javac/apt/Processor.java +++ b/src/core/lombok/javac/apt/Processor.java @@ -53,6 +53,8 @@ import com.sun.tools.javac.processing.JavacFiler; import com.sun.tools.javac.processing.JavacProcessingEnvironment; import com.sun.tools.javac.util.Options; +import lombok.permit.Permit; + /** * This processor should not be used. It used to be THE processor. This class is only there to warn people that something went wrong, and for the * lombok developers to see if what the reason for those failures is. @@ -101,8 +103,7 @@ public class Processor extends AbstractProcessor { try { JavacProcessingEnvironment environment = (JavacProcessingEnvironment) procEnv; Options instance = Options.instance(environment.getContext()); - Field field = Options.class.getDeclaredField("values"); - field.setAccessible(true); + Field field = Permit.getField(Options.class, "values"); @SuppressWarnings("unchecked") Map<String, String> values = (Map<String, String>) field.get(instance); if (values.isEmpty()) { message.append("Options: empty\n\n"); @@ -125,8 +126,7 @@ public class Processor extends AbstractProcessor { private void findServices(StringBuilder message, Filer filer) { try { - Field filerFileManagerField = JavacFiler.class.getDeclaredField("fileManager"); - filerFileManagerField.setAccessible(true); + Field filerFileManagerField = Permit.getField(JavacFiler.class, "fileManager"); JavaFileManager jfm = (JavaFileManager) filerFileManagerField.get(filer); ClassLoader processorClassLoader = jfm.hasLocation(ANNOTATION_PROCESSOR_PATH) ? jfm.getClassLoader(ANNOTATION_PROCESSOR_PATH) : jfm.getClassLoader(CLASS_PATH); Enumeration<URL> resources = processorClassLoader.getResources("META-INF/services/javax.annotation.processing.Processor"); diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java index e4e40095..c97c7ede 100644 --- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java +++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java @@ -58,6 +58,7 @@ import lombok.experimental.Tolerate; import lombok.javac.Javac; import lombok.javac.JavacNode; import lombok.javac.JavacTreeMaker; +import lombok.permit.Permit; import com.sun.tools.javac.code.BoundKind; import com.sun.tools.javac.code.Flags; @@ -1049,10 +1050,9 @@ public class JavacHandlerUtil { if (TYPE != null) return; if (!in.getName().equals("com.sun.tools.javac.tree.JCTree$JCAnnotatedType")) return; try { - CONSTRUCTOR = in.getDeclaredConstructor(List.class, JCExpression.class); - CONSTRUCTOR.setAccessible(true); - ANNOTATIONS = in.getDeclaredField("annotations"); - UNDERLYING_TYPE = in.getDeclaredField("underlyingType"); + CONSTRUCTOR = Permit.getConstructor(in, List.class, JCExpression.class); + ANNOTATIONS = Permit.getField(in, "annotations"); + UNDERLYING_TYPE = Permit.getField(in, "underlyingType"); TYPE = in; } catch (Exception ignore) {} } @@ -1102,9 +1102,9 @@ public class JavacHandlerUtil { Method r = null; Method e = null; try { - f = ClassSymbol.class.getField("members_field"); - r = f.getType().getMethod("remove", Symbol.class); - e = f.getType().getMethod("enter", Symbol.class); + f = Permit.getField(ClassSymbol.class, "members_field"); + r = Permit.getMethod(f.getType(), "remove", Symbol.class); + e = Permit.getMethod(f.getType(), "enter", Symbol.class); } catch (Exception ex) {} membersField = f; removeMethod = r; |