protected void addError(String msg) { addError(msg, this.annotation); }
private ConstantExpression getConstantExpression(Expression exp, ClassNode attrType) { Expression result = exp; if (!(result instanceof ConstantExpression)) { result = transformInlineConstants(result, attrType); } if (result instanceof ConstantExpression) { return (ConstantExpression) result; } String base = "Expected '" + exp.getText() + "' to be an inline constant of type " + attrType.getName(); if (exp instanceof PropertyExpression) { addError(base + " not a property expression", exp); } else if (exp instanceof VariableExpression && ((VariableExpression)exp).getAccessedVariable() instanceof FieldNode) { addError(base + " not a field expression", exp); } else { addError(base, exp); } return ConstantExpression.EMPTY_EXPRESSION; }
private ClassNode getAttributeType(AnnotationNode node, String attrName) { ClassNode classNode = node.getClassNode(); List methods = classNode.getMethods(attrName); // if size is >1, then the method was overwritten or something, we ignore that // if it is an error, we have to test it at another place. But size==0 is // an error, because it means that no such attribute exists. if (methods.isEmpty()) { addError("'" + attrName + "'is not part of the annotation " + classNode, node); return ClassHelper.OBJECT_TYPE; } MethodNode method = (MethodNode) methods.get(0); return method.getReturnType(); }
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; }
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); } }
protected void visitEnumExpression(String attrName, PropertyExpression propExpr, ClassNode attrType) { if (!propExpr.getObjectExpression().getType().isDerivedFrom(attrType)) { addError("Attribute '" + attrName + "' should have type '" + attrType.getName() + "' (Enum), but found " + propExpr.getObjectExpression().getType().getName(), propExpr); } }
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(); ClassNode attrType = getAttributeType(node, attrName); Expression attrExpr = transformInlineConstants(entry.getValue(), attrType); entry.setValue(attrExpr); visitExpression(attrName, attrExpr, attrType); } VMPluginFactory.getPlugin().configureAnnotation(node); return this.annotation; }
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()); } }
visitListExpression(attrName, le, attrType.getComponentType()); } else if (attrExp instanceof ClosureExpression) { addError("Annotation list attributes must use Groovy notation [el1, el2]", attrExp); } else { } else if (ClassHelper.CLASS_Type.equals(attrType)) { if (!(attrExp instanceof ClassExpression || attrExp instanceof ClosureExpression)) { addError("Only classes and closures can be used for attribute '" + attrName + "'", attrExp); visitEnumExpression(attrName, (PropertyExpression) attrExp, attrType); } else { addError("Expected enum value for attribute " + attrName, attrExp); visitAnnotationExpression(attrName, (AnnotationConstantExpression) attrExp, attrType); } else { addError("Expected annotation of type '" + attrType.getName() + "' for attribute " + attrName, attrExp); addError("Unexpected type " + attrType.getName(), attrExp);
public void checkReturnType(ClassNode attrType, ASTNode node) { if (attrType.isArray()) { checkReturnType(attrType.getComponentType(), node); } else if (ClassHelper.isPrimitiveType(attrType)) { } else if (ClassHelper.STRING_TYPE.equals(attrType)) { } else if (ClassHelper.CLASS_Type.equals(attrType)) { } else if (attrType.isDerivedFrom(ClassHelper.Enum_Type)) { } else if (isValidAnnotationClass(attrType)) { } else { addError("Unexpected return type " + attrType.getName(), node); } }
private boolean validateEnumConstant(Expression exp) { if (exp instanceof PropertyExpression) { PropertyExpression pe = (PropertyExpression) exp; String name = pe.getPropertyAsString(); if (pe.getObjectExpression() instanceof ClassExpression && name != null) { ClassExpression ce = (ClassExpression) pe.getObjectExpression(); ClassNode type = ce.getType(); if (type.isEnum()) { boolean ok = false; try { FieldNode enumField = type.getDeclaredField(name); ok = enumField != null && enumField.getType().equals(type); } catch(Exception ex) { // ignore } if(!ok) { addError("No enum const " + type.getName() + "." + name, pe); return false; } } } } return true; }
private ConstantExpression getConstantExpression(Expression exp) { if (exp instanceof ConstantExpression) { return (ConstantExpression) exp; } else { addError("expected a constant",exp); return ConstantExpression.EMTPY_EXPRESSION; } }
private ClassNode getAttributeType(AnnotationNode node, String attrName) { ClassNode classNode = node.getClassNode(); List methods = classNode.getMethods(attrName); // if size is >1, then the method was overwritten or something, we ignore that // if it is an error, we have to test it at another place. But size==0 is // an error, because it means that no such attribute exists. if (methods.size() == 0) { addError("'" + attrName + "'is not part of the annotation " + classNode, node); return ClassHelper.OBJECT_TYPE; } MethodNode method = (MethodNode) methods.get(0); return method.getReturnType(); }
protected void visitConstantExpression(String attrName, ConstantExpression constExpr, ClassNode attrType) { if(!constExpr.getType().isDerivedFrom(attrType)) { addError("Attribute '" + attrName + "' should have type '" + attrType.getName() + "'; " + "but found type '" + constExpr.getType().getName() + "'", constExpr); } }
protected void visitConstantExpression(String attrName, ConstantExpression constExpr, ClassNode attrType) { if(!constExpr.getType().isDerivedFrom(attrType)) { addError("Attribute '" + attrName + "' should have type '" + attrType.getName() + "'; " + "but found type '" + constExpr.getType().getName() + "'", constExpr); } }
protected void visitConstantExpression(String attrName, ConstantExpression constExpr, ClassNode attrType) { if(!constExpr.getType().isDerivedFrom(attrType)) { addError("Attribute '" + attrName + "' should have type '" + attrType.getName() + "'; " + "but found type '" + constExpr.getType().getName() + "'", constExpr); } }
protected void visitConstantExpression(String attrName, ConstantExpression constExpr, ClassNode attrType) { ClassNode type = ClassHelper.getWrapper(constExpr.getType()); if (!type.isDerivedFrom(attrType)) { addError("Attribute '" + attrName + "' should have type '" + attrType.getName() + "'; " + "but found type '" + constExpr.getType().getName() + "'", constExpr); } }
protected void visitEnumExpression(String attrName, PropertyExpression propExpr, ClassNode attrType) { if (!propExpr.getObjectExpression().getType().isDerivedFrom(attrType)) { addError("Attribute '" + attrName + "' should have type '" + attrType.getName() + "' (Enum), but found " + propExpr.getObjectExpression().getType().getName(), propExpr); } }
protected void visitEnumExpression(String attrName, PropertyExpression propExpr, ClassNode attrType) { if(!propExpr.getObjectExpression().getType().isDerivedFrom(attrType)) { addError("Attribute '" + attrName + "' should have type '" + attrType.getName() +"' (Enum), but found " + propExpr.getObjectExpression().getType().getName(), propExpr); } }
protected void visitEnumExpression(String attrName, PropertyExpression propExpr, ClassNode attrType) { if(!propExpr.getObjectExpression().getType().isDerivedFrom(attrType)) { addError("Attribute '" + attrName + "' should have type '" + attrType.getName() +"' (Enum), but found " + propExpr.getObjectExpression().getType().getName(), propExpr); } }