use of org.codehaus.groovy.ast.ClassNode in project groovy by apache.
the class WideningCategories method areEqualWithGenerics.
/**
* Compares two class nodes, but including their generics types.
* @param a
* @param b
* @return true if the class nodes are equal, false otherwise
*/
private static boolean areEqualWithGenerics(ClassNode a, ClassNode b) {
if (a == null)
return b == null;
if (!a.equals(b))
return false;
if (a.isUsingGenerics() && !b.isUsingGenerics())
return false;
GenericsType[] gta = a.getGenericsTypes();
GenericsType[] gtb = b.getGenericsTypes();
if (gta == null && gtb != null)
return false;
if (gtb == null && gta != null)
return false;
if (gta != null && gtb != null) {
if (gta.length != gtb.length)
return false;
for (int i = 0; i < gta.length; i++) {
GenericsType ga = gta[i];
GenericsType gb = gtb[i];
boolean result = ga.isPlaceholder() == gb.isPlaceholder() && ga.isWildcard() == gb.isWildcard();
result = result && ga.isResolved() && gb.isResolved();
result = result && ga.getName().equals(gb.getName());
result = result && areEqualWithGenerics(ga.getType(), gb.getType());
result = result && areEqualWithGenerics(ga.getLowerBound(), gb.getLowerBound());
if (result) {
ClassNode[] upA = ga.getUpperBounds();
if (upA != null) {
ClassNode[] upB = gb.getUpperBounds();
if (upB == null || upB.length != upA.length)
return false;
for (int j = 0; j < upA.length; j++) {
if (!areEqualWithGenerics(upA[j], upB[j]))
return false;
}
}
}
if (!result)
return false;
}
}
return true;
}
use of org.codehaus.groovy.ast.ClassNode in project groovy by apache.
the class AnnotationVisitor method visitConstantExpression.
protected void visitConstantExpression(String attrName, ConstantExpression constExpr, ClassNode attrType) {
ClassNode constType = constExpr.getType();
ClassNode wrapperType = ClassHelper.getWrapper(constType);
if (!hasCompatibleType(attrType, wrapperType)) {
addError("Attribute '" + attrName + "' should have type '" + attrType.getName() + "'; but found type '" + constType.getName() + "'", constExpr);
}
}
use of org.codehaus.groovy.ast.ClassNode in project groovy by apache.
the class AnnotationVisitor method checkCircularReference.
public void checkCircularReference(ClassNode searchClass, ClassNode attrType, Expression startExp) {
if (!isValidAnnotationClass(attrType))
return;
if (!(startExp instanceof AnnotationConstantExpression)) {
addError("Found '" + startExp.getText() + "' when expecting an Annotation Constant", startExp);
return;
}
AnnotationConstantExpression ace = (AnnotationConstantExpression) startExp;
AnnotationNode annotationNode = (AnnotationNode) ace.getValue();
if (annotationNode.getClassNode().equals(searchClass)) {
addError("Circular reference discovered in " + searchClass.getName(), startExp);
return;
}
ClassNode cn = annotationNode.getClassNode();
for (MethodNode method : cn.getMethods()) {
if (method.getReturnType().equals(searchClass)) {
addError("Circular reference discovered in " + cn.getName(), startExp);
}
ReturnStatement code = (ReturnStatement) method.getCode();
if (code == null)
continue;
checkCircularReference(searchClass, method.getReturnType(), code.getExpression());
}
}
use of org.codehaus.groovy.ast.ClassNode in project groovy by apache.
the class AnnotationVisitor method visit.
public AnnotationNode visit(AnnotationNode node) {
this.annotation = node;
this.reportClass = node.getClassNode();
if (!isValidAnnotationClass(node.getClassNode())) {
addError("class " + node.getClassNode().getName() + " is not an annotation");
return node;
}
// check if values have been passed for all annotation attributes that don't have defaults
if (!checkIfMandatoryAnnotationValuesPassed(node)) {
return node;
}
// if enum constants have been used, check if they are all valid
if (!checkIfValidEnumConstsAreUsed(node)) {
return node;
}
Map<String, Expression> attributes = node.getMembers();
for (Map.Entry<String, Expression> entry : attributes.entrySet()) {
String attrName = entry.getKey();
Expression attrExpr = transformInlineConstants(entry.getValue());
entry.setValue(attrExpr);
ClassNode attrType = getAttributeType(node, attrName);
visitExpression(attrName, attrExpr, attrType);
}
VMPluginFactory.getPlugin().configureAnnotation(node);
return this.annotation;
}
use of org.codehaus.groovy.ast.ClassNode in project groovy by apache.
the class AnnotationVisitor method checkIfMandatoryAnnotationValuesPassed.
private boolean checkIfMandatoryAnnotationValuesPassed(AnnotationNode node) {
boolean ok = true;
Map attributes = node.getMembers();
ClassNode classNode = node.getClassNode();
for (MethodNode mn : classNode.getMethods()) {
String methodName = mn.getName();
// if the annotation attribute has a default, getCode() returns a ReturnStatement with the default value
if (mn.getCode() == null && !attributes.containsKey(methodName)) {
addError("No explicit/default value found for annotation attribute '" + methodName + "'", node);
ok = false;
}
}
return ok;
}
Aggregations