/** * Indicates whether a method in a trait interface has a default implementation. * @param method a method node * @return true if the method has a default implementation in the trait */ public static boolean hasDefaultImplementation(final MethodNode method) { return !method.getAnnotations(IMPLEMENTED_CLASSNODE).isEmpty(); }
private static boolean checkIsConditionBlock(MethodCallExpression methodCallExpr) { ClassNode targetType = methodCallExpr.getObjectExpression().getType(); String methodName = methodCallExpr.getMethodAsString(); List<MethodNode> methods = targetType.getMethods(methodName); for (MethodNode method : methods) { for (AnnotationNode annotation : method.getAnnotations()) { if (annotation.getClassNode().getName().equals(ConditionBlock.class.getName())) return true; } } return false; } }
Parameter[] types = metaMethod.getParameters(); if (metaMethod.isStatic() && metaMethod.isPublic() && types.length > 0 && metaMethod.getAnnotations(Deprecated_TYPE).isEmpty()) { Parameter[] parameters = new Parameter[types.length - 1]; System.arraycopy(types, 1, parameters, 0, parameters.length);
protected void addTypeCheckingInfoAnnotation(final MethodNode node) { // TypeChecked$TypeCheckingInfo can not be applied on constructors if (node instanceof ConstructorNode) return; // if a returned inferred type is available and no @TypeCheckingInfo is on node, then add an // annotation to the method node ClassNode rtype = getInferredReturnType(node); if (rtype != null && node.getAnnotations(TYPECHECKING_INFO_NODE).isEmpty()) { AnnotationNode anno = new AnnotationNode(TYPECHECKING_INFO_NODE); anno.setMember("version", CURRENT_SIGNATURE_PROTOCOL); SignatureCodec codec = SignatureCodecFactory.getCodec(CURRENT_SIGNATURE_PROTOCOL_VERSION, getTransformLoader()); String genericsSignature = codec.encode(rtype); if (genericsSignature != null) { ConstantExpression signature = new ConstantExpression(genericsSignature); signature.setType(STRING_TYPE); anno.setMember("inferredType", signature); node.addAnnotation(anno); } } }
private void visitOverride(AnnotatedNode node, AnnotationNode visited) { ClassNode annotationClassNode = visited.getClassNode(); if (annotationClassNode.isResolved() && annotationClassNode.getName().equals("java.lang.Override")) { if (node instanceof MethodNode && !Boolean.TRUE.equals(node.getNodeMetaData(Verifier.DEFAULT_PARAMETER_GENERATED))) { boolean override = false; MethodNode origMethod = (MethodNode) node; ClassNode cNode = origMethod.getDeclaringClass(); if (origMethod.hasDefaultValue()) { List<MethodNode> variants = cNode.getDeclaredMethods(origMethod.getName()); for (MethodNode m : variants) { if (m.getAnnotations().contains(visited) && isOverrideMethod(m)) { override = true; break; } } } else { override = isOverrideMethod(origMethod); } if (!override) { addError("Method '" + origMethod.getName() + "' from class '" + cNode.getName() + "' does not override " + "method from its superclass or interfaces but is annotated with @Override.", visited); } } } }
private static SyntaxException createException(ClassNode trait, ClassNode targetNode, MethodNode forwarder, MethodNode existingMethod) { String middle; ASTNode errorTarget; if (existingMethod.getLineNumber() == -1) { // came from a trait errorTarget = targetNode; List<AnnotationNode> allAnnos = existingMethod.getAnnotations(Traits.TRAITBRIDGE_CLASSNODE); AnnotationNode bridgeAnno = allAnnos == null ? null : allAnnos.get(0); String fromTrait = null; if (bridgeAnno != null) { Expression traitClass = bridgeAnno.getMember("traitClass"); if (traitClass instanceof ClassExpression) { ClassExpression ce = (ClassExpression) traitClass; fromTrait = ce.getType().getNameWithoutPackage(); } } middle = "in '" + targetNode.getNameWithoutPackage(); if (fromTrait != null) { middle += "' from trait '" + fromTrait; } } else { errorTarget = existingMethod; middle = "declared in '" + targetNode.getNameWithoutPackage(); } String message = "The static '" + forwarder.getName() + "' method " + middle + "' conflicts with the instance method having the same signature from trait '" + trait.getNameWithoutPackage() + "'"; return new SyntaxException(message, errorTarget); }
private static MethodNode buildDelegatingMethod(final MethodNode annotatedMethod, final ClassNode ownerClassNode) { Statement code = annotatedMethod.getCode(); int access = ACC_PROTECTED; if (annotatedMethod.isStatic()) { access = ACC_PRIVATE | ACC_STATIC; } MethodNode method = new MethodNode( buildUniqueName(ownerClassNode, METHOD_LABEL, annotatedMethod), access, annotatedMethod.getReturnType(), cloneParams(annotatedMethod.getParameters()), annotatedMethod.getExceptions(), code ); method.addAnnotations(filterAnnotations(annotatedMethod.getAnnotations())); return method; }
private MethodNode copyMethod(MethodNode method, String newName) { // can't hurt to set return type to void MethodNode newMethod = new MethodNode(newName, method.getModifiers(), ClassHelper.VOID_TYPE, method.getParameters(), method.getExceptions(), method.getCode()); newMethod.addAnnotations(method.getAnnotations()); newMethod.setSynthetic(method.isSynthetic()); newMethod.setDeclaringClass(method.getDeclaringClass()); newMethod.setSourcePosition(method); newMethod.setVariableScope(method.getVariableScope()); newMethod.setGenericsTypes(method.getGenericsTypes()); newMethod.setAnnotationDefault(method.hasAnnotationDefault()); return newMethod; }
return; if (!candidate.getAnnotations(DEPRECATED_TYPE).isEmpty() && !includeDeprecated) return;
private MethodNode processMethod(ClassNode traitClass, ClassNode traitHelperClass, MethodNode methodNode, ClassNode fieldHelper, Collection<String> knownFields) { Parameter[] initialParams = methodNode.getParameters(); Parameter[] newParams = new Parameter[initialParams.length + 1]; newParams[0] = createSelfParameter(traitClass, methodNode.isStatic()); System.arraycopy(initialParams, 0, newParams, 1, initialParams.length); final int mod = methodNode.isPrivate() ? ACC_PRIVATE : ACC_PUBLIC | (methodNode.isFinal() ? ACC_FINAL : 0); MethodNode mNode = new MethodNode( methodNode.getName(), mod | ACC_STATIC, methodNode.getReturnType(), newParams, methodNode.getExceptions(), processBody(new VariableExpression(newParams[0]), methodNode.getCode(), traitClass, traitHelperClass, fieldHelper, knownFields) ); mNode.setSourcePosition(methodNode); mNode.addAnnotations(filterAnnotations(methodNode.getAnnotations())); mNode.setGenericsTypes(methodNode.getGenericsTypes()); if (methodNode.isAbstract()) { mNode.setModifiers(ACC_PUBLIC | ACC_ABSTRACT); } else { methodNode.addAnnotation(new AnnotationNode(Traits.IMPLEMENTED_CLASSNODE)); } methodNode.setCode(null); if (!methodNode.isPrivate() && !methodNode.isStatic()) { methodNode.setModifiers(ACC_PUBLIC | ACC_ABSTRACT); } return mNode; }
List<AnnotationNode> annotations = method.getAnnotations(); if (annotations != null) { newMethod.addAnnotations(annotations);
public void visit(ASTNode[] nodes, final SourceUnit source) { for (ClassNode classNode : source.getAST().getClasses()) { out: for (MethodNode method : classNode.getMethods()) { for (AnnotationNode ann : method.getAnnotations()) { if (ann.getClassNode().getName().equals(Command.class.getName())) { ClassNode superClass = classNode.getSuperClass(); if (superClass == null || superClass.getName().equals("java.lang.Object")) { classNode.setSuperClass(CRASH_COMMAND); } break out; } } } } } }
for (AnnotationNode annotation : main.getAnnotations()) { if (annotation.getClassNode().getName().equals(Usage.class.getSimpleName())) { resolveDescription = annotation.getMember("value").getText();
public static Threading.Policy getThreadingPolicy(MethodNode method, Threading.Policy defaultPolicy) { List<AnnotationNode> annotations = method.getAnnotations(THREADING_CNODE); if (!annotations.isEmpty()) { return getThreadingPolicy(annotations.get(0)); } return defaultPolicy; }
boolean hasAnnotation(MethodNode node, Class<? extends Annotation> a) { for (AnnotationNode ann : node.getAnnotations()) { if (ann.getClassNode().getName().equals(a.getName())) { return true; } } return false; }
protected void autoAnnotateSetupTeardown(ClassNode classNode) { MethodNode setupMethod = classNode.getDeclaredMethod(SET_UP_METHOD, GrailsArtefactClassInjector.ZERO_PARAMETERS); if ( setupMethod != null && setupMethod.getAnnotations(TestForTransformation.BEFORE_CLASS_NODE).size() == 0) { setupMethod.addAnnotation(TestForTransformation.BEFORE_ANNOTATION); } MethodNode tearDown = classNode.getDeclaredMethod(TEAR_DOWN_METHOD, GrailsArtefactClassInjector.ZERO_PARAMETERS); if ( tearDown != null && tearDown.getAnnotations(TestForTransformation.AFTER_CLASS_NODE).size() == 0) { tearDown.addAnnotation(TestForTransformation.AFTER_ANNOTATION); } } }
public static boolean isChromatticAnnoted(MethodNode methodNode) { if (methodNode == null) return false; for (AnnotationNode annotationNode : (List<AnnotationNode>) methodNode.getAnnotations()) { if (annotationNode.getClassNode().getName().startsWith(GroovyUtils.ANNOTATIONS_PACKAGE)) return true; } return false; }
private void inspectInterface(ClassNode cn, Set<ClassNode> crawled) { List<MethodNode> apiMethods = cn.getAllDeclaredMethods(); for(MethodNode apiMethod: apiMethods) { List<AnnotationNode> skipNodes = apiMethod.getAnnotations(new ClassNode(SkipStatistics.class)); if(skipNodes !=null && !skipNodes.isEmpty()) { MethodSignature ms = new MethodSignature(apiMethod, false); apiSkipStats.add(ms); } } if(!crawled.contains(cn)) { crawlParents(cn, crawled); } }
private BlockStatement getJunit4Setup(ClassNode classNode) { MethodNode setupMethod = classNode.getDeclaredMethod(SET_UP_METHOD, GrailsArtefactClassInjector.ZERO_PARAMETERS); if (setupMethod == null) { setupMethod = new MethodNode(SET_UP_METHOD,Modifier.PUBLIC,ClassHelper.VOID_TYPE,GrailsArtefactClassInjector.ZERO_PARAMETERS,null,new BlockStatement()); setupMethod.addAnnotation(MIXIN_METHOD_ANNOTATION); classNode.addMethod(setupMethod); } if (setupMethod.getAnnotations(BEFORE_CLASS_NODE).size() == 0) { setupMethod.addAnnotation(BEFORE_ANNOTATION); } return getOrCreateMethodBody(classNode, setupMethod, SET_UP_METHOD); }
private void addMethodOccurrences(MethodNode visitedMethod, ClassNode findingNode) { // Check return type addOccurrences(visitedMethod.getReturnType(), findingNode); // Check method parameters for (Parameter parameter : visitedMethod.getParameters()) { addOccurrences(parameter.getType(), findingNode); } // Check annotations for (AnnotationNode annotation : visitedMethod.getAnnotations(findingNode)) { addAnnotationOccurrences(annotation, findingNode); } }