use of org.codehaus.groovy.ast.FieldNode in project groovy by apache.
the class MemoizedASTTransformation method visit.
public void visit(ASTNode[] nodes, final SourceUnit source) {
init(nodes, source);
AnnotationNode annotationNode = (AnnotationNode) nodes[0];
AnnotatedNode annotatedNode = (AnnotatedNode) nodes[1];
if (MY_TYPE.equals(annotationNode.getClassNode()) && annotatedNode instanceof MethodNode) {
MethodNode methodNode = (MethodNode) annotatedNode;
if (methodNode.isAbstract()) {
addError("Annotation " + MY_TYPE_NAME + " cannot be used for abstract methods.", methodNode);
return;
}
if (methodNode.isVoidMethod()) {
addError("Annotation " + MY_TYPE_NAME + " cannot be used for void methods.", methodNode);
return;
}
ClassNode ownerClassNode = methodNode.getDeclaringClass();
MethodNode delegatingMethod = buildDelegatingMethod(methodNode, ownerClassNode);
ownerClassNode.addMethod(delegatingMethod);
int modifiers = FieldNode.ACC_PRIVATE | FieldNode.ACC_FINAL;
if (methodNode.isStatic()) {
modifiers = modifiers | FieldNode.ACC_STATIC;
}
int protectedCacheSize = getMemberIntValue(annotationNode, PROTECTED_CACHE_SIZE_NAME);
int maxCacheSize = getMemberIntValue(annotationNode, MAX_CACHE_SIZE_NAME);
MethodCallExpression memoizeClosureCallExpression = buildMemoizeClosureCallExpression(delegatingMethod, protectedCacheSize, maxCacheSize);
String memoizedClosureFieldName = buildUniqueName(ownerClassNode, CLOSURE_LABEL, methodNode);
FieldNode memoizedClosureField = new FieldNode(memoizedClosureFieldName, modifiers, newClass(ClassHelper.CLOSURE_TYPE), null, memoizeClosureCallExpression);
ownerClassNode.addField(memoizedClosureField);
BlockStatement newCode = new BlockStatement();
MethodCallExpression closureCallExpression = callX(fieldX(memoizedClosureField), CLOSURE_CALL_METHOD_NAME, args(methodNode.getParameters()));
closureCallExpression.setImplicitThis(false);
newCode.addStatement(returnS(closureCallExpression));
methodNode.setCode(newCode);
VariableScopeVisitor visitor = new VariableScopeVisitor(source);
visitor.visitClass(ownerClassNode);
}
}
use of org.codehaus.groovy.ast.FieldNode in project groovy by apache.
the class SingletonASTTransformation method createField.
private void createField(ClassNode classNode, String propertyName, boolean isLazy, boolean isStrict) {
int modifiers = isLazy ? ACC_PRIVATE | ACC_STATIC | ACC_VOLATILE : ACC_PUBLIC | ACC_FINAL | ACC_STATIC;
Expression initialValue = isLazy ? null : ctorX(classNode);
final FieldNode fieldNode = classNode.addField(propertyName, modifiers, newClass(classNode), initialValue);
createConstructor(classNode, fieldNode, propertyName, isStrict);
final BlockStatement body = new BlockStatement();
body.addStatement(isLazy ? lazyBody(classNode, fieldNode) : nonLazyBody(fieldNode));
classNode.addMethod(getGetterName(propertyName), ACC_STATIC | ACC_PUBLIC, newClass(classNode), Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, body);
}
use of org.codehaus.groovy.ast.FieldNode in project groovy by apache.
the class SourceURIASTTransformation method visit.
public void visit(ASTNode[] nodes, SourceUnit source) {
init(nodes, source);
AnnotatedNode parent = (AnnotatedNode) nodes[1];
AnnotationNode node = (AnnotationNode) nodes[0];
if (!MY_TYPE.equals(node.getClassNode()))
return;
if (parent instanceof DeclarationExpression) {
setScriptURIOnDeclaration((DeclarationExpression) parent, node);
} else if (parent instanceof FieldNode) {
setScriptURIOnField((FieldNode) parent, node);
} else {
addError("Expected to find the annotation " + MY_TYPE_NAME + " on an declaration statement.", parent);
}
}
use of org.codehaus.groovy.ast.FieldNode in project groovy by apache.
the class ImmutableASTTransformation method createConstructorStatement.
private Statement createConstructorStatement(ClassNode cNode, PropertyNode pNode, List<String> knownImmutableClasses, List<String> knownImmutables) {
FieldNode fNode = pNode.getField();
final ClassNode fieldType = fNode.getType();
Statement statement = null;
if (fieldType.isArray() || isOrImplements(fieldType, CLONEABLE_TYPE)) {
statement = createConstructorStatementArrayOrCloneable(fNode);
} else if (isKnownImmutableClass(fieldType, knownImmutableClasses) || isKnownImmutable(pNode.getName(), knownImmutables)) {
statement = createConstructorStatementDefault(fNode);
} else if (fieldType.isDerivedFrom(DATE_TYPE)) {
statement = createConstructorStatementDate(fNode);
} else if (isOrImplements(fieldType, COLLECTION_TYPE) || fieldType.isDerivedFrom(COLLECTION_TYPE) || isOrImplements(fieldType, MAP_TYPE) || fieldType.isDerivedFrom(MAP_TYPE)) {
statement = createConstructorStatementCollection(fNode);
} else if (fieldType.isResolved()) {
addError(createErrorMessage(cNode.getName(), fNode.getName(), fieldType.getName(), "compiling"), fNode);
statement = EmptyStatement.INSTANCE;
} else {
statement = createConstructorStatementGuarded(cNode, fNode);
}
return statement;
}
use of org.codehaus.groovy.ast.FieldNode in project groovy by apache.
the class ImmutableASTTransformation method createConstructorMapCommon.
private static void createConstructorMapCommon(ClassNode cNode, BlockStatement body) {
final List<FieldNode> fList = cNode.getFields();
for (FieldNode fNode : fList) {
// public fields will be rejected elsewhere
if (fNode.isPublic())
continue;
// a property
if (cNode.getProperty(fNode.getName()) != null)
continue;
if (fNode.isFinal() && fNode.isStatic())
continue;
// internal field
if (fNode.getName().contains("$") || fNode.isSynthetic())
continue;
if (fNode.isFinal() && fNode.getInitialExpression() != null)
body.addStatement(checkFinalArgNotOverridden(cNode, fNode));
body.addStatement(createConstructorStatementDefault(fNode));
}
doAddConstructor(cNode, new ConstructorNode(ACC_PUBLIC, params(new Parameter(HASHMAP_TYPE, "args")), ClassNode.EMPTY_ARRAY, body));
}
Aggregations