use of org.codehaus.groovy.ast.InnerClassNode in project groovy by apache.
the class AsmClassGenerator method visitClass.
// GroovyClassVisitor interface
// --------------------------------------------------------------------------
@Override
public void visitClass(final ClassNode classNode) {
referencedClasses.clear();
WriterControllerFactory factory = classNode.getNodeMetaData(WriterControllerFactory.class);
controller = new WriterController();
if (factory != null) {
controller = factory.makeController(controller);
}
controller.init(this, context, classVisitor, classNode);
if (controller.shouldOptimizeForInt() || factory != null) {
OptimizingStatementWriter.setNodeMeta(controller.getTypeChooser(), classNode);
}
classVisitor = controller.getClassVisitor();
try {
int bytecodeVersion = controller.getBytecodeVersion();
Object min = classNode.getNodeMetaData(MINIMUM_BYTECODE_VERSION);
if (min instanceof Integer) {
int minVersion = (int) min;
if ((bytecodeVersion ^ V_PREVIEW) < minVersion) {
bytecodeVersion = minVersion;
}
}
classVisitor.visit(bytecodeVersion, adjustedClassModifiersForClassWriting(classNode), controller.getInternalClassName(), BytecodeHelper.getGenericsSignature(classNode), controller.getInternalBaseClassName(), BytecodeHelper.getClassInternalNames(classNode.getInterfaces()));
classVisitor.visitSource(sourceFile, null);
if (classNode instanceof InnerClassNode) {
// GROOVY-9842
makeInnerClassEntry(classNode.getOuterClass());
// GROOVY-4649, et al.
makeInnerClassEntry(classNode);
MethodNode enclosingMethod = classNode.getEnclosingMethod();
if (enclosingMethod != null) {
classVisitor.visitOuterClass(BytecodeHelper.getClassInternalName(classNode.getOuterClass().getName()), 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
visitAnnotations(classNode, packageNode, classVisitor);
}
} else {
visitAnnotations(classNode, classVisitor);
visitTypeParameters(classNode, classVisitor);
visitType(classNode.getUnresolvedSuperClass(), classVisitor, newSuperTypeReference(-1), "", true);
ClassNode[] interfaces = classNode.getInterfaces();
for (int i = 0; i < interfaces.length; i++) {
visitType(interfaces[i], classVisitor, newSuperTypeReference(i), "", true);
}
if (classNode.isInterface()) {
String outerClassName = classNode.getName();
String name = outerClassName + "$" + context.getNextInnerClassIdx();
controller.setInterfaceClassLoadingClass(new InterfaceHelperClassNode(Optional.ofNullable(classNode.getOuterClass()).orElse(classNode), name, ACC_SUPER | ACC_STATIC | ACC_SYNTHETIC, 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-4649, GROOVY-6750, GROOVY-6808
for (Iterator<InnerClassNode> it = classNode.getInnerClasses(); it.hasNext(); ) {
makeInnerClassEntry(it.next());
}
if (sealedNative(classNode)) {
for (ClassNode sub : classNode.getPermittedSubclasses()) {
classVisitor.visitPermittedSubclass(sub.getName());
}
}
if (classNode.isRecord()) {
visitRecordComponents(classNode);
}
classVisitor.visitEnd();
} catch (GroovyRuntimeException e) {
e.setModule(classNode.getModule());
throw e;
} catch (NullPointerException | NegativeArraySizeException e) {
String m = e.getClass().getSimpleName() + " while processing " + sourceFile;
GroovyRuntimeException gre = new GroovyRuntimeException(m, e);
gre.setModule(classNode.getModule());
throw gre;
}
}
use of org.codehaus.groovy.ast.InnerClassNode in project groovy by apache.
the class SuperCallTraitTransformer method getHelper.
private ClassNode getHelper(final ClassNode traitType) {
// been created when referenced; create a placeholder to be resolved later.
if (!traitType.redirect().getInnerClasses().hasNext() && getSourceUnit().getAST().getClasses().contains(traitType.redirect())) {
ClassNode helperType = new InnerClassNode(traitType, Traits.helperClassName(traitType), ACC_PUBLIC | ACC_STATIC | ACC_ABSTRACT | ACC_SYNTHETIC, ClassHelper.OBJECT_TYPE, ClassNode.EMPTY_ARRAY, null).getPlainNodeReference();
helperType.setRedirect(null);
traitType.redirect().setNodeMetaData(UNRESOLVED_HELPER_CLASS, helperType);
return helperType;
}
return Traits.findHelper(traitType);
}
use of org.codehaus.groovy.ast.InnerClassNode in project groovy by apache.
the class Traits method findHelpers.
static TraitHelpersTuple findHelpers(final ClassNode trait) {
ClassNode helperClassNode = null;
ClassNode fieldHelperClassNode = null;
ClassNode staticFieldHelperClassNode = null;
Iterator<InnerClassNode> innerClasses = trait.redirect().getInnerClasses();
if (innerClasses != null && innerClasses.hasNext()) {
// trait defined in same source unit
while (innerClasses.hasNext()) {
ClassNode icn = innerClasses.next();
if (icn.getName().endsWith(Traits.FIELD_HELPER)) {
fieldHelperClassNode = icn;
} else if (icn.getName().endsWith(Traits.STATIC_FIELD_HELPER)) {
staticFieldHelperClassNode = icn;
} else if (icn.getName().endsWith(Traits.TRAIT_HELPER)) {
helperClassNode = icn;
}
}
} else {
// precompiled trait
try {
final ClassLoader classLoader = trait.getTypeClass().getClassLoader();
String helperClassName = Traits.helperClassName(trait);
helperClassNode = ClassHelper.make(Class.forName(helperClassName, false, classLoader));
try {
fieldHelperClassNode = ClassHelper.make(classLoader.loadClass(Traits.fieldHelperClassName(trait)));
staticFieldHelperClassNode = ClassHelper.make(classLoader.loadClass(Traits.staticFieldHelperClassName(trait)));
} catch (ClassNotFoundException e) {
// not a problem, the field helpers may be absent
}
} catch (ClassNotFoundException e) {
throw new GroovyBugError("Couldn't find trait helper classes on compile classpath!", e);
}
}
return new TraitHelpersTuple(helperClassNode, fieldHelperClassNode, staticFieldHelperClassNode);
}
use of org.codehaus.groovy.ast.InnerClassNode in project groovy by apache.
the class InnerClassVisitor method visitClass.
@Override
public void visitClass(ClassNode node) {
classNode = node;
InnerClassNode innerClass = null;
if (!node.isEnum() && !node.isInterface() && node instanceof InnerClassNode) {
innerClass = (InnerClassNode) node;
if (innerClass.getVariableScope() == null && (innerClass.getModifiers() & ACC_STATIC) == 0) {
innerClass.addField("this$0", ACC_FINAL | ACC_SYNTHETIC, node.getOuterClass().getPlainNodeReference(), null);
}
}
super.visitClass(node);
if (node.isEnum() || node.isInterface())
return;
if (innerClass == null)
return;
if (node.getSuperClass().isInterface() || Traits.isAnnotatedWithTrait(node.getSuperClass())) {
node.addInterface(node.getUnresolvedSuperClass());
node.setUnresolvedSuperClass(ClassHelper.OBJECT_TYPE);
}
}
use of org.codehaus.groovy.ast.InnerClassNode in project groovy by apache.
the class InnerClassCompletionVisitor method visitClass.
@Override
public void visitClass(final ClassNode node) {
classNode = node;
thisField = null;
InnerClassNode innerClass = null;
if (!node.isEnum() && !node.isInterface() && node instanceof InnerClassNode) {
innerClass = (InnerClassNode) node;
thisField = innerClass.getField("this$0");
if (innerClass.getVariableScope() == null && innerClass.getDeclaredConstructors().isEmpty()) {
// add empty default constructor
addGeneratedConstructor(innerClass, ACC_PUBLIC, Parameter.EMPTY_ARRAY, null, null);
}
}
if (node.isEnum() || node.isInterface())
return;
// use Iterator.hasNext() to check for available inner classes
if (node.getInnerClasses().hasNext())
addDispatcherMethods(node);
if (innerClass == null)
return;
super.visitClass(node);
addMopMethods(innerClass);
}
Aggregations