use of org.codehaus.groovy.ast.Parameter in project groovy by apache.
the class GenericsUtils method parameterizeSAM.
/**
* Gets the parameter and return types of the abstract method of SAM.
*
* If the abstract method is not parameterized, we will get generics placeholders, e.g. T, U
* For example, the abstract method of {@link java.util.function.Function} is
* <pre>
* R apply(T t);
* </pre>
*
* We parameterize the above interface as {@code Function<String, Integer>}, then the abstract method will be
* <pre>
* Integer apply(String t);
* </pre>
*
* When we call {@code parameterizeSAM} on the ClassNode {@code Function<String, Integer>},
* we can get parameter types and return type of the above abstract method,
* i.e. ClassNode {@code ClassHelper.STRING_TYPE} and {@code ClassHelper.Integer_TYPE}
*
* @param samType the class node which contains only one abstract method
*
* @since 3.0.0
*/
public static Tuple2<ClassNode[], ClassNode> parameterizeSAM(final ClassNode samType) {
MethodNode abstractMethod = ClassHelper.findSAM(samType);
Map<GenericsType, GenericsType> generics = makeDeclaringAndActualGenericsTypeMapOfExactType(abstractMethod.getDeclaringClass(), samType);
Function<ClassNode, ClassNode> resolver = t -> {
if (t.isGenericsPlaceHolder()) {
return findActualTypeByGenericsPlaceholderName(t.getUnresolvedName(), generics);
}
return t;
};
ClassNode[] parameterTypes = Arrays.stream(abstractMethod.getParameters()).map(Parameter::getType).map(resolver).toArray(ClassNode[]::new);
ClassNode returnType = resolver.apply(abstractMethod.getReturnType());
return tuple(parameterTypes, returnType);
}
use of org.codehaus.groovy.ast.Parameter in project groovy by apache.
the class GeneralUtils method makeDescriptorWithoutReturnType.
/**
* @deprecated use MethodNodeUtils#methodDescriptorWithoutReturnType(MethodNode) instead
*/
@Deprecated
public static String makeDescriptorWithoutReturnType(final MethodNode mn) {
StringBuilder sb = new StringBuilder();
sb.append(mn.getName()).append(':');
for (Parameter p : mn.getParameters()) {
sb.append(p.getType()).append(',');
}
return sb.toString();
}
use of org.codehaus.groovy.ast.Parameter in project groovy by apache.
the class ClassCompletionVerifier method visitMethod.
@Override
public void visitMethod(MethodNode node) {
inConstructor = false;
inStaticConstructor = node.isStaticConstructor();
checkAbstractDeclaration(node);
checkRepetitiveMethod(node);
checkOverloadingPrivateAndPublic(node);
checkMethodModifiers(node);
checkGenericsUsage(node, node.getParameters());
checkGenericsUsage(node, node.getReturnType());
for (Parameter param : node.getParameters()) {
if (isPrimitiveVoid(param.getType())) {
addError("The " + getDescription(param) + " in " + getDescription(node) + " has invalid type void", param);
}
}
super.visitMethod(node);
}
use of org.codehaus.groovy.ast.Parameter in project groovy by apache.
the class ClassCompletionVerifier method appendParamsDescription.
private void appendParamsDescription(Parameter[] parameters, StringBuilder msg) {
msg.append("(");
boolean needsComma = false;
for (Parameter parameter : parameters) {
if (needsComma) {
msg.append(",");
} else {
needsComma = true;
}
msg.append(parameter.getType());
}
msg.append(")");
}
use of org.codehaus.groovy.ast.Parameter in project groovy by apache.
the class ClassCompletionVerifier method checkMethodsForOverridingFinal.
private void checkMethodsForOverridingFinal(ClassNode cn) {
for (MethodNode method : cn.getMethods()) {
Parameter[] params = method.getParameters();
for (MethodNode superMethod : cn.getSuperClass().getMethods(method.getName())) {
Parameter[] superParams = superMethod.getParameters();
if (!hasEqualParameterTypes(params, superParams))
continue;
if (!superMethod.isFinal())
break;
addInvalidUseOfFinalError(method, params, superMethod.getDeclaringClass());
return;
}
}
}
Aggregations