aboutsummaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorRoel Spilker <r.spilker@gmail.com>2014-03-17 21:56:49 +0100
committerRoel Spilker <r.spilker@gmail.com>2014-03-17 21:56:49 +0100
commit17972d59fa7e2eec6b73ba5da8234f5fa7ac2536 (patch)
treefbcc5c272480e5915b60ac0992639c083d2507d8 /src/core
parent362000933f46ad1d509a659e39279298440a97ec (diff)
downloadlombok-17972d59fa7e2eec6b73ba5da8234f5fa7ac2536.tar.gz
lombok-17972d59fa7e2eec6b73ba5da8234f5fa7ac2536.tar.bz2
lombok-17972d59fa7e2eec6b73ba5da8234f5fa7ac2536.zip
[#590][refactor] Fix for deadlock in WeakHashMap. Refactored all code that used a
WeakHashMap to fake a field.
Diffstat (limited to 'src/core')
-rw-r--r--src/core/lombok/core/AnnotationProcessor.java10
-rw-r--r--src/core/lombok/core/debug/DebugSnapshotStore.java3
-rw-r--r--src/core/lombok/eclipse/HandlerLibrary.java15
-rw-r--r--src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java42
-rw-r--r--src/core/lombok/javac/HandlerLibrary.java13
-rw-r--r--src/core/lombok/javac/handlers/JavacHandlerUtil.java16
6 files changed, 29 insertions, 70 deletions
diff --git a/src/core/lombok/core/AnnotationProcessor.java b/src/core/lombok/core/AnnotationProcessor.java
index 734150cd..ecce1849 100644
--- a/src/core/lombok/core/AnnotationProcessor.java
+++ b/src/core/lombok/core/AnnotationProcessor.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2009-2011 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
@@ -29,9 +29,7 @@ import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
-import java.util.Map;
import java.util.Set;
-import java.util.WeakHashMap;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.ProcessingEnvironment;
@@ -63,8 +61,8 @@ public class AnnotationProcessor extends AbstractProcessor {
private final List<ProcessorDescriptor> active = new ArrayList<ProcessorDescriptor>();
private final List<String> delayedWarnings = new ArrayList<String>();
- private static final Map<ClassLoader, Boolean> lombokAlreadyAddedTo = new WeakHashMap<ClassLoader, Boolean>();
-
+ private static final BooleanFieldAugment<ClassLoader> lombokAlreadyAddedTo = BooleanFieldAugment.augment(ClassLoader.class, "lombok$alreadyAddedTo");
+
static class JavacDescriptor extends ProcessorDescriptor {
private Processor processor;
@@ -100,7 +98,7 @@ public class AnnotationProcessor extends AbstractProcessor {
private ClassLoader findAndPatchClassLoader(ProcessingEnvironment procEnv) throws Exception {
ClassLoader environmentClassLoader = procEnv.getClass().getClassLoader();
if (environmentClassLoader != null && environmentClassLoader.getClass().getCanonicalName().equals("org.codehaus.plexus.compiler.javac.IsolatedClassLoader")) {
- if (lombokAlreadyAddedTo.put(environmentClassLoader, true) == null) {
+ if (!lombokAlreadyAddedTo.set(environmentClassLoader)) {
Method m = environmentClassLoader.getClass().getDeclaredMethod("addURL", URL.class);
URL selfUrl = new File(ClassRootFinder.findClassRootOfClass(AnnotationProcessor.class)).toURI().toURL();
m.invoke(environmentClassLoader, selfUrl);
diff --git a/src/core/lombok/core/debug/DebugSnapshotStore.java b/src/core/lombok/core/debug/DebugSnapshotStore.java
index 19f4d5b1..daaf9509 100644
--- a/src/core/lombok/core/debug/DebugSnapshotStore.java
+++ b/src/core/lombok/core/debug/DebugSnapshotStore.java
@@ -35,8 +35,9 @@ import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
public class DebugSnapshotStore {
public static final DebugSnapshotStore INSTANCE = new DebugSnapshotStore();
- public static boolean GLOBAL_DSS_DISABLE_SWITCH = true;
+ public static final boolean GLOBAL_DSS_DISABLE_SWITCH = true;
+ // All access should synchronize on the map
private final Map<CompilationUnitDeclaration, List<DebugSnapshot>> map =
new WeakHashMap<CompilationUnitDeclaration, List<DebugSnapshot>>();
diff --git a/src/core/lombok/eclipse/HandlerLibrary.java b/src/core/lombok/eclipse/HandlerLibrary.java
index 242e923c..15a98b85 100644
--- a/src/core/lombok/eclipse/HandlerLibrary.java
+++ b/src/core/lombok/eclipse/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
@@ -33,11 +33,11 @@ import java.util.HashMap;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
-import java.util.WeakHashMap;
import lombok.Lombok;
import lombok.core.AnnotationValues;
import lombok.core.AnnotationValues.AnnotationValueDecodeFail;
+import lombok.core.BooleanFieldAugment;
import lombok.core.HandlerPriority;
import lombok.core.SpiLoadUtil;
import lombok.core.TypeLibrary;
@@ -185,19 +185,14 @@ public class HandlerLibrary {
}
}
- private static final Map<ASTNode, Object> handledMap = new WeakHashMap<ASTNode, Object>();
- private static final Object MARKER = new Object();
+ private static final BooleanFieldAugment<ASTNode> handled = BooleanFieldAugment.augment(ASTNode.class, "lombok$handled");
private boolean checkAndSetHandled(ASTNode node) {
- synchronized (handledMap) {
- return handledMap.put(node, MARKER) != MARKER;
- }
+ return !handled.set(node);
}
private boolean needsHandling(ASTNode node) {
- synchronized (handledMap) {
- return handledMap.get(node) != MARKER;
- }
+ return !handled.get(node);
}
/**
diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
index fbad53d4..c4115652 100644
--- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
+++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
@@ -25,7 +25,6 @@ import static lombok.eclipse.Eclipse.*;
import static lombok.core.TransformationsUtil.*;
import java.lang.reflect.Constructor;
-import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
@@ -35,7 +34,6 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import java.util.WeakHashMap;
import lombok.AccessLevel;
import lombok.Data;
@@ -44,6 +42,8 @@ import lombok.Lombok;
import lombok.core.AST.Kind;
import lombok.core.AnnotationValues;
import lombok.core.AnnotationValues.AnnotationValue;
+import lombok.core.BooleanFieldAugment;
+import lombok.core.ReferenceFieldAugment;
import lombok.core.TransformationsUtil;
import lombok.core.TypeResolver;
import lombok.eclipse.EclipseAST;
@@ -207,27 +207,10 @@ public class EclipseHandlerUtil {
}
}
- private static Field generatedByField;
-
- static {
- try {
- generatedByField = ASTNode.class.getDeclaredField("$generatedBy");
- } catch (Throwable t) {
- //ignore - no $generatedBy exists when running in ecj.
- }
- }
-
- private static Map<ASTNode, ASTNode> generatedNodes = new WeakHashMap<ASTNode, ASTNode>();
+ private static ReferenceFieldAugment<ASTNode, ASTNode> generatedNodes = ReferenceFieldAugment.augment(ASTNode.class, ASTNode.class, "$generatedBy");
public static ASTNode getGeneratedBy(ASTNode node) {
- if (generatedByField != null) {
- try {
- return (ASTNode) generatedByField.get(node);
- } catch (Exception e) {}
- }
- synchronized (generatedNodes) {
- return generatedNodes.get(node);
- }
+ return generatedNodes.get(node);
}
public static boolean isGenerated(ASTNode node) {
@@ -235,15 +218,7 @@ public class EclipseHandlerUtil {
}
public static ASTNode setGeneratedBy(ASTNode node, ASTNode source) {
- if (generatedByField != null) {
- try {
- generatedByField.set(node, source);
- return node;
- } catch (Exception e) {}
- }
- synchronized (generatedNodes) {
- generatedNodes.put(node, source);
- }
+ generatedNodes.set(node, source);
return node;
}
@@ -865,12 +840,11 @@ public class EclipseHandlerUtil {
}
}
- private static final Map<FieldDeclaration, Object> generatedLazyGettersWithPrimitiveBoolean = new WeakHashMap<FieldDeclaration, Object>();
- private static final Object MARKER = new Object();
+ private static final BooleanFieldAugment<FieldDeclaration> generatedLazyGettersWithPrimitiveBoolean = BooleanFieldAugment.augment(FieldDeclaration.class, "lombok$booleanLazyGetter");
static void registerCreatedLazyGetter(FieldDeclaration field, char[] methodName, TypeReference returnType) {
if (isBoolean(returnType)) {
- generatedLazyGettersWithPrimitiveBoolean.put(field, MARKER);
+ generatedLazyGettersWithPrimitiveBoolean.set(field);
}
}
@@ -880,7 +854,7 @@ public class EclipseHandlerUtil {
private static GetterMethod findGetter(EclipseNode field) {
FieldDeclaration fieldDeclaration = (FieldDeclaration) field.get();
- boolean forceBool = generatedLazyGettersWithPrimitiveBoolean.containsKey(fieldDeclaration);
+ boolean forceBool = generatedLazyGettersWithPrimitiveBoolean.get(fieldDeclaration);
TypeReference fieldType = fieldDeclaration.type;
boolean isBoolean = forceBool || isBoolean(fieldType);
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<JCTree, Object> handledMap = new WeakHashMap<JCTree, Object>();
- private static final Object MARKER = new Object();
+ private static final BooleanFieldAugment<JCTree> 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<JCTree, WeakReference<JCTree>> generatedNodes = new WeakHashMap<JCTree, WeakReference<JCTree>>();
+ private static ReferenceFieldAugment<JCTree, JCTree> 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<JCTree> 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 extends JCTree> 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<JCTree>(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;
}