From 649a7b72bf8119e286e991bce29fb63bdb26c09d Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Tue, 15 May 2018 00:50:58 +0200 Subject: [new-style include/exclude] added new-style include/exclude support to EqualsAndHashCode. --- .../eclipse/handlers/EclipseHandlerUtil.java | 14 +++ .../lombok/eclipse/handlers/HandleBuilder.java | 7 +- .../eclipse/handlers/HandleEqualsAndHashCode.java | 140 ++++++++------------- .../lombok/eclipse/handlers/HandleToString.java | 14 +-- 4 files changed, 75 insertions(+), 100 deletions(-) (limited to 'src/core/lombok/eclipse') diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java index 9f9b3975..1758a220 100644 --- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java +++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java @@ -1041,6 +1041,20 @@ public class EclipseHandlerUtil { return call; } + static Expression createMethodAccessor(EclipseNode method, ASTNode source, char[] receiver) { + int pS = source == null ? 0 : source.sourceStart, pE = source == null ? 0 : source.sourceEnd; + long p = (long) pS << 32 | pE; + + MethodDeclaration methodDecl = (MethodDeclaration) method.get(); + MessageSend call = new MessageSend(); + setGeneratedBy(call, source); + call.sourceStart = pS; call.statementEnd = call.sourceEnd = pE; + call.receiver = new SingleNameReference(receiver, p); + setGeneratedBy(call.receiver, source); + call.selector = methodDecl.selector; + return call; + } + /** Serves as return value for the methods that check for the existence of fields and methods. */ public enum MemberExistsResult { NOT_EXISTS, EXISTS_BY_LOMBOK, EXISTS_BY_USER; diff --git a/src/core/lombok/eclipse/handlers/HandleBuilder.java b/src/core/lombok/eclipse/handlers/HandleBuilder.java index aa9fad84..38bd9b60 100644 --- a/src/core/lombok/eclipse/handlers/HandleBuilder.java +++ b/src/core/lombok/eclipse/handlers/HandleBuilder.java @@ -74,9 +74,10 @@ import lombok.Builder; import lombok.Builder.ObtainVia; import lombok.ConfigurationKeys; import lombok.Singular; +import lombok.ToString; import lombok.core.AST.Kind; import lombok.core.handlers.HandlerUtil; -import lombok.core.handlers.InclusionExclusionUtils.ToStringMember; +import lombok.core.handlers.InclusionExclusionUtils.Included; import lombok.core.AnnotationValues; import lombok.core.HandlerPriority; import lombok.eclipse.Eclipse; @@ -450,10 +451,10 @@ public class HandleBuilder extends EclipseAnnotationHandler { } if (methodExists("toString", builderType, 0) == MemberExistsResult.NOT_EXISTS) { - List> fieldNodes = new ArrayList>(); + List> fieldNodes = new ArrayList>(); for (BuilderFieldData bfd : builderFields) { for (EclipseNode f : bfd.createdFields) { - fieldNodes.add(new ToStringMember(f, null, true)); + fieldNodes.add(new Included(f, null, true)); } } MethodDeclaration md = HandleToString.createToString(builderType, fieldNodes, true, false, ast, FieldAccess.ALWAYS_FIELD); diff --git a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java index 2a497420..e30df698 100644 --- a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2015 The Project Lombok Authors. + * Copyright (C) 2009-2018 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 @@ -38,6 +38,8 @@ import lombok.ConfigurationKeys; import lombok.EqualsAndHashCode; import lombok.core.AST.Kind; import lombok.core.handlers.HandlerUtil; +import lombok.core.handlers.InclusionExclusionUtils; +import lombok.core.handlers.InclusionExclusionUtils.Included; import lombok.core.AnnotationValues; import lombok.core.configuration.CallSuperType; import lombok.eclipse.Eclipse; @@ -56,7 +58,6 @@ import org.eclipse.jdt.internal.compiler.ast.ConditionalExpression; import org.eclipse.jdt.internal.compiler.ast.EqualExpression; import org.eclipse.jdt.internal.compiler.ast.Expression; import org.eclipse.jdt.internal.compiler.ast.FalseLiteral; -import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration; import org.eclipse.jdt.internal.compiler.ast.IfStatement; import org.eclipse.jdt.internal.compiler.ast.InstanceOfExpression; import org.eclipse.jdt.internal.compiler.ast.IntLiteral; @@ -98,62 +99,41 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler BUILT_IN_TYPES = Collections.unmodifiableSet(new HashSet(Arrays.asList( "byte", "short", "int", "long", "char", "boolean", "double", "float"))); - public void checkForBogusFieldNames(EclipseNode type, AnnotationValues annotation) { - if (annotation.isExplicit("exclude")) { - for (int i : createListOfNonExistentFields(Arrays.asList(annotation.getInstance().exclude()), type, true, true)) { - annotation.setWarning("exclude", "This field does not exist, or would have been excluded anyway.", i); - } - } - if (annotation.isExplicit("of")) { - for (int i : createListOfNonExistentFields(Arrays.asList(annotation.getInstance().of()), type, false, false)) { - annotation.setWarning("of", "This field does not exist.", i); - } - } - } - - public void generateEqualsAndHashCodeForType(EclipseNode typeNode, EclipseNode errorNode) { - if (hasAnnotation(EqualsAndHashCode.class, typeNode)) { - //The annotation will make it happen, so we can skip it. - return; - } - - Boolean doNotUseGettersConfiguration = typeNode.getAst().readConfiguration(ConfigurationKeys.EQUALS_AND_HASH_CODE_DO_NOT_USE_GETTERS); - FieldAccess access = doNotUseGettersConfiguration == null || !doNotUseGettersConfiguration ? FieldAccess.GETTER : FieldAccess.PREFER_FIELD; - - generateMethods(typeNode, errorNode, null, null, null, false, access, new ArrayList()); - } - @Override public void handle(AnnotationValues annotation, Annotation ast, EclipseNode annotationNode) { handleFlagUsage(annotationNode, ConfigurationKeys.EQUALS_AND_HASH_CODE_FLAG_USAGE, "@EqualsAndHashCode"); EqualsAndHashCode ann = annotation.getInstance(); - List excludes = Arrays.asList(ann.exclude()); - List includes = Arrays.asList(ann.of()); - EclipseNode typeNode = annotationNode.up(); + List> members = InclusionExclusionUtils.handleEqualsAndHashCodeMarking(annotationNode.up(), annotation, annotationNode); + if (members == null) return; List onParam = unboxAndRemoveAnnotationParameter(ast, "onParam", "@EqualsAndHashCode(onParam", annotationNode); - checkForBogusFieldNames(typeNode, annotation); Boolean callSuper = ann.callSuper(); if (!annotation.isExplicit("callSuper")) callSuper = null; - if (!annotation.isExplicit("exclude")) excludes = null; - if (!annotation.isExplicit("of")) includes = null; - - if (excludes != null && includes != null) { - excludes = null; - annotation.setWarning("exclude", "exclude and of are mutually exclusive; the 'exclude' parameter will be ignored."); - } Boolean doNotUseGettersConfiguration = annotationNode.getAst().readConfiguration(ConfigurationKeys.EQUALS_AND_HASH_CODE_DO_NOT_USE_GETTERS); boolean doNotUseGetters = annotation.isExplicit("doNotUseGetters") || doNotUseGettersConfiguration == null ? ann.doNotUseGetters() : doNotUseGettersConfiguration; FieldAccess fieldAccess = doNotUseGetters ? FieldAccess.PREFER_FIELD : FieldAccess.GETTER; - generateMethods(typeNode, annotationNode, excludes, includes, callSuper, true, fieldAccess, onParam); + generateMethods(annotationNode.up(), annotationNode, members, callSuper, true, fieldAccess, onParam); + } + + public void generateEqualsAndHashCodeForType(EclipseNode typeNode, EclipseNode errorNode) { + if (hasAnnotation(EqualsAndHashCode.class, typeNode)) { + //The annotation will make it happen, so we can skip it. + return; + } + + List> members = InclusionExclusionUtils.handleEqualsAndHashCodeMarking(typeNode, null, null); + + Boolean doNotUseGettersConfiguration = typeNode.getAst().readConfiguration(ConfigurationKeys.EQUALS_AND_HASH_CODE_DO_NOT_USE_GETTERS); + FieldAccess access = doNotUseGettersConfiguration == null || !doNotUseGettersConfiguration ? FieldAccess.GETTER : FieldAccess.PREFER_FIELD; + + generateMethods(typeNode, errorNode, members, null, false, access, new ArrayList()); } - public void generateMethods(EclipseNode typeNode, EclipseNode errorNode, List excludes, List includes, - Boolean callSuper, boolean whineIfExists, FieldAccess fieldAccess, List onParam) { - assert excludes == null || includes == null; + public void generateMethods(EclipseNode typeNode, EclipseNode errorNode, List> members, + Boolean callSuper, boolean whineIfExists, FieldAccess fieldAccess, List onParam) { TypeDeclaration typeDecl = null; @@ -171,7 +151,7 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler nodesForEquality = new ArrayList(); - if (includes != null) { - for (EclipseNode child : typeNode.down()) { - if (child.getKind() != Kind.FIELD) continue; - FieldDeclaration fieldDecl = (FieldDeclaration) child.get(); - if (includes.contains(new String(fieldDecl.name))) nodesForEquality.add(child); - } - } else { - for (EclipseNode child : typeNode.down()) { - if (child.getKind() != Kind.FIELD) continue; - FieldDeclaration fieldDecl = (FieldDeclaration) child.get(); - if (!filterField(fieldDecl)) continue; - - //Skip transient fields. - if ((fieldDecl.modifiers & ClassFileConstants.AccTransient) != 0) continue; - //Skip excluded fields. - if (excludes != null && excludes.contains(new String(fieldDecl.name))) continue; - nodesForEquality.add(child); - } - } - boolean isFinal = (typeDecl.modifiers & ClassFileConstants.AccFinal) != 0; boolean needsCanEqual = !isFinal || !isDirectDescendantOfObject; MemberExistsResult equalsExists = methodExists("equals", typeNode, 1); @@ -257,7 +216,7 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler fields, boolean callSuper, ASTNode source, FieldAccess fieldAccess) { + public MethodDeclaration createHashCode(EclipseNode type, Collection> members, boolean callSuper, ASTNode source, FieldAccess fieldAccess) { int pS = source.sourceStart, pE = source.sourceEnd; long p = (long)pS << 32 | pE; - MethodDeclaration method = new MethodDeclaration( - ((CompilationUnitDeclaration) type.top().get()).compilationResult); + MethodDeclaration method = new MethodDeclaration(((CompilationUnitDeclaration) type.top().get()).compilationResult); setGeneratedBy(method, source); method.modifiers = toEclipseModifier(AccessLevel.PUBLIC); @@ -294,10 +252,10 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler statements = new ArrayList(); - final boolean isEmpty = fields.isEmpty(); + final boolean isEmpty = members.isEmpty(); /* final int PRIME = X; */ { - /* Without fields, PRIME isn't used, and that would trigger a 'local variable not used' warning. */ + /* Without members, PRIME isn't used, as that would trigger a 'local variable not used' warning. */ if (!isEmpty) { LocalDeclaration primeDecl = new LocalDeclaration(PRIME, pS, pE); setGeneratedBy(primeDecl, source); @@ -310,7 +268,7 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler member : members) { + EclipseNode memberNode = member.getNode(); + boolean isMethod = memberNode.getKind() == Kind.METHOD; + + TypeReference fType = getFieldType(memberNode, fieldAccess); + char[] dollarFieldName = ((isMethod ? "$$" : "$") + memberNode.getName()).toCharArray(); char[] token = fType.getLastToken(); - Expression fieldAccessor = createFieldAccessor(field, fieldAccess, source); + Expression fieldAccessor = isMethod ? createMethodAccessor(memberNode, source) : createFieldAccessor(memberNode, fieldAccess, source); if (fType.dimensions() == 0 && token != null) { if (Arrays.equals(TypeConstants.BOOLEAN, token)) { /* booleanField ? X : Y */ @@ -534,12 +495,11 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler fields, boolean callSuper, ASTNode source, FieldAccess fieldAccess, boolean needsCanEqual, List onParam) { + public MethodDeclaration createEquals(EclipseNode type, Collection> members, boolean callSuper, ASTNode source, FieldAccess fieldAccess, boolean needsCanEqual, List onParam) { int pS = source.sourceStart; int pE = source.sourceEnd; long p = (long)pS << 32 | pE; - MethodDeclaration method = new MethodDeclaration( - ((CompilationUnitDeclaration) type.top().get()).compilationResult); + MethodDeclaration method = new MethodDeclaration(((CompilationUnitDeclaration) type.top().get()).compilationResult); setGeneratedBy(method, source); method.modifiers = toEclipseModifier(AccessLevel.PUBLIC); method.returnType = TypeReference.baseTypeReference(TypeIds.T_boolean, 0); @@ -606,7 +566,7 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler other = (Outer.Inner.MyType) o; */ { - if (!fields.isEmpty() || needsCanEqual) { + if (!members.isEmpty() || needsCanEqual) { LocalDeclaration other = new LocalDeclaration(otherName, pS, pE); other.modifiers |= ClassFileConstants.AccFinal; setGeneratedBy(other, source); @@ -675,11 +635,14 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler member : members) { + EclipseNode memberNode = member.getNode(); + boolean isMethod = memberNode.getKind() == Kind.METHOD; + + TypeReference fType = getFieldType(memberNode, fieldAccess); char[] token = fType.getLastToken(); - Expression thisFieldAccessor = createFieldAccessor(field, fieldAccess, source); - Expression otherFieldAccessor = createFieldAccessor(field, fieldAccess, source, otherName); + Expression thisFieldAccessor = isMethod ? createMethodAccessor(memberNode, source) : createFieldAccessor(memberNode, fieldAccess, source); + Expression otherFieldAccessor = isMethod ? createMethodAccessor(memberNode, source, otherName) : createFieldAccessor(memberNode, fieldAccess, source, otherName); if (fType.dimensions() == 0 && token != null) { if (Arrays.equals(TypeConstants.FLOAT, token)) { @@ -700,8 +663,8 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler onParam) { /* protected boolean canEqual(final java.lang.Object other) { * return other instanceof Outer.Inner.MyType; @@ -788,8 +749,7 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler { handleFlagUsage(annotationNode, ConfigurationKeys.TO_STRING_FLAG_USAGE, "@ToString"); ToString ann = annotation.getInstance(); - List> members = InclusionExclusionUtils.handleToStringMarking(annotationNode.up(), annotation, annotationNode); + List> members = InclusionExclusionUtils.handleToStringMarking(annotationNode.up(), annotation, annotationNode); if (members == null) return; Boolean callSuper = ann.callSuper(); @@ -105,11 +105,11 @@ public class HandleToString extends EclipseAnnotationHandler { Boolean doNotUseGettersConfiguration = typeNode.getAst().readConfiguration(ConfigurationKeys.TO_STRING_DO_NOT_USE_GETTERS); FieldAccess access = doNotUseGettersConfiguration == null || !doNotUseGettersConfiguration ? FieldAccess.GETTER : FieldAccess.PREFER_FIELD; - List> members = InclusionExclusionUtils.handleToStringMarking(typeNode, null, null); + List> members = InclusionExclusionUtils.handleToStringMarking(typeNode, null, null); generateToString(typeNode, errorNode, members, includeFieldNames, null, false, access); } - public void generateToString(EclipseNode typeNode, EclipseNode errorNode, List> members, + public void generateToString(EclipseNode typeNode, EclipseNode errorNode, List> members, boolean includeFieldNames, Boolean callSuper, boolean whineIfExists, FieldAccess fieldAccess) { TypeDeclaration typeDecl = null; @@ -145,7 +145,7 @@ public class HandleToString extends EclipseAnnotationHandler { } } - public static MethodDeclaration createToString(EclipseNode type, Collection> members, + public static MethodDeclaration createToString(EclipseNode type, Collection> members, boolean includeNames, boolean callSuper, ASTNode source, FieldAccess fieldAccess) { String typeName = getTypeName(type); @@ -163,7 +163,7 @@ public class HandleToString extends EclipseAnnotationHandler { } else if (members.isEmpty()) { prefix = (typeName + "()").toCharArray(); } else if (includeNames) { - ToStringMember firstMember = members.iterator().next(); + Included firstMember = members.iterator().next(); String name = firstMember.getInc() == null ? "" : firstMember.getInc().name(); if (name.isEmpty()) name = firstMember.getNode().getName(); prefix = (typeName + "(" + name + "=").toCharArray(); @@ -187,7 +187,7 @@ public class HandleToString extends EclipseAnnotationHandler { first = false; } - for (ToStringMember member : members) { + for (Included member : members) { EclipseNode memberNode = member.getNode(); TypeReference fieldType = getFieldType(memberNode, fieldAccess); -- cgit