Skip to content

Commit 702fe17

Browse files
committed
Separate Method and Constructor Existence Checks
1 parent 23d1ffd commit 702fe17

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

liquidjava-verifier/src/main/java/liquidjava/processor/refinement_checker/ExternalRefinementTypeChecker.java

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,26 @@ public <R> void visitCtMethod(CtMethod<R> method) {
7878
if (errorEmitter.foundError())
7979
return;
8080

81-
if (!methodExists(method)) {
82-
ErrorHandler.printCustomError(method, String.format("Could not find method '%s %s' in class '%s'",
83-
method.getType().getSimpleName(), method.getSignature(), prefix), errorEmitter);
81+
CtType<?> targetType = factory.Type().createReference(prefix).getTypeDeclaration();
82+
if (targetType == null || !(targetType instanceof CtClass))
8483
return;
84+
85+
boolean isConstructor = method.getSimpleName().equals(targetType.getSimpleName());
86+
if (isConstructor) {
87+
if (!constructorExists(targetType, method)) {
88+
ErrorHandler.printCustomError(method,
89+
String.format("Could not find constructor '%s' for '%s'", method.getSignature(), prefix),
90+
errorEmitter);
91+
return;
92+
}
93+
} else {
94+
if (!methodExists(targetType, method)) {
95+
ErrorHandler.printCustomError(method, String.format("Could not find method '%s %s' for '%s'",
96+
method.getType().getSimpleName(), method.getSignature(), prefix), errorEmitter);
97+
return;
98+
}
8599
}
100+
86101
MethodsFunctionsChecker mfc = new MethodsFunctionsChecker(this);
87102
try {
88103
mfc.getMethodRefinements(method, prefix);
@@ -138,25 +153,18 @@ private boolean classExists(String className) {
138153
return factory.Type().createReference(className).getTypeDeclaration() != null;
139154
}
140155

141-
private boolean methodExists(CtMethod<?> method) {
142-
CtType<?> targetType = factory.Type().createReference(prefix).getTypeDeclaration();
143-
if (targetType == null)
144-
return false;
145-
146-
String methodName = method.getSimpleName();
147-
boolean isConstructor = methodName.equals(targetType.getSimpleName());
156+
private boolean methodExists(CtType<?> targetType, CtMethod<?> method) {
157+
// find method with matching signature
158+
return targetType.getMethods().stream().filter(m -> m.getSimpleName().equals(method.getSimpleName()))
159+
.anyMatch(m -> parametersMatch(m.getParameters(), method.getParameters())
160+
&& typesMatch(m.getType(), method.getType()));
161+
}
148162

149-
if (isConstructor && targetType instanceof CtClass) {
150-
// find constructor with matching signature
151-
CtClass<?> targetClass = (CtClass<?>) targetType;
152-
return targetClass.getConstructors().stream()
153-
.anyMatch(c -> parametersMatch(c.getParameters(), method.getParameters()));
154-
} else {
155-
// find method with matching signature
156-
return targetType.getMethods().stream().filter(m -> m.getSimpleName().equals(methodName))
157-
.anyMatch(m -> parametersMatch(m.getParameters(), method.getParameters())
158-
&& typesMatch(m.getType(), method.getType()));
159-
}
163+
private boolean constructorExists(CtType<?> targetType, CtMethod<?> method) {
164+
// find constructor with matching signature
165+
CtClass<?> targetClass = (CtClass<?>) targetType;
166+
return targetClass.getConstructors().stream()
167+
.anyMatch(c -> parametersMatch(c.getParameters(), method.getParameters()));
160168
}
161169

162170
private boolean typesMatch(CtTypeReference<?> type1, CtTypeReference<?> type2) {

0 commit comments

Comments
 (0)