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/eclipse/HandlerLibrary.java | 15 +++----- .../eclipse/handlers/EclipseHandlerUtil.java | 42 +++++----------------- 2 files changed, 13 insertions(+), 44 deletions(-) (limited to 'src/core/lombok/eclipse') 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 handledMap = new WeakHashMap(); - private static final Object MARKER = new Object(); + private static final BooleanFieldAugment 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 generatedNodes = new WeakHashMap(); + private static ReferenceFieldAugment 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 generatedLazyGettersWithPrimitiveBoolean = new WeakHashMap(); - private static final Object MARKER = new Object(); + private static final BooleanFieldAugment 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); -- cgit