From b867f81b8a251a8a32e42f53c2be34d520938bd7 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Tue, 13 Dec 2016 00:52:32 +0100 Subject: Lombok will now also fix the typemirror info when generating bean-related methods/constructors, to allow other annotation processors to see these generated methods/constructors too. --- src/core/lombok/javac/apt/LombokProcessor.java | 49 ++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 3 deletions(-) (limited to 'src/core/lombok/javac/apt') diff --git a/src/core/lombok/javac/apt/LombokProcessor.java b/src/core/lombok/javac/apt/LombokProcessor.java index 6547c143..6e229279 100644 --- a/src/core/lombok/javac/apt/LombokProcessor.java +++ b/src/core/lombok/javac/apt/LombokProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2015 The Project Lombok Authors. + * Copyright (C) 2009-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 @@ -67,7 +67,6 @@ import com.sun.tools.javac.util.Context; */ @SupportedAnnotationTypes("*") public class LombokProcessor extends AbstractProcessor { - private JavacProcessingEnvironment processingEnv; private JavacTransformer transformer; private Trees trees; @@ -82,9 +81,14 @@ public class LombokProcessor extends AbstractProcessor { } this.processingEnv = (JavacProcessingEnvironment) procEnv; + String beforeOurs = listAnnotationProcessorsBeforeOurs(); + if (beforeOurs != null) { + procEnv.getMessager().printMessage(Kind.NOTE, "Lombok is not the first annotation processor in the lineup. Configure your build tool with an explicit list of processors so that lombok is first. See https://projectlombok.org/configureMultipleProcessors for more. Processors before lombok in the lineup: " + beforeOurs); + } + placePostCompileAndDontMakeForceRoundDummiesHook(); - transformer = new JavacTransformer(procEnv.getMessager()); trees = Trees.instance(procEnv); + transformer = new JavacTransformer(procEnv.getMessager(), trees); SortedSet p = transformer.getPriorities(); if (p.isEmpty()) { this.priorityLevels = new long[] {0L}; @@ -97,6 +101,45 @@ public class LombokProcessor extends AbstractProcessor { } } + private static final String JPE = "com.sun.tools.javac.processing.JavacProcessingEnvironment"; + private static final Field javacProcessingEnvironment_discoveredProcs = getFieldAccessor(JPE, "discoveredProcs"); + private static final Field discoveredProcessors_procStateList = getFieldAccessor(JPE + "$DiscoveredProcessors", "procStateList"); + private static final Field processorState_processor = getFieldAccessor(JPE + "$processor", "processor"); + + 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; + } catch (ClassNotFoundException e) { + return null; + } catch (NoSuchFieldException e) { + return null; + } + } + + private String listAnnotationProcessorsBeforeOurs() { + try { + Object discoveredProcessors = javacProcessingEnvironment_discoveredProcs.get(this.processingEnv); + ArrayList states = (ArrayList) discoveredProcessors_procStateList.get(discoveredProcessors); + if (states == null || states.isEmpty()) return null; + if (states.size() == 1) return processorState_processor.get(states.get(0)).getClass().getName(); + + int idx = 0; + StringBuilder out = new StringBuilder(); + for (Object processState : states) { + idx++; + String name = processorState_processor.get(processState).getClass().getName(); + if (out.length() > 0) out.append(", "); + out.append("[").append(idx).append("] ").append(name); + } + return out.toString(); + } catch (Exception e) { + return null; + } + } + private void placePostCompileAndDontMakeForceRoundDummiesHook() { stopJavacProcessingEnvironmentFromClosingOurClassloader(); -- cgit