org.codehaus.groovy.ast.AnnotationNode类的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(14.4k)|赞(0)|评价(0)|浏览(124)

本文整理了Java中org.codehaus.groovy.ast.AnnotationNode类的一些代码示例,展示了AnnotationNode类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。AnnotationNode类的具体详情如下:
包路径:org.codehaus.groovy.ast.AnnotationNode
类名称:AnnotationNode

AnnotationNode介绍

[英]Represents an annotation which can be attached to interfaces, classes, methods and fields.
[中]表示可以附加到接口、类、方法和字段的注释。

代码示例

代码示例来源:origin: org.codehaus.groovy/groovy

ClassNode repeatable = null;
AnnotationNode repeatee = next.getValue().get(0);
List<AnnotationNode> repeateeAnnotations = repeatee.getClassNode().getAnnotations();
for (AnnotationNode anno : repeateeAnnotations) {
  ClassNode annoClassNode = anno.getClassNode();
  if (annoClassNode.getName().equals("java.lang.annotation.Repeatable")) {
    Expression value = anno.getMember("value");
    if (value instanceof ClassExpression) {
      ClassExpression ce = (ClassExpression) value;
      if (ce.getType() != null && ce.getType().isAnnotationDefinition()) {
        repeatable = ce.getType();
        break;
  AnnotationNode collector = new AnnotationNode(repeatable);
  collector.setRuntimeRetention(true); // checked earlier
  List<Expression> annos = new ArrayList<Expression>();
  for (AnnotationNode an : next.getValue()) {
    annos.add(new AnnotationConstantExpression(an));
  collector.addMember("value", new ListExpression(annos));
  node.addAnnotation(collector);
  node.getAnnotations().removeAll(next.getValue());

代码示例来源:origin: org.codehaus.groovy/groovy

private void extractGrab(Expression init, ConstantExpression ce) {
  if (ce.getValue() instanceof AnnotationNode) {
    AnnotationNode annotation = (AnnotationNode) ce.getValue();
    if ((init != null) && (annotation.getMember("initClass") != null)) {
      annotation.setMember("initClass", init);
    }
    String name = annotation.getClassNode().getName();
    if ((GRAB_CLASS_NAME.equals(name))
        || (allowShortGrab && GRAB_SHORT_NAME.equals(name))
        || (grabAliases.contains(name))) {
      grabAnnotations.add(annotation);
    }
    if ((GRABEXCLUDE_CLASS_NAME.equals(name))
        || (allowShortGrabExcludes && GRABEXCLUDE_SHORT_NAME.equals(name))
        || (grabExcludeAliases.contains(name))) {
      grabExcludeAnnotations.add(annotation);
    }
    if ((GRABCONFIG_CLASS_NAME.equals(name))
        || (allowShortGrabConfig && GRABCONFIG_SHORT_NAME.equals(name))
        || (grabConfigAliases.contains(name))) {
      grabConfigAnnotations.add(annotation);
    }
    if ((GRABRESOLVER_CLASS_NAME.equals(name))
        || (allowShortGrabResolver && GRABRESOLVER_SHORT_NAME.equals(name))
        || (grabResolverAliases.contains(name))) {
      grabResolverAnnotations.add(annotation);
    }
  }
}

代码示例来源:origin: org.codehaus.groovy/groovy

List<AnnotationNode> annotationList = annotatedNode.getAnnotations();
for (AnnotationNode annotation : annotationList)  {
  List<AnnotationNode> annotations = annotation.getClassNode().getAnnotations(AbstractASTTransformation.RETENTION_CLASSNODE);
  if (annotations.isEmpty()) continue;
  Expression valueExpression = retentionPolicyAnnotation.getMember("value");
  if (!(valueExpression instanceof PropertyExpression)) continue;
      propertyExpression.getProperty() instanceof ConstantExpression &&
              "RUNTIME".equals(((ConstantExpression) (propertyExpression.getProperty())).getValue()) ||
                  "CLASS".equals(((ConstantExpression) (propertyExpression.getProperty())).getValue())
          );
    AnnotationNode newAnnotation = new AnnotationNode(annotation.getClassNode());
    for (Map.Entry<String, Expression> member : annotation.getMembers().entrySet())  {
      newAnnotation.addMember(member.getKey(), member.getValue());
    newAnnotation.setSourcePosition(annotatedNode);

代码示例来源:origin: org.codehaus.groovy/groovy

private static void mergeParameters(AnnotationNode to, AnnotationNode from) {
  for (String name : from.getMembers().keySet()) {
    if (to.getMember(name) == null) {
      to.setMember(name, from.getMember(name));
    }
  }
}

代码示例来源:origin: org.codehaus.groovy/groovy

private static List<AnnotationNode> copy(List<AnnotationNode> orig, AnnotationNode aliasAnnotationUsage) {
  if (orig.isEmpty()) return orig;
  List<AnnotationNode> ret = new ArrayList<AnnotationNode>(orig.size());
  for (AnnotationNode an : orig) {
    AnnotationNode newAn = new AnnotationNode(an.getClassNode());
    copyMembers(an, newAn);
    newAn.setSourcePosition(aliasAnnotationUsage);
    ret.add(newAn);
  }
  return ret;
}

代码示例来源:origin: org.codehaus.groovy/groovy

public void visitAnnotations(AnnotatedNode node) {
  List<AnnotationNode> annotations = node.getAnnotations();
  if (annotations.isEmpty()) return;
  for (AnnotationNode an : annotations) {
    // skip built-in properties
    if (an.isBuiltIn()) continue;
    for (Map.Entry<String, Expression> member : an.getMembers().entrySet()) {
      member.setValue(transform(member.getValue()));
    }
  }
}

代码示例来源:origin: org.codehaus.groovy/groovy

for (ListIterator<AnnotationNode> it = cn.getAnnotations().listIterator(); it.hasNext(); ) {
  AnnotationNode an = it.next();
  if (an.getClassNode().getName().equals(AnnotationCollector.class.getName())) {
    collector = an;
    break;
final Expression member = collector.getMember("serializeClass");
if (member instanceof ClassExpression) {
  ClassExpression ce = (ClassExpression) member;
  legacySerialization = ce.getType().getName().equals(cn.getName());
      ACC_PUBLIC | ACC_STATIC | ACC_FINAL, ClassHelper.OBJECT_TYPE.getPlainNodeReference());
  cn.getModule().addClass(helper);
  helper.addAnnotation(new AnnotationNode(COMPILESTATIC_CLASSNODE));
  MethodNode serializeClass = collector.getClassNode().getMethod("serializeClass", Parameter.EMPTY_ARRAY);
  collector.setMember("serializeClass", new ClassExpression(helper.getPlainNodeReference()));
for (ListIterator<AnnotationNode> it = cn.getAnnotations().listIterator(); it.hasNext(); ) {
  AnnotationNode an = it.next();
  if (an == collector || "java.lang.annotation".equals(an.getClassNode().getPackageName())) {
    continue;

代码示例来源:origin: crashub/crash

private void handle(String name, AnnotatedNode annotated) {
  for (AnnotationNode ann : (List<AnnotationNode>)annotated.getAnnotations()) {
   if (ann.getClassNode().getName().endsWith(Argument.class.getName())) {
    Expression expr = ann.getMember("name");
    if (expr == null) {
     expr = new ConstantExpression(name);
     ann.setMember("name", expr);
    }
   }
  }
 }
}

代码示例来源:origin: stapler/stapler

/**
   * Captures the parameter names as annotations on the class.
   */
  private void write(MethodNode c) {
    ListExpression v = new ListExpression();
    for( Parameter p : c.getParameters() )
      v.addExpression(new ConstantExpression(p.getName()));

    AnnotationNode a = new AnnotationNode(new ClassNode(CapturedParameterNames.class));
    a.addMember("value",v);
    c.addAnnotation(a);
  }
}

代码示例来源:origin: org.codehaus.groovy/groovy

boolean noCastRequired = genericsSpec.isEmpty() || fixedReturnType.getName().equals(ClassHelper.VOID_TYPE.getName());
Expression forwardExpression = noCastRequired ? mce : new CastExpression(fixedReturnType,mce);
int access = helperMethod.getModifiers();
boolean isHelperForStaticMethod = helperMethodParams[0].getOriginType().equals(ClassHelper.CLASS_Type);
if (Modifier.isPrivate(access) && !isHelperForStaticMethod) {
AnnotationNode bridgeAnnotation = new AnnotationNode(Traits.TRAITBRIDGE_CLASSNODE);
bridgeAnnotation.addMember("traitClass", new ClassExpression(trait));
bridgeAnnotation.addMember("desc", new ConstantExpression(BytecodeHelper.getMethodDescriptor(helperMethod.getReturnType(), traitMethodParams)));
forwarder.addAnnotation(
    bridgeAnnotation

代码示例来源:origin: org.codehaus.groovy/groovy

public void visit(ASTNode[] nodes, SourceUnit source) {
  init(nodes, source);
  AnnotatedNode parent = (AnnotatedNode) nodes[1];
  AnnotationNode node = (AnnotationNode) nodes[0];
  boolean legacyMode = LEGACY_TYPE_NAME.equals(node.getClassNode().getName());
  if (!MY_TYPE.equals(node.getClassNode()) && !legacyMode) return;
  Expression value = node.getMember("value");
  if (parent instanceof ClassNode) {
    List<groovy.transform.PackageScopeTarget> targets;
    if (value == null) targets = Collections.singletonList(legacyMode ? PackageScopeTarget.FIELDS : PackageScopeTarget.CLASS);
    else targets = determineTargets(value);
    visitClassNode((ClassNode) parent, targets);
    parent.getAnnotations();
  } else {
    if (value != null) {
      addError("Error during " + MY_TYPE_NAME
          + " processing: " + TARGET_CLASS_NAME + " only allowed at class level.", parent);
      return;
    }
    if (parent instanceof MethodNode) {
      visitMethodNode((MethodNode) parent);
    } else if (parent instanceof FieldNode) {
      visitFieldNode((FieldNode) parent);
    }
  }
}

代码示例来源:origin: org.codehaus.groovy/groovy

public void visit(ASTNode[] nodes, SourceUnit source) {
  init(nodes, source);
  AnnotatedNode parent = (AnnotatedNode) nodes[1];
  AnnotationNode anno = (AnnotationNode) nodes[0];
  if (!MY_TYPE.equals(anno.getClassNode())) return;
  if (parent instanceof ClassNode) {
    ClassNode cNode = (ClassNode) parent;
    if (!checkNotInterface(cNode, MY_TYPE_NAME)) return;
    ClassNode exception = getMemberClassValue(anno, "exception");
    if (exception != null && Undefined.isUndefinedException(exception)) {
      exception = null;
    }
    String message = getMemberStringValue(anno, "message");
    Expression code = anno.getMember("code");
    if (code != null && !(code instanceof ClosureExpression)) {
      addError("Expected closure value for annotation parameter 'code'. Found " + code, cNode);
      return;
    }
    createMethods(cNode, exception, message, (ClosureExpression) code);
    if (code != null) {
      anno.setMember("code", new ClosureExpression(Parameter.EMPTY_ARRAY, EmptyStatement.INSTANCE));
    }
  }
}

代码示例来源:origin: org.codehaus.groovy/groovy

private static List<AnnotationNode> getTargetListFromAnnotations(ClassNode alias) {
  List<AnnotationNode> annotations = alias.getAnnotations();
  if (annotations.size() < 2) {
    return Collections.emptyList();
  }
  List<AnnotationNode> ret = new ArrayList<AnnotationNode>(annotations.size());
  for (AnnotationNode an : annotations) {
    ClassNode type = an.getClassNode();
    if (type.getName().equals(AnnotationCollector.class.getName()) || "java.lang.annotation".equals(type.getPackageName())) continue;
    AnnotationNode toAdd = new AnnotationNode(type);
    copyMembers(an, toAdd);
    ret.add(toAdd);
  }
  return ret;
}

代码示例来源:origin: org.codehaus.groovy/groovy

public void visitAnnotations(AnnotatedNode node) {
  List<AnnotationNode> annotations = node.getAnnotations();
  if (annotations.isEmpty()) return;
  Map<String, AnnotationNode> tmpAnnotations = new HashMap<String, AnnotationNode>();
  ClassNode annType;
  for (AnnotationNode an : annotations) {
    // skip built-in properties
    if (an.isBuiltIn()) continue;
    annType = an.getClassNode();
    resolveOrFail(annType, ",  unable to find class for annotation", an);
    for (Map.Entry<String, Expression> member : an.getMembers().entrySet()) {
      Expression newValue = transform(member.getValue());
      Expression adjusted = transformInlineConstants(newValue);
      member.setValue(adjusted);
      checkAnnotationMemberValue(adjusted);
    }
    if (annType.isResolved()) {
      Class annTypeClass = annType.getTypeClass();
      Retention retAnn = (Retention) annTypeClass.getAnnotation(Retention.class);
      if (retAnn != null && retAnn.value().equals(RetentionPolicy.RUNTIME) && !isRepeatable(annTypeClass)) {
        // remember runtime/non-repeatable annos (auto collecting of Repeatable annotations is handled elsewhere)
        AnnotationNode anyPrevAnnNode = tmpAnnotations.put(annTypeClass.getName(), an);
        if (anyPrevAnnNode != null) {
          addError("Cannot specify duplicate annotation on the same member : " + annType.getName(), an);
        }
      }
    }
  }
}

代码示例来源:origin: spockframework/spock

@Override
@SuppressWarnings("unchecked")
public void visitAnnotations(AnnotatedNode node) {
 for (AnnotationNode an : node.getAnnotations()) {
  ClassNode cn = an.getClassNode();
  // this comparison should be good enough, and also works in phase conversion
  if (cn.getNameWithoutPackage().equals(Inspect.class.getSimpleName())) {
   ConstantExpression name = (ConstantExpression)an.getMember("value");
   if (name == null || !(name.getValue() instanceof String))
    throw new AstInspectorException("@Inspect must have a String argument");
   addNode(markedNodes, (String)name.getValue(), node);
   break;
  }
 }
 super.visitAnnotations(node);
}

代码示例来源:origin: spockframework/spock

public static AnnotationNode getAnnotation(ASTNode node, Class<?> annotationType) {
 if (!(node instanceof AnnotatedNode)) return null;
 AnnotatedNode annotated = (AnnotatedNode)node;
 @SuppressWarnings("unchecked")
 List<AnnotationNode> annotations = annotated.getAnnotations();
 for (AnnotationNode a : annotations)
  if (a.getClassNode().getName().equals(annotationType.getName())) return a;
 return null;
}

代码示例来源:origin: org.codehaus.groovy/groovy

protected void visitAnnotations(AnnotatedNode node, int target) {
  if (node.getAnnotations().isEmpty()) {
    return;
  this.currentClass.setAnnotated(true);
  if (!isAnnotationCompatible()) {
    addError("Annotations are not supported in the current runtime. " + JVM_ERROR_MESSAGE, node);
  for (AnnotationNode unvisited : node.getAnnotations()) {
    AnnotationNode visited = visitAnnotation(unvisited);
    String name = visited.getClassNode().getName();
    if (visited.hasRuntimeRetention()) {
      List<AnnotationNode> seen = runtimeAnnotations.get(name);
      if (seen == null) {
    if (!isTargetAnnotation && !visited.isTargetAllowed(target)) {
      addError("Annotation @" + name + " is not allowed on element "
          + AnnotationNode.targetToName(target), visited);

代码示例来源:origin: org.codehaus.groovy/groovy

AnnotatedNode annotatedNode = (AnnotatedNode) nodes[1];
if (!type().equals(node.getClassNode())) {
  internalError("Transformation called from wrong annotation: " + node.getClassNode().getName());
} else if (!applyToAllMembers && annotatedNode instanceof MethodNode) {
  this.visitMethod((MethodNode) annotatedNode);
  this.visitClass(annotatedNode.getDeclaringClass());
} else if (!applyToAllMembers && annotatedNode instanceof FieldNode) {
  this.visitField((FieldNode) annotatedNode);
  this.visitClass(annotatedNode.getDeclaringClass());
} else if (!applyToAllMembers && annotatedNode instanceof DeclarationExpression) {
  this.visitDeclarationExpression((DeclarationExpression) annotatedNode);
  this.visitClass(annotatedNode.getDeclaringClass());
} else {
    final List<ClassNode> classes = tree.getClasses();
    for (ClassNode classNode : classes) {
      if (classNode.isScript()) {
        visitClass(classNode);

代码示例来源:origin: spockframework/spock

private void addSpecMetadata(Spec spec) {
 AnnotationNode ann = new AnnotationNode(nodeCache.SpecMetadata);
 String pathname = spec.getAst().getModule().getContext().getName();
 String filename = new File(pathname).getName();
 ann.setMember(SpecMetadata.FILENAME, new ConstantExpression(filename));
 ann.setMember(SpecMetadata.LINE, new ConstantExpression(spec.getAst().getLineNumber()));
 spec.getAst().addAnnotation(ann);
}

代码示例来源:origin: org.codehaus.groovy/groovy

private Expression serialize(AnnotationNode an) {
    MapExpression map = new MapExpression();
    for (String key : an.getMembers().keySet()) {
      map.addMapEntryExpression(new ConstantExpression(key), serialize(an.getMember(key)));
    }
    List<Expression> l = new ArrayList<Expression>(2);
    l.add(new ClassExpression(an.getClassNode()));
    l.add(map);
    return new ArrayExpression(ClassHelper.OBJECT_TYPE, l);
  }
}

相关文章