mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-20 02:17:52 +00:00
Fix for issue #136: @Getter and @Setter now work on static fields again.
This commit is contained in:
1 parent
83b4dd8613
commit
357fd954e5
6 files changed
+49
-35
No files matched your search
@@ -186,11 +186,23 @@ public class EclipseHandlerUtil {
|
||||
GetterMethod getter = useFieldsDirectly ? null : findGetter(field);
|
||||
|
||||
if (getter == null) {
|
||||
FieldReference thisX = new FieldReference(field.getName().toCharArray(), p);
|
||||
Eclipse.setGeneratedBy(thisX, source);
|
||||
thisX.receiver = new ThisReference(pS, pE);
|
||||
Eclipse.setGeneratedBy(thisX.receiver, source);
|
||||
return thisX;
|
||||
FieldDeclaration fieldDecl = (FieldDeclaration)field.get();
|
||||
FieldReference ref = new FieldReference(field.getName().toCharArray(), p);
|
||||
if ((fieldDecl.modifiers & ClassFileConstants.AccStatic) != 0) {
|
||||
EclipseNode containerNode = field.up();
|
||||
if (containerNode != null && containerNode.get() instanceof TypeDeclaration) {
|
||||
ref.receiver = new SingleNameReference(((TypeDeclaration)containerNode.get()).name, p);
|
||||
} else {
|
||||
Expression smallRef = new FieldReference(field.getName().toCharArray(), p);
|
||||
Eclipse.setGeneratedBy(smallRef, source);
|
||||
return smallRef;
|
||||
}
|
||||
} else {
|
||||
ref.receiver = new ThisReference(pS, pE);
|
||||
}
|
||||
Eclipse.setGeneratedBy(ref, source);
|
||||
Eclipse.setGeneratedBy(ref.receiver, source);
|
||||
return ref;
|
||||
}
|
||||
|
||||
MessageSend call = new MessageSend();
|
||||
|
||||
@@ -37,12 +37,11 @@ import lombok.eclipse.EclipseNode;
|
||||
|
||||
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
|
||||
import org.eclipse.jdt.internal.compiler.ast.Annotation;
|
||||
import org.eclipse.jdt.internal.compiler.ast.Expression;
|
||||
import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration;
|
||||
import org.eclipse.jdt.internal.compiler.ast.FieldReference;
|
||||
import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration;
|
||||
import org.eclipse.jdt.internal.compiler.ast.ReturnStatement;
|
||||
import org.eclipse.jdt.internal.compiler.ast.Statement;
|
||||
import org.eclipse.jdt.internal.compiler.ast.ThisReference;
|
||||
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
|
||||
import org.eclipse.jdt.internal.compiler.ast.TypeReference;
|
||||
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
|
||||
@@ -172,7 +171,7 @@ public class HandleGetter implements EclipseAnnotationHandler<Getter> {
|
||||
}
|
||||
}
|
||||
|
||||
MethodDeclaration method = generateGetter((TypeDeclaration) fieldNode.up().get(), field, getterName, modifier, source);
|
||||
MethodDeclaration method = generateGetter((TypeDeclaration) fieldNode.up().get(), fieldNode, getterName, modifier, source);
|
||||
Annotation[] copiedAnnotations = copyAnnotations(
|
||||
findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN),
|
||||
findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN), source);
|
||||
@@ -185,8 +184,8 @@ public class HandleGetter implements EclipseAnnotationHandler<Getter> {
|
||||
return true;
|
||||
}
|
||||
|
||||
private MethodDeclaration generateGetter(TypeDeclaration parent, FieldDeclaration field, String name,
|
||||
int modifier, ASTNode source) {
|
||||
private MethodDeclaration generateGetter(TypeDeclaration parent, EclipseNode fieldNode, String name, int modifier, ASTNode source) {
|
||||
FieldDeclaration field = (FieldDeclaration) fieldNode.get();
|
||||
MethodDeclaration method = new MethodDeclaration(parent.compilationResult);
|
||||
Eclipse.setGeneratedBy(method, source);
|
||||
method.modifiers = modifier;
|
||||
@@ -198,12 +197,8 @@ public class HandleGetter implements EclipseAnnotationHandler<Getter> {
|
||||
method.thrownExceptions = null;
|
||||
method.typeParameters = null;
|
||||
method.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
|
||||
long p = ((long)field.declarationSourceStart << 32) | field.declarationSourceEnd;
|
||||
FieldReference thisX = new FieldReference(field.name, p);
|
||||
Eclipse.setGeneratedBy(thisX, source);
|
||||
thisX.receiver = new ThisReference(source.sourceStart, source.sourceEnd);
|
||||
Eclipse.setGeneratedBy(thisX.receiver, source);
|
||||
Statement returnStatement = new ReturnStatement(thisX, field.sourceStart, field.sourceEnd);
|
||||
Expression fieldRef = createFieldAccessor(fieldNode, true, source);
|
||||
Statement returnStatement = new ReturnStatement(fieldRef, field.sourceStart, field.sourceEnd);
|
||||
Eclipse.setGeneratedBy(returnStatement, source);
|
||||
method.bodyStart = method.declarationSourceStart = method.sourceStart = source.sourceStart;
|
||||
method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = source.sourceEnd;
|
||||
|
||||
@@ -40,13 +40,12 @@ import org.eclipse.jdt.internal.compiler.ast.ASTNode;
|
||||
import org.eclipse.jdt.internal.compiler.ast.Annotation;
|
||||
import org.eclipse.jdt.internal.compiler.ast.Argument;
|
||||
import org.eclipse.jdt.internal.compiler.ast.Assignment;
|
||||
import org.eclipse.jdt.internal.compiler.ast.Expression;
|
||||
import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration;
|
||||
import org.eclipse.jdt.internal.compiler.ast.FieldReference;
|
||||
import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration;
|
||||
import org.eclipse.jdt.internal.compiler.ast.NameReference;
|
||||
import org.eclipse.jdt.internal.compiler.ast.SingleNameReference;
|
||||
import org.eclipse.jdt.internal.compiler.ast.Statement;
|
||||
import org.eclipse.jdt.internal.compiler.ast.ThisReference;
|
||||
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
|
||||
import org.eclipse.jdt.internal.compiler.ast.TypeReference;
|
||||
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
|
||||
@@ -168,16 +167,15 @@ public class HandleSetter implements EclipseAnnotationHandler<Setter> {
|
||||
//continue with creating the setter
|
||||
}
|
||||
|
||||
MethodDeclaration method = generateSetter((TypeDeclaration) fieldNode.up().get(), field, setterName, modifier, pos);
|
||||
MethodDeclaration method = generateSetter((TypeDeclaration) fieldNode.up().get(), fieldNode, setterName, modifier, pos);
|
||||
|
||||
injectMethod(fieldNode.up(), method);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private MethodDeclaration generateSetter(TypeDeclaration parent, FieldDeclaration field, String name,
|
||||
int modifier, ASTNode source) {
|
||||
|
||||
private MethodDeclaration generateSetter(TypeDeclaration parent, EclipseNode fieldNode, String name, int modifier, ASTNode source) {
|
||||
FieldDeclaration field = (FieldDeclaration) fieldNode.get();
|
||||
int pS = source.sourceStart, pE = source.sourceEnd;
|
||||
long p = (long)pS << 32 | pE;
|
||||
MethodDeclaration method = new MethodDeclaration(parent.compilationResult);
|
||||
@@ -196,13 +194,10 @@ public class HandleSetter implements EclipseAnnotationHandler<Setter> {
|
||||
method.thrownExceptions = null;
|
||||
method.typeParameters = null;
|
||||
method.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
|
||||
FieldReference thisX = new FieldReference(field.name, p);
|
||||
Eclipse.setGeneratedBy(thisX, source);
|
||||
thisX.receiver = new ThisReference(source.sourceStart, source.sourceEnd);
|
||||
Eclipse.setGeneratedBy(thisX.receiver, source);
|
||||
Expression fieldRef = createFieldAccessor(fieldNode, true, source);
|
||||
NameReference fieldNameRef = new SingleNameReference(field.name, p);
|
||||
Eclipse.setGeneratedBy(fieldNameRef, source);
|
||||
Assignment assignment = new Assignment(thisX, fieldNameRef, (int)p);
|
||||
Assignment assignment = new Assignment(fieldRef, fieldNameRef, (int)p);
|
||||
assignment.sourceStart = pS; assignment.sourceEnd = pE;
|
||||
Eclipse.setGeneratedBy(assignment, source);
|
||||
method.bodyStart = method.declarationSourceStart = method.sourceStart = source.sourceStart;
|
||||
|
||||
@@ -42,7 +42,6 @@ import com.sun.tools.javac.tree.JCTree.JCAnnotation;
|
||||
import com.sun.tools.javac.tree.JCTree.JCBlock;
|
||||
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
|
||||
import com.sun.tools.javac.tree.JCTree.JCExpression;
|
||||
import com.sun.tools.javac.tree.JCTree.JCFieldAccess;
|
||||
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
|
||||
import com.sun.tools.javac.tree.JCTree.JCStatement;
|
||||
import com.sun.tools.javac.tree.JCTree.JCTypeParameter;
|
||||
@@ -186,8 +185,8 @@ public class HandleGetter implements JavacAnnotationHandler<Getter> {
|
||||
|
||||
private JCMethodDecl createGetter(long access, JavacNode field, TreeMaker treeMaker) {
|
||||
JCVariableDecl fieldNode = (JCVariableDecl) field.get();
|
||||
JCFieldAccess thisX = treeMaker.Select(treeMaker.Ident(field.toName("this")), fieldNode.name);
|
||||
JCStatement returnStatement = treeMaker.Return(thisX);
|
||||
JCExpression fieldRef = createFieldAccessor(treeMaker, field, true);
|
||||
JCStatement returnStatement = treeMaker.Return(fieldRef);
|
||||
|
||||
JCBlock methodBody = treeMaker.Block(0, List.of(returnStatement));
|
||||
Name methodName = field.toName(toGetterName(fieldNode));
|
||||
|
||||
@@ -49,7 +49,6 @@ import com.sun.tools.javac.tree.JCTree.JCAssign;
|
||||
import com.sun.tools.javac.tree.JCTree.JCBlock;
|
||||
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
|
||||
import com.sun.tools.javac.tree.JCTree.JCExpression;
|
||||
import com.sun.tools.javac.tree.JCTree.JCFieldAccess;
|
||||
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
|
||||
import com.sun.tools.javac.tree.JCTree.JCStatement;
|
||||
import com.sun.tools.javac.tree.JCTree.JCTypeParameter;
|
||||
@@ -189,8 +188,8 @@ public class HandleSetter implements JavacAnnotationHandler<Setter> {
|
||||
private JCMethodDecl createSetter(long access, JavacNode field, TreeMaker treeMaker) {
|
||||
JCVariableDecl fieldDecl = (JCVariableDecl) field.get();
|
||||
|
||||
JCFieldAccess thisX = treeMaker.Select(treeMaker.Ident(field.toName("this")), fieldDecl.name);
|
||||
JCAssign assign = treeMaker.Assign(thisX, treeMaker.Ident(fieldDecl.name));
|
||||
JCExpression fieldRef = createFieldAccessor(treeMaker, field, true);
|
||||
JCAssign assign = treeMaker.Assign(fieldRef, treeMaker.Ident(fieldDecl.name));
|
||||
|
||||
List<JCStatement> statements;
|
||||
List<JCAnnotation> nonNulls = findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN);
|
||||
|
||||
@@ -361,16 +361,30 @@ public class JavacHandlerUtil {
|
||||
* Creates an expression that reads the field. Will either be {@code this.field} or {@code this.getField()} depending on whether or not there's a getter.
|
||||
*/
|
||||
static JCExpression createFieldAccessor(TreeMaker maker, JavacNode field, boolean useFieldsDirectly) {
|
||||
return createFieldAccessor(maker, field, useFieldsDirectly, maker.Ident(field.toName("this")));
|
||||
return createFieldAccessor(maker, field, useFieldsDirectly, null);
|
||||
}
|
||||
|
||||
static JCExpression createFieldAccessor(TreeMaker maker, JavacNode field, boolean useFieldsDirectly, JCExpression receiver) {
|
||||
GetterMethod getter = useFieldsDirectly ? null : findGetter(field);
|
||||
JCVariableDecl fieldDecl = (JCVariableDecl) field.get();
|
||||
|
||||
if (getter == null) {
|
||||
return maker.Select(receiver, ((JCVariableDecl)field.get()).name);
|
||||
if (receiver == null) {
|
||||
if ((fieldDecl.mods.flags & Flags.STATIC) == 0) {
|
||||
receiver = maker.Ident(field.toName("this"));
|
||||
} else {
|
||||
JavacNode containerNode = field.up();
|
||||
if (containerNode != null && containerNode.get() instanceof JCClassDecl) {
|
||||
JCClassDecl container = (JCClassDecl) field.up().get();
|
||||
receiver = maker.Ident(container.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return receiver == null ? maker.Ident(fieldDecl.name) : maker.Select(receiver, fieldDecl.name);
|
||||
}
|
||||
|
||||
if (receiver == null) receiver = maker.Ident(field.toName("this"));
|
||||
JCMethodInvocation call = maker.Apply(List.<JCExpression>nil(),
|
||||
maker.Select(receiver, getter.name), List.<JCExpression>nil());
|
||||
return call;
|
||||
|
||||
Reference in new issue
Block a user