From 17972d59fa7e2eec6b73ba5da8234f5fa7ac2536 Mon Sep 17 00:00:00 2001 From: Roel Spilker Date: Mon, 17 Mar 2014 21:56:49 +0100 Subject: [#590][refactor] Fix for deadlock in WeakHashMap. Refactored all code that used a WeakHashMap to fake a field. --- src/core/lombok/javac/HandlerLibrary.java | 13 +++++-------- src/core/lombok/javac/handlers/JavacHandlerUtil.java | 16 +++++----------- 2 files changed, 10 insertions(+), 19 deletions(-) (limited to 'src/core/lombok/javac') diff --git a/src/core/lombok/javac/HandlerLibrary.java b/src/core/lombok/javac/HandlerLibrary.java index 4306b5f2..0cab56f9 100644 --- a/src/core/lombok/javac/HandlerLibrary.java +++ b/src/core/lombok/javac/HandlerLibrary.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2013 The Project Lombok Authors. + * Copyright (C) 2009-2014 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 @@ -30,16 +30,16 @@ import java.util.HashMap; import java.util.Map; import java.util.SortedSet; import java.util.TreeSet; -import java.util.WeakHashMap; import javax.annotation.processing.Messager; import javax.tools.Diagnostic; +import lombok.core.AnnotationValues.AnnotationValueDecodeFail; +import lombok.core.BooleanFieldAugment; import lombok.core.HandlerPriority; import lombok.core.SpiLoadUtil; import lombok.core.TypeLibrary; import lombok.core.TypeResolver; -import lombok.core.AnnotationValues.AnnotationValueDecodeFail; import lombok.javac.handlers.JavacHandlerUtil; import com.sun.tools.javac.tree.JCTree; @@ -204,13 +204,10 @@ public class HandlerLibrary { if (t != null) t.printStackTrace(); } - private static final Map handledMap = new WeakHashMap(); - private static final Object MARKER = new Object(); + private static final BooleanFieldAugment handled = BooleanFieldAugment.augment(JCTree.class, "lombok$handled"); private boolean checkAndSetHandled(JCTree node) { - synchronized (handledMap) { - return handledMap.put(node, MARKER) != MARKER; - } + return !handled.set(node); } /** diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java index bc4dda8e..de73b3be 100644 --- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java +++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java @@ -25,13 +25,11 @@ import static lombok.core.TransformationsUtil.INVALID_ON_BUILDERS; import static lombok.javac.Javac.*; import java.lang.annotation.Annotation; -import java.lang.ref.WeakReference; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; -import java.util.WeakHashMap; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -41,6 +39,7 @@ import lombok.Getter; import lombok.core.AST.Kind; import lombok.core.AnnotationValues; import lombok.core.AnnotationValues.AnnotationValue; +import lombok.core.ReferenceFieldAugment; import lombok.core.TransformationsUtil; import lombok.core.TypeResolver; import lombok.delombok.LombokOptionsFactory; @@ -109,7 +108,7 @@ public class JavacHandlerUtil { } } - private static Map> generatedNodes = new WeakHashMap>(); + private static ReferenceFieldAugment generatedNodes = ReferenceFieldAugment.augmentWeakField(JCTree.class, JCTree.class, "lombok$generatedNodes"); /** * Contributed by Jan Lahoda; many lombok transformations should not be run (or a lite version should be run) when the netbeans editor @@ -126,10 +125,7 @@ public class JavacHandlerUtil { } public static JCTree getGeneratedBy(JCTree node) { - synchronized (generatedNodes) { - WeakReference ref = generatedNodes.get(node); - return ref == null ? null : ref.get(); - } + return generatedNodes.get(node); } public static boolean isGenerated(JCTree node) { @@ -145,10 +141,8 @@ public class JavacHandlerUtil { public static T setGeneratedBy(T node, JCTree source, Context context) { if (node == null) return null; - synchronized (generatedNodes) { - if (source == null) generatedNodes.remove(node); - else generatedNodes.put(node, new WeakReference(source)); - } + if (source == null) generatedNodes.clear(node); + else generatedNodes.set(node, source); if (source != null && (!inNetbeansEditor(context) || (node instanceof JCVariableDecl && (((JCVariableDecl) node).mods.flags & Flags.PARAMETER) != 0))) node.pos = source.pos; return node; } -- cgit