diff options
Diffstat (limited to 'src/core/lombok')
-rw-r--r-- | src/core/lombok/core/Agent.java | 62 | ||||
-rw-r--r-- | src/core/lombok/javac/HandlerLibrary.java | 49 | ||||
-rw-r--r-- | src/core/lombok/javac/JavacAST.java | 1 |
3 files changed, 82 insertions, 30 deletions
diff --git a/src/core/lombok/core/Agent.java b/src/core/lombok/core/Agent.java new file mode 100644 index 00000000..2ba6bb20 --- /dev/null +++ b/src/core/lombok/core/Agent.java @@ -0,0 +1,62 @@ +/* + * Copyright © 2009 Reinier Zwitserloot and Roel Spilker. + * + * 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.core; + +import java.lang.instrument.Instrumentation; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +public abstract class Agent { + protected abstract void runAgent(String agentArgs, Instrumentation instrumentation, boolean injected) throws Exception; + + public static void agentmain(String agentArgs, Instrumentation instrumentation) throws Exception { + runAgents(agentArgs, instrumentation, true); + } + + public static void premain(String agentArgs, Instrumentation instrumentation) throws Exception { + runAgents(agentArgs, instrumentation, false); + } + + private static final List<String> AGENT_NAMES = Collections.unmodifiableList(Arrays.asList( + "lombok.netbeans.agent.NetbeansPatcher", + "lombok.eclipse.agent.EclipsePatcher" + )); + + private static void runAgents(String agentArgs, Instrumentation instrumentation, boolean injected) throws Exception { + for (String agentName : AGENT_NAMES) { + try { + Class<?> agentClass = Class.forName(agentName); + Agent agent = (Agent) agentClass.newInstance(); + agent.runAgent(agentArgs, instrumentation, injected); + } catch (ClassNotFoundException e) { + //That's okay - this lombok evidently is a version with support for something stripped out. + } catch (ClassCastException e) { + throw new InternalError("Lombok bug. Class: " + agentName + " is not an implementation of lombok.core.Agent"); + } catch (IllegalAccessException e) { + throw new InternalError("Lombok bug. Class: " + agentName + " is not public"); + } catch (InstantiationException e) { + throw new InternalError("Lombok bug. Class: " + agentName + " is not concrete or has no public no-args constructor"); + } + } + } +} diff --git a/src/core/lombok/javac/HandlerLibrary.java b/src/core/lombok/javac/HandlerLibrary.java index bbe9dec0..48218513 100644 --- a/src/core/lombok/javac/HandlerLibrary.java +++ b/src/core/lombok/javac/HandlerLibrary.java @@ -21,14 +21,12 @@ */ package lombok.javac; +import java.io.IOException; import java.lang.annotation.Annotation; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; -import java.util.Iterator; import java.util.Map; -import java.util.ServiceConfigurationError; -import java.util.ServiceLoader; import javax.annotation.processing.Messager; import javax.tools.Diagnostic; @@ -85,46 +83,36 @@ public class HandlerLibrary { public static HandlerLibrary load(Messager messager) { HandlerLibrary library = new HandlerLibrary(messager); - loadAnnotationHandlers(library); - loadVisitorHandlers(library); + try { + loadAnnotationHandlers(library); + loadVisitorHandlers(library); + } catch (IOException e) { + System.err.println("Lombok isn't running due to misconfigured SPI files: " + e); + } return library; } /** Uses SPI Discovery to find implementations of {@link JavacAnnotationHandler}. */ @SuppressWarnings("unchecked") - private static void loadAnnotationHandlers(HandlerLibrary lib) { + private static void loadAnnotationHandlers(HandlerLibrary lib) throws IOException { //No, that seemingly superfluous reference to JavacAnnotationHandler's classloader is not in fact superfluous! - Iterator<JavacAnnotationHandler> it = ServiceLoader.load(JavacAnnotationHandler.class, - JavacAnnotationHandler.class.getClassLoader()).iterator(); - while (it.hasNext()) { - try { - JavacAnnotationHandler<?> handler = it.next(); - Class<? extends Annotation> annotationClass = - SpiLoadUtil.findAnnotationClass(handler.getClass(), JavacAnnotationHandler.class); - AnnotationHandlerContainer<?> container = new AnnotationHandlerContainer(handler, annotationClass); - if (lib.annotationHandlers.put(container.annotationClass.getName(), container) != null) { - lib.javacWarning("Duplicate handlers for annotation type: " + container.annotationClass.getName()); - } - lib.typeLibrary.addType(container.annotationClass.getName()); - } catch (ServiceConfigurationError e) { - lib.javacWarning("Can't load Lombok annotation handler for javac", e); + for (JavacAnnotationHandler handler : SpiLoadUtil.findServices(JavacAnnotationHandler.class, JavacAnnotationHandler.class.getClassLoader())) { + Class<? extends Annotation> annotationClass = + SpiLoadUtil.findAnnotationClass(handler.getClass(), JavacAnnotationHandler.class); + AnnotationHandlerContainer<?> container = new AnnotationHandlerContainer(handler, annotationClass); + if (lib.annotationHandlers.put(container.annotationClass.getName(), container) != null) { + lib.javacWarning("Duplicate handlers for annotation type: " + container.annotationClass.getName()); } + lib.typeLibrary.addType(container.annotationClass.getName()); } } /** Uses SPI Discovery to find implementations of {@link JavacASTVisitor}. */ - private static void loadVisitorHandlers(HandlerLibrary lib) { + private static void loadVisitorHandlers(HandlerLibrary lib) throws IOException { //No, that seemingly superfluous reference to JavacASTVisitor's classloader is not in fact superfluous! - Iterator<JavacASTVisitor> it = ServiceLoader.load(JavacASTVisitor.class, - JavacASTVisitor.class.getClassLoader()).iterator(); - while (it.hasNext()) { - try { - JavacASTVisitor handler = it.next(); - lib.visitorHandlers.add(handler); - } catch (ServiceConfigurationError e) { - lib.javacWarning("Can't load Lombok visitor handler for javac", e); - } + for (JavacASTVisitor visitor : SpiLoadUtil.findServices(JavacASTVisitor.class, JavacASTVisitor.class.getClassLoader())) { + lib.visitorHandlers.add(visitor); } } @@ -177,6 +165,7 @@ public class HandlerLibrary { if (container == null) continue; try { + System.out.println("Calling handle on: "+ container.handler.getClass().getName()); handled |= container.handle(node); } catch (AnnotationValueDecodeFail fail) { fail.owner.setError(fail.getMessage(), fail.idx); diff --git a/src/core/lombok/javac/JavacAST.java b/src/core/lombok/javac/JavacAST.java index 33c167f9..9cfe7f34 100644 --- a/src/core/lombok/javac/JavacAST.java +++ b/src/core/lombok/javac/JavacAST.java @@ -125,6 +125,7 @@ public class JavacAST extends AST<JavacAST, JavacNode, JCTree> { /** @return A TreeMaker instance that you can use to create new AST nodes. */ public TreeMaker getTreeMaker() { + treeMaker.at(-1); return treeMaker; } |