aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorReinier Zwitserloot <r.zwitserloot@projectlombok.org>2020-02-14 01:20:14 +0100
committerReinier Zwitserloot <r.zwitserloot@projectlombok.org>2020-02-14 01:21:24 +0100
commit89f98da78d3ffd9e9f6f7151fcaf5e4329d2e8dd (patch)
tree7e77630855e4a66851f4c3972a54263f024e58a7 /src
parent15b09ee27466baa9107ce6556e9302191f1cd7b5 (diff)
downloadlombok-89f98da78d3ffd9e9f6f7151fcaf5e4329d2e8dd.tar.gz
lombok-89f98da78d3ffd9e9f6f7151fcaf5e4329d2e8dd.tar.bz2
lombok-89f98da78d3ffd9e9f6f7151fcaf5e4329d2e8dd.zip
[fixes #678] `@Synchronize` an instance method on static variable no longer emits a warning.
Diffstat (limited to 'src')
-rw-r--r--src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java8
-rw-r--r--src/core/lombok/eclipse/handlers/HandleSynchronized.java61
-rw-r--r--src/core/lombok/javac/handlers/HandleSynchronized.java44
3 files changed, 83 insertions, 30 deletions
diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
index 25999e85..252fd998 100644
--- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
+++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
@@ -992,7 +992,7 @@ public class EclipseHandlerUtil {
* Given for example {@code class Outer { class Inner {} }} this would generate {@code char[][] { "Outer", "Inner" }}.
* For method local and top level types, this generates a size-1 char[][] where the only char[] element is {@code name} itself.
*/
- private static char[][] getQualifiedInnerName(EclipseNode parent, char[] name) {
+ public static char[][] getQualifiedInnerName(EclipseNode parent, char[] name) {
int count = 0;
EclipseNode n = parent;
@@ -1692,12 +1692,14 @@ public class EclipseHandlerUtil {
*/
public static MemberExistsResult fieldExists(String fieldName, EclipseNode node) {
node = upToTypeNode(node);
+ char[] fieldNameChars = null;
if (node != null && node.get() instanceof TypeDeclaration) {
- TypeDeclaration typeDecl = (TypeDeclaration)node.get();
+ TypeDeclaration typeDecl = (TypeDeclaration) node.get();
if (typeDecl.fields != null) for (FieldDeclaration def : typeDecl.fields) {
char[] fName = def.name;
if (fName == null) continue;
- if (fieldName.equals(new String(fName))) {
+ if (fieldNameChars == null) fieldNameChars = fieldName.toCharArray();
+ if (Arrays.equals(fName, fieldNameChars)) {
return getGeneratedBy(def) == null ? MemberExistsResult.EXISTS_BY_USER : MemberExistsResult.EXISTS_BY_LOMBOK;
}
}
diff --git a/src/core/lombok/eclipse/handlers/HandleSynchronized.java b/src/core/lombok/eclipse/handlers/HandleSynchronized.java
index 22f7f967..08d00d91 100644
--- a/src/core/lombok/eclipse/handlers/HandleSynchronized.java
+++ b/src/core/lombok/eclipse/handlers/HandleSynchronized.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2009-2014 The Project Lombok Authors.
+ * Copyright (C) 2009-2020 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
@@ -25,6 +25,7 @@ import static lombok.core.handlers.HandlerUtil.*;
import static lombok.eclipse.handlers.EclipseHandlerUtil.*;
import java.lang.reflect.Modifier;
+import java.util.Arrays;
import lombok.ConfigurationKeys;
import lombok.Synchronized;
@@ -34,6 +35,7 @@ import lombok.core.AST.Kind;
import lombok.eclipse.DeferUntilPostDiet;
import lombok.eclipse.EclipseAnnotationHandler;
import lombok.eclipse.EclipseNode;
+import lombok.eclipse.handlers.EclipseHandlerUtil.MemberExistsResult;
import org.eclipse.jdt.internal.compiler.ast.Annotation;
import org.eclipse.jdt.internal.compiler.ast.ArrayAllocationExpression;
@@ -47,6 +49,7 @@ import org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference;
import org.eclipse.jdt.internal.compiler.ast.Statement;
import org.eclipse.jdt.internal.compiler.ast.SynchronizedStatement;
import org.eclipse.jdt.internal.compiler.ast.ThisReference;
+import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
import org.eclipse.jdt.internal.compiler.lookup.TypeConstants;
import org.mangosdk.spi.ProviderFor;
@@ -63,31 +66,52 @@ public class HandleSynchronized extends EclipseAnnotationHandler<Synchronized> {
@Override public void preHandle(AnnotationValues<Synchronized> annotation, Annotation source, EclipseNode annotationNode) {
EclipseNode methodNode = annotationNode.up();
if (methodNode == null || methodNode.getKind() != Kind.METHOD || !(methodNode.get() instanceof MethodDeclaration)) return;
- MethodDeclaration method = (MethodDeclaration)methodNode.get();
+ MethodDeclaration method = (MethodDeclaration) methodNode.get();
if (method.isAbstract()) return;
- createLockField(annotation, annotationNode, method.isStatic(), false);
+ createLockField(annotation, annotationNode, new boolean[] {method.isStatic()}, false);
}
- public char[] createLockField(AnnotationValues<Synchronized> annotation, EclipseNode annotationNode, boolean isStatic, boolean reportErrors) {
+ public char[] createLockField(AnnotationValues<Synchronized> annotation, EclipseNode annotationNode, boolean[] isStatic, boolean reportErrors) {
char[] lockName = annotation.getInstance().value().toCharArray();
Annotation source = (Annotation) annotationNode.get();
boolean autoMake = false;
if (lockName.length == 0) {
autoMake = true;
- lockName = isStatic ? STATIC_LOCK_NAME : INSTANCE_LOCK_NAME;
+ lockName = isStatic[0] ? STATIC_LOCK_NAME : INSTANCE_LOCK_NAME;
}
- if (fieldExists(new String(lockName), annotationNode) == MemberExistsResult.NOT_EXISTS) {
+ EclipseNode typeNode = upToTypeNode(annotationNode);
+ MemberExistsResult exists = MemberExistsResult.NOT_EXISTS;
+
+ if (typeNode != null && typeNode.get() instanceof TypeDeclaration) {
+ TypeDeclaration typeDecl = (TypeDeclaration) typeNode.get();
+ if (typeDecl.fields != null) for (FieldDeclaration def : typeDecl.fields) {
+ char[] fName = def.name;
+ if (fName == null) continue;
+ if (Arrays.equals(fName, lockName)) {
+ exists = getGeneratedBy(def) == null ? MemberExistsResult.EXISTS_BY_USER : MemberExistsResult.EXISTS_BY_LOMBOK;
+ boolean st = def.isStatic();
+ if (!st && isStatic[0]) {
+ if (reportErrors) annotationNode.addError(String.format("The field %s is non-static and thus cannot be used on this static method", new String(lockName)));
+ return null;
+ }
+ isStatic[0] = st;
+ break;
+ }
+ }
+ }
+
+ if (exists == MemberExistsResult.NOT_EXISTS) {
if (!autoMake) {
- if (reportErrors) annotationNode.addError(String.format("The field %s does not exist.", new String(lockName)));
+ if (reportErrors) annotationNode.addError(String.format("The field %s does not exist", new String(lockName)));
return null;
}
FieldDeclaration fieldDecl = new FieldDeclaration(lockName, 0, -1);
setGeneratedBy(fieldDecl, source);
fieldDecl.declarationSourceEnd = -1;
- fieldDecl.modifiers = (isStatic ? Modifier.STATIC : 0) | Modifier.FINAL | Modifier.PRIVATE;
+ fieldDecl.modifiers = (isStatic[0] ? Modifier.STATIC : 0) | Modifier.FINAL | Modifier.PRIVATE;
//We use 'new Object[0];' because unlike 'new Object();', empty arrays *ARE* serializable!
ArrayAllocationExpression arrayAlloc = new ArrayAllocationExpression();
@@ -111,20 +135,21 @@ public class HandleSynchronized extends EclipseAnnotationHandler<Synchronized> {
int p1 = source.sourceStart -1;
int p2 = source.sourceStart -2;
- long pos = (((long)p1) << 32) | p2;
+ long pos = (((long) p1) << 32) | p2;
EclipseNode methodNode = annotationNode.up();
if (methodNode == null || methodNode.getKind() != Kind.METHOD || !(methodNode.get() instanceof MethodDeclaration)) {
annotationNode.addError("@Synchronized is legal only on methods.");
return;
}
- MethodDeclaration method = (MethodDeclaration)methodNode.get();
+ MethodDeclaration method = (MethodDeclaration) methodNode.get();
if (method.isAbstract()) {
annotationNode.addError("@Synchronized is legal only on concrete methods.");
return;
}
- char[] lockName = createLockField(annotation, annotationNode, method.isStatic(), true);
+ boolean[] isStatic = { method.isStatic() };
+ char[] lockName = createLockField(annotation, annotationNode, isStatic, true);
if (lockName == null) return;
if (method.statements == null) return;
@@ -137,18 +162,22 @@ public class HandleSynchronized extends EclipseAnnotationHandler<Synchronized> {
block.sourceStart = method.bodyStart;
Expression lockVariable;
- if (method.isStatic()) lockVariable = new QualifiedNameReference(new char[][] {
- methodNode.up().getName().toCharArray(), lockName }, new long[] { pos, pos }, p1, p2);
- else {
+ if (isStatic[0]) {
+ EclipseNode typeNode = upToTypeNode(annotationNode);
+ char[][] n = getQualifiedInnerName(typeNode, lockName);
+ long[] ps = new long[n.length];
+ Arrays.fill(ps, pos);
+ lockVariable = new QualifiedNameReference(n, ps, p1, p2);
+ } else {
lockVariable = new FieldReference(lockName, pos);
ThisReference thisReference = new ThisReference(p1, p2);
setGeneratedBy(thisReference, source);
- ((FieldReference)lockVariable).receiver = thisReference;
+ ((FieldReference) lockVariable).receiver = thisReference;
}
setGeneratedBy(lockVariable, source);
method.statements = new Statement[] {
- new SynchronizedStatement(lockVariable, block, 0, 0)
+ new SynchronizedStatement(lockVariable, block, 0, 0)
};
// Positions for in-method generated nodes are special
diff --git a/src/core/lombok/javac/handlers/HandleSynchronized.java b/src/core/lombok/javac/handlers/HandleSynchronized.java
index 20e85d7e..b6f1e47f 100644
--- a/src/core/lombok/javac/handlers/HandleSynchronized.java
+++ b/src/core/lombok/javac/handlers/HandleSynchronized.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2009-2014 The Project Lombok Authors.
+ * Copyright (C) 2009-2020 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
@@ -37,11 +37,14 @@ import lombok.javac.handlers.JavacHandlerUtil.MemberExistsResult;
import org.mangosdk.spi.ProviderFor;
import com.sun.tools.javac.code.Flags;
+import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCAnnotation;
+import com.sun.tools.javac.tree.JCTree.JCClassDecl;
import com.sun.tools.javac.tree.JCTree.JCExpression;
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
import com.sun.tools.javac.tree.JCTree.JCNewArray;
import com.sun.tools.javac.tree.JCTree.JCStatement;
+import com.sun.tools.javac.tree.JCTree.JCTypeParameter;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.List;
@@ -65,7 +68,6 @@ public class HandleSynchronized extends JavacAnnotationHandler<Synchronized> {
if (methodNode == null || methodNode.getKind() != Kind.METHOD || !(methodNode.get() instanceof JCMethodDecl)) {
annotationNode.addError("@Synchronized is legal only on methods.");
-
return;
}
@@ -73,21 +75,41 @@ public class HandleSynchronized extends JavacAnnotationHandler<Synchronized> {
if ((method.mods.flags & Flags.ABSTRACT) != 0) {
annotationNode.addError("@Synchronized is legal only on concrete methods.");
-
return;
}
- boolean isStatic = (method.mods.flags & Flags.STATIC) != 0;
+
+ boolean[] isStatic = new boolean[] {(method.mods.flags & Flags.STATIC) != 0};
String lockName = annotation.getInstance().value();
boolean autoMake = false;
if (lockName.length() == 0) {
autoMake = true;
- lockName = isStatic ? STATIC_LOCK_NAME : INSTANCE_LOCK_NAME;
+ lockName = isStatic[0] ? STATIC_LOCK_NAME : INSTANCE_LOCK_NAME;
}
JavacTreeMaker maker = methodNode.getTreeMaker().at(ast.pos);
Context context = methodNode.getContext();
- if (fieldExists(lockName, methodNode) == MemberExistsResult.NOT_EXISTS) {
+ JavacNode typeNode = upToTypeNode(annotationNode);
+
+ MemberExistsResult exists = MemberExistsResult.NOT_EXISTS;
+
+ if (typeNode != null && typeNode.get() instanceof JCClassDecl) {
+ for (JCTree def : ((JCClassDecl) typeNode.get()).defs) {
+ if (def instanceof JCVariableDecl) {
+ if (((JCVariableDecl) def).name.contentEquals(lockName)) {
+ exists = getGeneratedBy(def) == null ? MemberExistsResult.EXISTS_BY_USER : MemberExistsResult.EXISTS_BY_LOMBOK;
+ boolean st = ((((JCVariableDecl) def).mods.flags) & Flags.STATIC) != 0;
+ if (isStatic[0] && !st) {
+ annotationNode.addError("The field " + lockName + " is non-static and this cannot be used on this static method");
+ return;
+ }
+ isStatic[0] = st;
+ }
+ }
+ }
+ }
+
+ if (exists == MemberExistsResult.NOT_EXISTS) {
if (!autoMake) {
annotationNode.addError("The field " + lockName + " does not exist.");
return;
@@ -95,18 +117,18 @@ public class HandleSynchronized extends JavacAnnotationHandler<Synchronized> {
JCExpression objectType = genJavaLangTypeRef(methodNode, ast.pos, "Object");
//We use 'new Object[0];' because unlike 'new Object();', empty arrays *ARE* serializable!
JCNewArray newObjectArray = maker.NewArray(genJavaLangTypeRef(methodNode, ast.pos, "Object"),
- List.<JCExpression>of(maker.Literal(CTC_INT, 0)), null);
+ List.<JCExpression>of(maker.Literal(CTC_INT, 0)), null);
JCVariableDecl fieldDecl = recursiveSetGeneratedBy(maker.VarDef(
- maker.Modifiers(Flags.PRIVATE | Flags.FINAL | (isStatic ? Flags.STATIC : 0)),
- methodNode.toName(lockName), objectType, newObjectArray), ast, context);
+ maker.Modifiers(Flags.PRIVATE | Flags.FINAL | (isStatic[0] ? Flags.STATIC : 0)),
+ methodNode.toName(lockName), objectType, newObjectArray), ast, context);
injectFieldAndMarkGenerated(methodNode.up(), fieldDecl);
}
if (method.body == null) return;
JCExpression lockNode;
- if (isStatic) {
- lockNode = chainDots(methodNode, ast.pos, methodNode.up().getName(), lockName);
+ if (isStatic[0]) {
+ lockNode = namePlusTypeParamsToTypeReference(maker, typeNode, methodNode.toName(lockName), false, List.<JCTypeParameter>nil());
} else {
lockNode = maker.Select(maker.Ident(methodNode.toName("this")), methodNode.toName(lockName));
}