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; }
private static List<AnnotationNode> makeListOfAnnotations(Object[][] data) { if (data.length == 0) { return Collections.emptyList(); } List<AnnotationNode> ret = new ArrayList<AnnotationNode>(data.length); for (Object[] inner : data) { Class<?> anno = (Class) inner[0]; AnnotationNode toAdd = new AnnotationNode(ClassHelper.make(anno)); ret.add(toAdd); @SuppressWarnings("unchecked") Map<String,Object> member = (Map<String, Object>) inner[1]; if (member.isEmpty()) { continue; } Map<String, Expression> generated = new HashMap<String, Expression>(member.size()); for (Map.Entry<String, Object> entry : member.entrySet()) { generated.put(entry.getKey(), makeExpression(entry.getValue())); } copyMembers(generated, toAdd); } return ret; }
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; }
private void setAnnotationMetaData(Annotation[] annotations, AnnotatedNode an) { for (Annotation annotation : annotations) { AnnotationNode node = new AnnotationNode(ClassHelper.make(annotation.annotationType())); configureAnnotation(node, annotation); an.addAnnotation(node); } }
private List<AnnotationNode> getTargetListFromValue(AnnotationNode collector, AnnotationNode aliasAnnotationUsage, SourceUnit source) { Expression memberValue = collector.getMember("value"); if (memberValue == null) { return Collections.emptyList(); } if (!(memberValue instanceof ListExpression)) { addError("Annotation collector expected a list of classes, but got a "+memberValue.getClass(), collector, source); return Collections.emptyList(); } ListExpression memberListExp = (ListExpression) memberValue; List<Expression> memberList = memberListExp.getExpressions(); if (memberList.isEmpty()) { return Collections.emptyList(); } List<AnnotationNode> ret = new ArrayList<AnnotationNode>(); for (Expression e : memberList) { AnnotationNode toAdd = new AnnotationNode(e.getType()); toAdd.setSourcePosition(aliasAnnotationUsage); ret.add(toAdd); } return ret; }
private AnnotationNode createDataProviderAnnotation(Expression dataProviderExpr, int nextDataVariableIndex) { AnnotationNode ann = new AnnotationNode(resources.getAstNodeCache().DataProviderMetadata); ann.addMember(DataProviderMetadata.LINE, new ConstantExpression(dataProviderExpr.getLineNumber())); List<Expression> dataVariableNames = new ArrayList<>(); for (int i = nextDataVariableIndex; i < dataProcessorVars.size(); i++) dataVariableNames.add(new ConstantExpression(dataProcessorVars.get(i).getName())); ann.addMember(DataProviderMetadata.DATA_VARIABLES, new ListExpression(dataVariableNames)); return ann; }
public List<AnnotationNode> visit(AnnotationNode collector, AnnotationNode aliasAnnotationUsage, AnnotatedNode aliasAnnotated, SourceUnit source) { AnnotationNode node = new AnnotationNode(COMPILESTATIC_NODE); node.addMember("value", new PropertyExpression(new ClassExpression(TYPECHECKINGMODE_NODE), "SKIP")); return Collections.singletonList(node); } }
public static void markAsGenerated(ClassNode containingClass, AnnotatedNode nodeToMark) { boolean shouldAnnotate = containingClass.getModule() != null && containingClass.getModule().getContext() != null; if (shouldAnnotate && !hasAnnotation(nodeToMark, GENERATED_TYPE)) { nodeToMark.addAnnotation(new AnnotationNode(GENERATED_TYPE)); } }
AnnotationNode collector = new AnnotationNode(repeatable); collector.setRuntimeRetention(true); // checked earlier List<Expression> annos = new ArrayList<Expression>();
AnnotationNode newAnnotation = new AnnotationNode(annotation.getClassNode()); for (Map.Entry<String, Expression> member : annotation.getMembers().entrySet()) { newAnnotation.addMember(member.getKey(), member.getValue());
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); }
private void setScriptBaseClassFromConfig(ClassNode cn) { String baseClassName = null; if (unit != null) { baseClassName = unit.getConfig().getScriptBaseClass(); } else if (context != null) { baseClassName = context.getConfiguration().getScriptBaseClass(); } if (baseClassName != null) { if (!cn.getSuperClass().getName().equals(baseClassName)) { cn.setSuperClass(ClassHelper.make(baseClassName)); AnnotationNode annotationNode = new AnnotationNode(BaseScriptASTTransformation.MY_TYPE); cn.addAnnotation(annotationNode); } } }
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 addBlockMetadata(Block block, BlockKind kind) { AnnotationNode blockAnn = new AnnotationNode(nodeCache.BlockMetadata); blockAnn.setMember(BlockMetadata.KIND, new PropertyExpression( new ClassExpression(nodeCache.BlockKind), kind.name())); ListExpression textExprs = new ListExpression(); for (String text : block.getDescriptions()) textExprs.addExpression(new ConstantExpression(text)); blockAnn.setMember(BlockMetadata.TEXTS, textExprs); blockAnnElems.addExpression(new AnnotationConstantExpression(blockAnn)); }
private boolean processDelegateParam(MethodNode mNode, Parameter mapParam, ArgumentListExpression args, List<String> propNames, Parameter fromParam) { if (isInnerClass(fromParam.getType())) { if (mNode.isStatic()) { addError("Error during " + MY_TYPE_NAME + " processing. Delegate type '" + fromParam.getType().getNameWithoutPackage() + "' is an inner class which is not supported.", mNode); return false; } } Set<String> names = new HashSet<String>(); List<PropertyNode> props = getAllProperties(names, fromParam.getType(), true, false, false, true, false, true); for (String next : names) { if (hasDuplicates(mNode, propNames, next)) return false; } List<MapEntryExpression> entries = new ArrayList<MapEntryExpression>(); for (PropertyNode pNode : props) { String name = pNode.getName(); entries.add(new MapEntryExpression(constX(name), propX(varX(mapParam), name))); AnnotationNode namedParam = new AnnotationNode(NAMED_PARAM_TYPE); namedParam.addMember("value", constX(name)); namedParam.addMember("type", classX(pNode.getType())); mapParam.addAnnotation(namedParam); } Expression delegateMap = new MapExpression(entries); args.addExpression(castX(fromParam.getType(), delegateMap)); return true; }
private void addFieldMetadata(Field field) { AnnotationNode ann = new AnnotationNode(nodeCache.FieldMetadata); ann.setMember(FieldMetadata.NAME, new ConstantExpression(field.getName())); ann.setMember(FieldMetadata.ORDINAL, new ConstantExpression(field.getOrdinal())); ann.setMember(FieldMetadata.LINE, new ConstantExpression(field.getAst().getLineNumber())); ann.setMember(FieldMetadata.INITIALIZER, new ConstantExpression(field.hasInitialExpression())); field.getAst().addAnnotation(ann); }
private boolean processImplicitNamedParam(MethodNode mNode, Parameter mapParam, ArgumentListExpression args, List<String> propNames, Parameter fromParam) { boolean required = fromParam.hasInitialExpression(); String name = fromParam.getName(); if (hasDuplicates(mNode, propNames, name)) return false; AnnotationNode namedParam = new AnnotationNode(NAMED_PARAM_TYPE); namedParam.addMember("value", constX(name)); namedParam.addMember("type", classX(fromParam.getType())); namedParam.addMember("required", constX(required, true)); mapParam.addAnnotation(namedParam); args.addExpression(propX(varX(mapParam), name)); return true; }
protected AnnotationNode annotation(AST annotationNode) { annotationBeingDef = true; AST node = annotationNode.getFirstChild(); String name = qualifiedName(node); AnnotationNode annotatedNode = new AnnotationNode(ClassHelper.make(name)); configureAST(annotatedNode, annotationNode); while (true) { node = node.getNextSibling(); if (isType(ANNOTATION_MEMBER_VALUE_PAIR, node)) { AST memberNode = node.getFirstChild(); String param = identifier(memberNode); Expression expression = expression(memberNode.getNextSibling()); if (annotatedNode.getMember(param) != null) { throw new ASTRuntimeException(memberNode, "Annotation member '" + param + "' has already been associated with a value"); } annotatedNode.setMember(param, expression); } else { break; } } annotationBeingDef = false; return annotatedNode; }
private void addFeatureMetadata(FeatureMethod feature) { AnnotationNode ann = new AnnotationNode(nodeCache.FeatureMetadata); ann.setMember(FeatureMetadata.NAME, new ConstantExpression(feature.getName())); ann.setMember(FeatureMetadata.ORDINAL, new ConstantExpression(feature.getOrdinal())); ann.setMember(FeatureMetadata.LINE, new ConstantExpression(feature.getAst().getLineNumber())); ann.setMember(FeatureMetadata.BLOCKS, blockAnnElems = new ListExpression()); ListExpression paramNames = new ListExpression(); for (Parameter param : feature.getAst().getParameters()) paramNames.addExpression(new ConstantExpression(param.getName())); ann.setMember(FeatureMetadata.PARAMETER_NAMES, paramNames); feature.getAst().addAnnotation(ann); }
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; }