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); }
fieldNode.addAnnotation(annotation);
public static FieldExpression injectedField(@Nonnull ClassNode owner, @Nonnull ClassNode type, @Nonnull String name, @Nullable String qualifierName) { FieldNode fieldNode = GriffonASTUtils.injectField(owner, name, Modifier.PRIVATE, type, null, false); List<AnnotationNode> annotations = fieldNode.getAnnotations(INJECT_TYPE); if (annotations == null || annotations.isEmpty()) { fieldNode.addAnnotation(new AnnotationNode(INJECT_TYPE)); } if (isNotBlank(qualifierName)) { AnnotationNode namedAnnotation = new AnnotationNode(NAMED_TYPE); namedAnnotation.addMember("value", new ConstantExpression(qualifierName)); fieldNode.addAnnotation(namedAnnotation); } return new FieldExpression(fieldNode); }
@Nonnull public static FieldExpression injectedField(@Nonnull ClassNode owner, @Nonnull ClassNode type, @Nonnull String name, @Nullable String qualifierName) { FieldNode fieldNode = GriffonASTUtils.injectField(owner, name, Modifier.PRIVATE, type, null, false); if (fieldNode.getAnnotations(INJECT_TYPE) == null) { fieldNode.addAnnotation(new AnnotationNode(INJECT_TYPE)); } if (isNotBlank(qualifierName) && fieldNode.getAnnotations(NAMED_TYPE) == null) { AnnotationNode namedAnnotation = new AnnotationNode(NAMED_TYPE); namedAnnotation.addMember("value", new ConstantExpression(qualifierName)); fieldNode.addAnnotation(namedAnnotation); } return new FieldExpression(fieldNode); } }
private void addSpockFieldMetadata(FieldNode field, int ordinal) { AnnotationNode ann = new AnnotationNode(ClassHelper.make(FieldMetadata.class)); ann.setMember(FieldMetadata.NAME, new ConstantExpression(field.getName())); ann.setMember(FieldMetadata.ORDINAL, new ConstantExpression(ordinal)); ann.setMember(FieldMetadata.LINE, new ConstantExpression(field.getLineNumber())); field.addAnnotation(ann); }
protected void addJunitRuleFields(ClassNode classNode) { if(classNode.getField(JUNIT_ADAPTER_FIELD_NAME) != null) { return; } ClassNode junitAdapterType = ClassHelper.make(TestRuntimeJunitAdapter.class); FieldNode junitAdapterFieldNode = classNode.addField(JUNIT_ADAPTER_FIELD_NAME, Modifier.STATIC, junitAdapterType, new ConstructorCallExpression(junitAdapterType, MethodCallExpression.NO_ARGUMENTS)); boolean spockTest = isSpockTest(classNode); FieldNode staticRuleFieldNode = classNode.addField(RULE_FIELD_NAME_BASE + "StaticClassRule", Modifier.PRIVATE | Modifier.STATIC, ClassHelper.make(TestRule.class), new MethodCallExpression(new FieldExpression(junitAdapterFieldNode), "newClassRule", new ClassExpression(classNode))); AnnotationNode classRuleAnnotation = new AnnotationNode(ClassHelper.make(ClassRule.class)); if(spockTest) { // @ClassRule must be added to @Shared field in spock FieldNode spockSharedRuleFieldNode = classNode.addField(RULE_FIELD_NAME_BASE + "SharedClassRule", Modifier.PUBLIC, ClassHelper.make(TestRule.class), new FieldExpression(staticRuleFieldNode)); spockSharedRuleFieldNode.addAnnotation(classRuleAnnotation); spockSharedRuleFieldNode.addAnnotation(new AnnotationNode(ClassHelper.make(Shared.class))); addSpockFieldMetadata(spockSharedRuleFieldNode, 0); } else { staticRuleFieldNode.setModifiers(Modifier.PUBLIC | Modifier.STATIC); staticRuleFieldNode.addAnnotation(classRuleAnnotation); } FieldNode ruleFieldNode = classNode.addField(RULE_FIELD_NAME_BASE + "Rule", Modifier.PUBLIC, ClassHelper.make(TestRule.class), new MethodCallExpression(new FieldExpression(junitAdapterFieldNode), "newRule", new VariableExpression("this"))); ruleFieldNode.addAnnotation(new AnnotationNode(ClassHelper.make(Rule.class))); if(spockTest) { addSpockFieldMetadata(ruleFieldNode, 0); } }
List<AnnotationNode> annotations = fieldNode.getAnnotations(INJECT_TYPE); if (annotations == null || annotations.isEmpty()) { fieldNode.addAnnotation(new AnnotationNode(INJECT_TYPE));
protected FieldNode addTestRuntimeAwareMixinFieldIfNonExistent(ClassNode classNode, ClassNode fieldType, String fieldName) { if (classNode == null || classNode.getField(fieldName) != null) { return null; } MapExpression constructorArguments = new MapExpression(); constructorArguments.addMapEntryExpression(new MapEntryExpression(new ConstantExpression("testClass"), new ClassExpression(classNode))); FieldNode mixinInstanceFieldNode = classNode.addField(fieldName, Modifier.STATIC, fieldType, new ConstructorCallExpression(fieldType, constructorArguments)); mixinInstanceFieldNode.addAnnotation(new AnnotationNode(ClassHelper.make(MixinInstance.class))); addJunitRuleFields(classNode); return mixinInstanceFieldNode; }
final ClassNode annotationClassNode = annotation.getClassNode(); if (annotationClassNode.getAnnotations(ASTTRANSFORM_TYPE).isEmpty()) { fieldNode.addAnnotation(annotation);