use of org.codehaus.groovy.ast.ConstructorNode in project groovy-core by groovy.
the class InheritConstructorsASTTransformation method processClass.
private void processClass(ClassNode cNode, AnnotationNode node) {
if (cNode.isInterface()) {
addError("Error processing interface '" + cNode.getName() + "'. " + MY_TYPE_NAME + " only allowed for classes.", cNode);
return;
}
boolean copyConstructorAnnotations = memberHasValue(node, "constructorAnnotations", true);
boolean copyParameterAnnotations = memberHasValue(node, "parameterAnnotations", true);
ClassNode sNode = cNode.getSuperClass();
List<AnnotationNode> superAnnotations = sNode.getAnnotations(MY_TYPE);
if (superAnnotations.size() == 1) {
// We need @InheritConstructors from parent classes processed first
// so force that order here. The transformation is benign on an already
// processed node so processing twice in any order won't matter bar
// a very small time penalty.
processClass(sNode, node);
}
for (ConstructorNode cn : sNode.getDeclaredConstructors()) {
addConstructorUnlessAlreadyExisting(cNode, cn, copyConstructorAnnotations, copyParameterAnnotations);
}
}
use of org.codehaus.groovy.ast.ConstructorNode in project spring-boot by spring-projects.
the class ResolveDependencyCoordinatesTransformationTests method transformationOfAnnotationOnConstructor.
@Test
public void transformationOfAnnotationOnConstructor() {
ClassNode classNode = new ClassNode("Test", 0, new ClassNode(Object.class));
this.moduleNode.addClass(classNode);
ConstructorNode constructorNode = new ConstructorNode(0, null);
constructorNode.addAnnotation(this.grabAnnotation);
classNode.addMethod(constructorNode);
assertGrabAnnotationHasBeenTransformed();
}
use of org.codehaus.groovy.ast.ConstructorNode in project groovy by apache.
the class StaticTypeCheckingVisitor method visitClass.
@Override
public void visitClass(final ClassNode node) {
if (shouldSkipClassNode(node))
return;
if (extension.beforeVisitClass(node)) {
extension.afterVisitClass(node);
return;
}
Object type = node.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
if (type != null) {
// transformation has already been run on this class node
// so we'll use a silent collector in order not to duplicate errors
typeCheckingContext.pushErrorCollector();
}
typeCheckingContext.pushEnclosingClassNode(node);
Set<MethodNode> oldVisitedMethod = typeCheckingContext.alreadyVisitedMethods;
typeCheckingContext.alreadyVisitedMethods = new LinkedHashSet<MethodNode>();
super.visitClass(node);
Iterator<InnerClassNode> innerClasses = node.getInnerClasses();
while (innerClasses.hasNext()) {
InnerClassNode innerClassNode = innerClasses.next();
visitClass(innerClassNode);
}
typeCheckingContext.alreadyVisitedMethods = oldVisitedMethod;
node.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, node);
// works in a two pass sequence and we don't want to skip the second pass
for (MethodNode methodNode : node.getMethods()) {
methodNode.putNodeMetaData(StaticTypeCheckingVisitor.class, Boolean.TRUE);
}
for (ConstructorNode constructorNode : node.getDeclaredConstructors()) {
constructorNode.putNodeMetaData(StaticTypeCheckingVisitor.class, Boolean.TRUE);
}
extension.afterVisitClass(node);
}
use of org.codehaus.groovy.ast.ConstructorNode in project groovy by apache.
the class ClosureWriter method writeClosure.
public void writeClosure(ClosureExpression expression) {
CompileStack compileStack = controller.getCompileStack();
MethodVisitor mv = controller.getMethodVisitor();
ClassNode classNode = controller.getClassNode();
AsmClassGenerator acg = controller.getAcg();
// generate closure as public class to make sure it can be properly invoked by classes of the
// Groovy runtime without circumventing JVM access checks (see CachedMethod for example).
int mods = ACC_PUBLIC;
if (classNode.isInterface()) {
mods |= ACC_STATIC;
}
ClassNode closureClass = getOrAddClosureClass(expression, mods);
String closureClassinternalName = BytecodeHelper.getClassInternalName(closureClass);
List constructors = closureClass.getDeclaredConstructors();
ConstructorNode node = (ConstructorNode) constructors.get(0);
Parameter[] localVariableParams = node.getParameters();
mv.visitTypeInsn(NEW, closureClassinternalName);
mv.visitInsn(DUP);
if (controller.isStaticMethod() || compileStack.isInSpecialConstructorCall()) {
(new ClassExpression(classNode)).visit(acg);
(new ClassExpression(controller.getOutermostClass())).visit(acg);
} else {
mv.visitVarInsn(ALOAD, 0);
controller.getOperandStack().push(ClassHelper.OBJECT_TYPE);
loadThis();
}
// on the stack
for (int i = 2; i < localVariableParams.length; i++) {
Parameter param = localVariableParams[i];
String name = param.getName();
loadReference(name, controller);
if (param.getNodeMetaData(ClosureWriter.UseExistingReference.class) == null) {
param.setNodeMetaData(ClosureWriter.UseExistingReference.class, Boolean.TRUE);
}
}
// we may need to pass in some other constructors
//cv.visitMethodInsn(INVOKESPECIAL, innerClassinternalName, "<init>", prototype + ")V");
mv.visitMethodInsn(INVOKESPECIAL, closureClassinternalName, "<init>", BytecodeHelper.getMethodDescriptor(ClassHelper.VOID_TYPE, localVariableParams), false);
controller.getOperandStack().replace(ClassHelper.CLOSURE_TYPE, localVariableParams.length);
}
use of org.codehaus.groovy.ast.ConstructorNode in project groovy by apache.
the class StaticInvocationWriter method writeInvokeConstructor.
@Override
public void writeInvokeConstructor(final ConstructorCallExpression call) {
MethodNode mn = call.getNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET);
if (mn == null) {
super.writeInvokeConstructor(call);
return;
}
if (writeAICCall(call))
return;
ConstructorNode cn;
if (mn instanceof ConstructorNode) {
cn = (ConstructorNode) mn;
} else {
cn = new ConstructorNode(mn.getModifiers(), mn.getParameters(), mn.getExceptions(), mn.getCode());
cn.setDeclaringClass(mn.getDeclaringClass());
}
TupleExpression args = makeArgumentList(call.getArguments());
if (cn.isPrivate()) {
ClassNode classNode = controller.getClassNode();
ClassNode declaringClass = cn.getDeclaringClass();
if (declaringClass != classNode) {
MethodNode bridge = null;
if (call.getNodeMetaData(StaticTypesMarker.PV_METHODS_ACCESS) != null) {
Map<MethodNode, MethodNode> bridgeMethods = declaringClass.getNodeMetaData(StaticCompilationMetadataKeys.PRIVATE_BRIDGE_METHODS);
bridge = bridgeMethods != null ? bridgeMethods.get(cn) : null;
}
if (bridge != null && bridge instanceof ConstructorNode) {
ArgumentListExpression newArgs = new ArgumentListExpression(new ConstantExpression(null));
for (Expression arg : args) {
newArgs.addExpression(arg);
}
cn = (ConstructorNode) bridge;
args = newArgs;
} else {
controller.getSourceUnit().addError(new SyntaxException("Cannot call private constructor for " + declaringClass.toString(false) + " from class " + classNode.toString(false), call.getLineNumber(), call.getColumnNumber(), mn.getLastLineNumber(), call.getLastColumnNumber()));
}
}
}
String ownerDescriptor = prepareConstructorCall(cn);
int before = controller.getOperandStack().getStackLength();
loadArguments(args.getExpressions(), cn.getParameters());
finnishConstructorCall(cn, ownerDescriptor, controller.getOperandStack().getStackLength() - before);
}
Aggregations