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 8bc358f
Showing 1 changed file with 25 additions and 14 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 @@ -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 Down

0 comments on commit 8bc358f

Please sign in to comment.