use of org.codehaus.groovy.ast.AnnotationNode in project groovy-core by groovy.
the class AsmClassGenerator method visitClass.
// GroovyClassVisitor interface
// -------------------------------------------------------------------------
public void visitClass(ClassNode classNode) {
referencedClasses.clear();
WriterControllerFactory factory = (WriterControllerFactory) classNode.getNodeMetaData(WriterControllerFactory.class);
WriterController normalController = new WriterController();
if (factory != null) {
this.controller = factory.makeController(normalController);
} else {
this.controller = normalController;
}
this.controller.init(this, context, cv, classNode);
if (controller.shouldOptimizeForInt() || factory != null) {
OptimizingStatementWriter.setNodeMeta(controller.getTypeChooser(), classNode);
}
try {
cv.visit(controller.getBytecodeVersion(), adjustedClassModifiersForClassWriting(classNode), controller.getInternalClassName(), BytecodeHelper.getGenericsSignature(classNode), controller.getInternalBaseClassName(), BytecodeHelper.getClassInternalNames(classNode.getInterfaces()));
cv.visitSource(sourceFile, null);
if (classNode instanceof InnerClassNode) {
InnerClassNode innerClass = (InnerClassNode) classNode;
MethodNode enclosingMethod = innerClass.getEnclosingMethod();
if (enclosingMethod != null) {
String outerClassName = BytecodeHelper.getClassInternalName(innerClass.getOuterClass().getName());
cv.visitOuterClass(outerClassName, enclosingMethod.getName(), BytecodeHelper.getMethodDescriptor(enclosingMethod));
}
}
if (classNode.getName().endsWith("package-info")) {
PackageNode packageNode = classNode.getPackage();
if (packageNode != null) {
// pull them out of package node but treat them like they were on class node
for (AnnotationNode an : packageNode.getAnnotations()) {
// skip built-in properties
if (an.isBuiltIn())
continue;
if (an.hasSourceRetention())
continue;
AnnotationVisitor av = getAnnotationVisitor(classNode, an, cv);
visitAnnotationAttributes(an, av);
av.visitEnd();
}
}
cv.visitEnd();
return;
} else {
visitAnnotations(classNode, cv);
}
if (classNode.isInterface()) {
ClassNode owner = classNode;
if (owner instanceof InnerClassNode) {
owner = owner.getOuterClass();
}
String outerClassName = classNode.getName();
String name = outerClassName + "$" + context.getNextInnerClassIdx();
controller.setInterfaceClassLoadingClass(new InterfaceHelperClassNode(owner, name, 4128, ClassHelper.OBJECT_TYPE, controller.getCallSiteWriter().getCallSites()));
super.visitClass(classNode);
createInterfaceSyntheticStaticFields();
} else {
super.visitClass(classNode);
MopWriter.Factory mopWriterFactory = classNode.getNodeMetaData(MopWriter.Factory.class);
if (mopWriterFactory == null) {
mopWriterFactory = MopWriter.FACTORY;
}
MopWriter mopWriter = mopWriterFactory.create(controller);
mopWriter.createMopMethods();
controller.getCallSiteWriter().generateCallSiteArray();
createSyntheticStaticFields();
}
// GROOVY-6750 and GROOVY-6808
for (Iterator<InnerClassNode> iter = classNode.getInnerClasses(); iter.hasNext(); ) {
InnerClassNode innerClass = iter.next();
makeInnerClassEntry(innerClass);
}
makeInnerClassEntry(classNode);
cv.visitEnd();
} catch (GroovyRuntimeException e) {
e.setModule(classNode.getModule());
throw e;
} catch (NegativeArraySizeException nase) {
throw new GroovyRuntimeException("NegativeArraySizeException while processing " + sourceFile, nase);
} catch (NullPointerException npe) {
throw new GroovyRuntimeException("NPE while processing " + sourceFile, npe);
}
}
use of org.codehaus.groovy.ast.AnnotationNode in project groovy-core by groovy.
the class ExternalizeVerifierASTTransformation method visit.
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 (!hasNoargConstructor(cNode)) {
addError(MY_TYPE_NAME + ": An Externalizable class requires a no-arg constructor but none found", cNode);
}
if (!implementsExternalizable(cNode)) {
addError(MY_TYPE_NAME + ": An Externalizable class must implement the Externalizable interface", cNode);
}
boolean includeFields = memberHasValue(anno, "includeFields", true);
boolean checkPropertyTypes = memberHasValue(anno, "checkPropertyTypes", true);
List<String> excludes = getMemberList(anno, "excludes");
List<FieldNode> list = getInstancePropertyFields(cNode);
if (includeFields) {
list.addAll(getInstanceNonPropertyFields(cNode));
}
checkProps(list, excludes, checkPropertyTypes);
}
}
use of org.codehaus.groovy.ast.AnnotationNode in project groovy-core by groovy.
the class ImmutableASTTransformation 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 (!node.getClassNode().getName().endsWith(".Immutable"))
return;
List<PropertyNode> newProperties = new ArrayList<PropertyNode>();
if (parent instanceof ClassNode) {
final List<String> knownImmutableClasses = getKnownImmutableClasses(node);
final List<String> knownImmutables = getKnownImmutables(node);
ClassNode cNode = (ClassNode) parent;
String cName = cNode.getName();
if (!checkNotInterface(cNode, MY_TYPE_NAME))
return;
makeClassFinal(cNode);
final List<PropertyNode> pList = getInstanceProperties(cNode);
for (PropertyNode pNode : pList) {
adjustPropertyForImmutability(pNode, newProperties);
}
for (PropertyNode pNode : newProperties) {
cNode.getProperties().remove(pNode);
addProperty(cNode, pNode);
}
final List<FieldNode> fList = cNode.getFields();
for (FieldNode fNode : fList) {
ensureNotPublic(cName, fNode);
}
boolean includeSuperProperties = false;
if (hasAnnotation(cNode, TupleConstructorASTTransformation.MY_TYPE)) {
AnnotationNode tupleCons = cNode.getAnnotations(TupleConstructorASTTransformation.MY_TYPE).get(0);
includeSuperProperties = memberHasValue(tupleCons, "includeSuperProperties", true);
if (unsupportedTupleAttribute(tupleCons, "excludes"))
return;
if (unsupportedTupleAttribute(tupleCons, "includes"))
return;
if (unsupportedTupleAttribute(tupleCons, "includeFields"))
return;
if (unsupportedTupleAttribute(tupleCons, "includeProperties"))
return;
if (unsupportedTupleAttribute(tupleCons, "includeSuperFields"))
return;
if (unsupportedTupleAttribute(tupleCons, "callSuper"))
return;
if (unsupportedTupleAttribute(tupleCons, "force"))
return;
}
createConstructors(cNode, knownImmutableClasses, knownImmutables, includeSuperProperties);
if (!hasAnnotation(cNode, EqualsAndHashCodeASTTransformation.MY_TYPE)) {
createHashCode(cNode, true, false, false, null, null);
createEquals(cNode, false, false, false, null, null);
}
if (!hasAnnotation(cNode, ToStringASTTransformation.MY_TYPE)) {
createToString(cNode, false, false, null, null, false, true);
}
if (memberHasValue(node, MEMBER_ADD_COPY_WITH, true) && pList.size() > 0 && !hasDeclaredMethod(cNode, COPY_WITH_METHOD, 1)) {
createCopyWith(cNode, pList);
}
}
}
use of org.codehaus.groovy.ast.AnnotationNode in project groovy-core by groovy.
the class ToStringASTTransformation method visit.
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;
boolean includeSuper = memberHasValue(anno, "includeSuper", true);
boolean includeSuperProperties = memberHasValue(anno, "includeSuperProperties", true);
boolean cacheToString = memberHasValue(anno, "cache", true);
if (includeSuper && cNode.getSuperClass().getName().equals("java.lang.Object")) {
addError("Error during " + MY_TYPE_NAME + " processing: includeSuper=true but '" + cNode.getName() + "' has no super class.", anno);
}
boolean includeNames = memberHasValue(anno, "includeNames", true);
boolean includeFields = memberHasValue(anno, "includeFields", true);
List<String> excludes = getMemberList(anno, "excludes");
List<String> includes = getMemberList(anno, "includes");
boolean ignoreNulls = memberHasValue(anno, "ignoreNulls", true);
boolean includePackage = !memberHasValue(anno, "includePackage", false);
if (hasAnnotation(cNode, CanonicalASTTransformation.MY_TYPE)) {
AnnotationNode canonical = cNode.getAnnotations(CanonicalASTTransformation.MY_TYPE).get(0);
if (excludes == null || excludes.isEmpty())
excludes = getMemberList(canonical, "excludes");
if (includes == null || includes.isEmpty())
includes = getMemberList(canonical, "includes");
}
if (!checkIncludeExclude(anno, excludes, includes, MY_TYPE_NAME))
return;
createToString(cNode, includeSuper, includeFields, excludes, includes, includeNames, ignoreNulls, includePackage, cacheToString, includeSuperProperties);
}
}
use of org.codehaus.groovy.ast.AnnotationNode in project groovy-core by groovy.
the class TupleConstructorASTTransformation method visit.
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;
boolean includeFields = memberHasValue(anno, "includeFields", true);
boolean includeProperties = !memberHasValue(anno, "includeProperties", false);
boolean includeSuperFields = memberHasValue(anno, "includeSuperFields", true);
boolean includeSuperProperties = memberHasValue(anno, "includeSuperProperties", true);
boolean callSuper = memberHasValue(anno, "callSuper", true);
boolean force = memberHasValue(anno, "force", true);
boolean useSetters = memberHasValue(anno, "useSetters", true);
List<String> excludes = getMemberList(anno, "excludes");
List<String> includes = getMemberList(anno, "includes");
if (hasAnnotation(cNode, CanonicalASTTransformation.MY_TYPE)) {
AnnotationNode canonical = cNode.getAnnotations(CanonicalASTTransformation.MY_TYPE).get(0);
if (excludes == null || excludes.isEmpty())
excludes = getMemberList(canonical, "excludes");
if (includes == null || includes.isEmpty())
includes = getMemberList(canonical, "includes");
}
if (!checkIncludeExclude(anno, excludes, includes, MY_TYPE_NAME))
return;
// if @Immutable is found, let it pick up options and do work so we'll skip
if (hasAnnotation(cNode, ImmutableASTTransformation.MY_TYPE))
return;
createConstructor(cNode, includeFields, includeProperties, includeSuperFields, includeSuperProperties, callSuper, force, excludes, includes, useSetters);
}
}
Aggregations