Skip to content

Commit

Permalink
SignaturePattern: minor structural refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Kriegisch <[email protected]>
  • Loading branch information
kriegaex committed Apr 12, 2024
1 parent 9a15ee2 commit 5deb172
Showing 1 changed file with 37 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -42,11 +43,11 @@
import org.aspectj.weaver.World;

public class SignaturePattern extends PatternNode implements ISignaturePattern {
private MemberKind kind;
private ModifiersPattern modifiers;
private final MemberKind kind;
private final ModifiersPattern modifiers;
private TypePattern returnType;
private TypePattern declaringType;
private NamePattern name;
private final NamePattern name;
private TypePatternList parameterTypes;
private int bits = 0x0000;
private static final int PARAMETER_ANNOTATION_MATCHING = 0x0001;
Expand Down Expand Up @@ -103,29 +104,37 @@ public SignaturePattern resolveBindings(IScope scope, Bindings bindings) {

private void checkForIncorrectTargetKind(PatternNode patternNode, IScope scope, boolean targetsOtherThanTypeAllowed) {
checkForIncorrectTargetKind(patternNode, scope, targetsOtherThanTypeAllowed, false);

}

// bug 115252 - adding an xlint warning if the annnotation target type is
// wrong. This logic, or similar, may have to be applied elsewhere in the case
// of pointcuts which don't go through SignaturePattern.resolveBindings(..)
private void checkForIncorrectTargetKind(PatternNode patternNode, IScope scope, boolean targetsOtherThanTypeAllowed,
boolean parameterTargettingAnnotationsAllowed) {
private void checkForIncorrectTargetKind(
PatternNode patternNode,
IScope scope,
boolean targetsOtherThanTypeAllowed,
boolean parameterTargettingAnnotationsAllowed
)
{
final World world = scope.getWorld();
// return if we're not in java5 mode, if the unmatchedTargetKind Xlint
// warning has been turned off, or if the patternNode is *
if (!scope.getWorld().isInJava5Mode() || scope.getWorld().getLint().unmatchedTargetKind == null
|| (patternNode instanceof AnyTypePattern)) {
if (
!world.isInJava5Mode() ||
!world.getLint().unmatchedTargetKind.isEnabled() ||
patternNode instanceof AnyTypePattern
) {
return;
}
if (patternNode instanceof ExactAnnotationTypePattern) {
ResolvedType resolvedType = ((ExactAnnotationTypePattern) patternNode).getAnnotationType().resolve(scope.getWorld());
ResolvedType resolvedType = ((ExactAnnotationTypePattern) patternNode).getAnnotationType().resolve(world);
if (targetsOtherThanTypeAllowed) {
AnnotationTargetKind[] targetKinds = resolvedType.getAnnotationTargetKinds();
if (targetKinds == null) {
return;
}
reportUnmatchedTargetKindMessage(targetKinds, patternNode, scope, true);
} else if (!targetsOtherThanTypeAllowed && !resolvedType.canAnnotationTargetType()) {
} else if (!resolvedType.canAnnotationTargetType()) {
// everything is incorrect since we've already checked whether we have the TYPE target annotation
AnnotationTargetKind[] targetKinds = resolvedType.getAnnotationTargetKinds();
if (targetKinds == null) {
Expand All @@ -134,12 +143,11 @@ private void checkForIncorrectTargetKind(PatternNode patternNode, IScope scope,
reportUnmatchedTargetKindMessage(targetKinds, patternNode, scope, false);
}
} else {
TypePatternVisitor visitor = new TypePatternVisitor(scope, targetsOtherThanTypeAllowed,
parameterTargettingAnnotationsAllowed);
TypePatternVisitor visitor = new TypePatternVisitor(scope, targetsOtherThanTypeAllowed, parameterTargettingAnnotationsAllowed);
patternNode.traverse(visitor, null);
if (visitor.containedIncorrectTargetKind()) {
Set<ExactAnnotationTypePattern> keys = visitor.getIncorrectTargetKinds().keySet();
for (PatternNode node : keys) {
for (ExactAnnotationTypePattern node : keys) {
AnnotationTargetKind[] targetKinds = visitor.getIncorrectTargetKinds().get(node);
reportUnmatchedTargetKindMessage(targetKinds, node, scope, false);
}
Expand All @@ -156,13 +164,16 @@ private void reportUnmatchedTargetKindMessage(AnnotationTargetKind[] annotationT
return;
}
if (i < (annotationTargetKinds.length - 1)) {
targetNames.append("ElementType." + targetKind.getName() + ",");
targetNames.append("ElementType.").append(targetKind.getName()).append(",");
} else {
targetNames.append("ElementType." + targetKind.getName() + "}");
targetNames.append("ElementType.").append(targetKind.getName()).append("}");
}
}
scope.getWorld().getLint().unmatchedTargetKind.signal(new String[] { node.toString(), targetNames.toString() },
getSourceLocation(), new ISourceLocation[0]);
scope.getWorld().getLint().unmatchedTargetKind.signal(
new String[] { node.toString(), targetNames.toString() },
getSourceLocation(),
new ISourceLocation[0]
);
}

/**
Expand All @@ -172,16 +183,11 @@ private void reportUnmatchedTargetKindMessage(AnnotationTargetKind[] annotationT
*/
private class TypePatternVisitor extends AbstractPatternNodeVisitor {

private IScope scope;
private Map<ExactAnnotationTypePattern, AnnotationTargetKind[]> incorrectTargetKinds = new HashMap<>();
private boolean targetsOtherThanTypeAllowed;
private boolean parameterTargettingAnnotationsAllowed;
private final IScope scope;
private final Map<ExactAnnotationTypePattern, AnnotationTargetKind[]> incorrectTargetKinds = new HashMap<>();
private final boolean targetsOtherThanTypeAllowed;
private final boolean parameterTargettingAnnotationsAllowed;

/**
* @param requiredTarget - the signature pattern Kind
* @param scope
* @param parameterTargettingAnnotationsAllowed
*/
public TypePatternVisitor(IScope scope, boolean targetsOtherThanTypeAllowed, boolean parameterTargettingAnnotationsAllowed) {
this.scope = scope;
this.targetsOtherThanTypeAllowed = targetsOtherThanTypeAllowed;
Expand Down Expand Up @@ -264,7 +270,7 @@ public Object visit(AnyWithAnnotationTypePattern node, Object data) {
}

public boolean containedIncorrectTargetKind() {
return (incorrectTargetKinds.size() != 0);
return (!incorrectTargetKinds.isEmpty());
}

public Map<ExactAnnotationTypePattern, AnnotationTargetKind[]> getIncorrectTargetKinds() {
Expand Down Expand Up @@ -450,12 +456,8 @@ private boolean parametersCannotMatch(JoinPointSignature methodJoinpoint) {

// Quick rule: pattern doesn't specify ellipsis and there are a different number of parameters on the
// method join point as compared with the pattern
if (parameterTypes.ellipsisCount == 0 && !equalCount) {
if (patternParameterCount > 0 && parameterTypes.get(patternParameterCount - 1).isVarArgs()) {
return false;
}
return true;
}
if (parameterTypes.ellipsisCount == 0 && !equalCount)
return patternParameterCount <= 0 || !parameterTypes.get(patternParameterCount - 1).isVarArgs();
}

return false;
Expand Down Expand Up @@ -674,9 +676,7 @@ private boolean matchesVarArgs(JoinPointSignature aMethodOrConstructor, World in
}
} else {
// the method ends with an array type, check that we don't *require* a varargs
if (lastPattern.isVarArgs()) {
return false;
}
return !lastPattern.isVarArgs();
}

return true;
Expand Down Expand Up @@ -723,7 +723,7 @@ private FuzzyBoolean matchesAnnotations(ResolvedMember member, World world) {
}

private ResolvedMember findMethod(ResolvedType aspectType, ResolvedMember ajcMethod) {
ResolvedMember decMethods[] = aspectType.getDeclaredMethods();
ResolvedMember[] decMethods = aspectType.getDeclaredMethods();
for (ResolvedMember member : decMethods) {
if (member.equals(ajcMethod)) {
return member;
Expand Down

0 comments on commit 5deb172

Please sign in to comment.