Java Code Examples for org.codehaus.groovy.ast.expr.ConstructorCallExpression#isUsingAnonymousInnerClass()
The following examples show how to use
org.codehaus.groovy.ast.expr.ConstructorCallExpression#isUsingAnonymousInnerClass() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StaticCompilationVisitor.java From groovy with Apache License 2.0 | 5 votes |
@Override public void visitConstructorCallExpression(final ConstructorCallExpression call) { super.visitConstructorCallExpression(call); if (call.isUsingAnonymousInnerClass() && call.getType().getNodeMetaData(StaticTypeCheckingVisitor.class) != null) { ClassNode anonType = call.getType(); anonType.putNodeMetaData(STATIC_COMPILE_NODE, anonType.getEnclosingMethod().getNodeMetaData(STATIC_COMPILE_NODE)); anonType.putNodeMetaData(WriterControllerFactory.class, anonType.getOuterClass().getNodeMetaData(WriterControllerFactory.class)); } MethodNode target = call.getNodeMetaData(DIRECT_METHOD_CALL_TARGET); if (target == null && call.getLineNumber() > 0) { addError("Target constructor for constructor call expression hasn't been set", call); } else if (target == null) { // try to find a target ArgumentListExpression argumentListExpression = InvocationWriter.makeArgumentList(call.getArguments()); List<Expression> expressions = argumentListExpression.getExpressions(); ClassNode[] args = new ClassNode[expressions.size()]; for (int i = 0, n = args.length; i < n; i += 1) { args[i] = typeChooser.resolveType(expressions.get(i), classNode); } target = findMethodOrFail(call, call.isSuperCall() ? classNode.getSuperClass() : classNode, "<init>", args); call.putNodeMetaData(DIRECT_METHOD_CALL_TARGET, target); } if (target != null) { memorizeInitialExpressions(target); } }
Example 2
Source File: FieldASTTransformation.java From groovy with Apache License 2.0 | 5 votes |
@Override public void visitConstructorCallExpression(final ConstructorCallExpression cce) { if (!insideScriptBody || !cce.isUsingAnonymousInnerClass()) return; ConstructorCallExpression old = currentAIC; currentAIC = cce; Expression newArgs = transform(cce.getArguments()); if (cce.getArguments() instanceof TupleExpression && newArgs instanceof TupleExpression) { List<Expression> argList = ((TupleExpression) cce.getArguments()).getExpressions(); argList.clear(); argList.addAll(((TupleExpression) newArgs).getExpressions()); } currentAIC = old; }
Example 3
Source File: InvocationWriter.java From groovy with Apache License 2.0 | 5 votes |
protected boolean writeAICCall(final ConstructorCallExpression call) { if (!call.isUsingAnonymousInnerClass()) return false; ConstructorNode cn = call.getType().getDeclaredConstructors().get(0); OperandStack os = controller.getOperandStack(); String ownerDescriptor = prepareConstructorCall(cn); List<Expression> args = makeArgumentList(call.getArguments()).getExpressions(); Parameter[] params = cn.getParameters(); // if a this appears as parameter here, then it should be // not static, unless we are in a static method. But since // ACG#visitVariableExpression does the opposite for this case, we // push here an explicit this. This should not have any negative effect // sine visiting a method call or property with implicit this will push // a new value for this again. controller.getCompileStack().pushImplicitThis(true); for (int i = 0, n = params.length; i < n; i += 1) { Parameter p = params[i]; Expression arg = args.get(i); if (arg instanceof VariableExpression) { VariableExpression var = (VariableExpression) arg; loadVariableWithReference(var); } else { arg.visit(controller.getAcg()); } os.doGroovyCast(p.getType()); } controller.getCompileStack().popImplicitThis(); finnishConstructorCall(cn, ownerDescriptor, args.size()); return true; }
Example 4
Source File: InnerClassVisitor.java From groovy with Apache License 2.0 | 4 votes |
@Override public void visitConstructorCallExpression(ConstructorCallExpression call) { super.visitConstructorCallExpression(call); if (!call.isUsingAnonymousInnerClass()) { passThisReference(call); return; } InnerClassNode innerClass = (InnerClassNode) call.getType(); ClassNode outerClass = innerClass.getOuterClass(); ClassNode superClass = innerClass.getSuperClass(); if (!superClass.isInterface() && superClass.getOuterClass() != null && !(superClass.isStaticClass() || (superClass.getModifiers() & ACC_STATIC) != 0)) { insertThis0ToSuperCall(call, innerClass); } if (!innerClass.getDeclaredConstructors().isEmpty()) return; if ((innerClass.getModifiers() & ACC_STATIC) != 0) return; VariableScope scope = innerClass.getVariableScope(); if (scope == null) return; boolean isStatic = !inClosure && isStatic(innerClass, scope, call); // expressions = constructor call arguments List<Expression> expressions = ((TupleExpression) call.getArguments()).getExpressions(); // block = init code for the constructor we produce BlockStatement block = new BlockStatement(); // parameters = parameters of the constructor int additionalParamCount = (isStatic ? 0 : 1) + scope.getReferencedLocalVariablesCount(); List<Parameter> parameters = new ArrayList<>(expressions.size() + additionalParamCount); // superCallArguments = arguments for the super call == the constructor call arguments List<Expression> superCallArguments = new ArrayList<>(expressions.size()); // first we add a super() call for all expressions given in the constructor call expression for (int i = 0, n = expressions.size(); i < n; i += 1) { // add one parameter for each expression in the constructor call Parameter param = new Parameter(ClassHelper.OBJECT_TYPE, "p" + additionalParamCount + i); parameters.add(param); // add the corresponding argument to the super constructor call superCallArguments.add(new VariableExpression(param)); } // add the super call ConstructorCallExpression cce = new ConstructorCallExpression( ClassNode.SUPER, new TupleExpression(superCallArguments) ); block.addStatement(new ExpressionStatement(cce)); int pCount = 0; if (!isStatic) { // need to pass "this" to access unknown methods/properties expressions.add(pCount, VariableExpression.THIS_EXPRESSION); ClassNode enclosingType = (inClosure ? ClassHelper.CLOSURE_TYPE : outerClass).getPlainNodeReference(); Parameter thisParameter = new Parameter(enclosingType, "p" + pCount); parameters.add(pCount++, thisParameter); // "this" reference is saved in a field named "this$0" FieldNode thisField = innerClass.addField("this$0", ACC_FINAL | ACC_SYNTHETIC, enclosingType, null); addFieldInit(thisParameter, thisField, block); } // for each shared variable, add a Reference field for (Iterator<Variable> it = scope.getReferencedLocalVariablesIterator(); it.hasNext();) { Variable var = it.next(); VariableExpression ve = new VariableExpression(var); ve.setClosureSharedVariable(true); ve.setUseReferenceDirectly(true); expressions.add(pCount, ve); ClassNode referenceType = ClassHelper.REFERENCE_TYPE.getPlainNodeReference(); Parameter p = new Parameter(referenceType, "p" + pCount); p.setOriginType(var.getOriginType()); parameters.add(pCount++, p); VariableExpression initial = new VariableExpression(p); initial.setSynthetic(true); initial.setUseReferenceDirectly(true); FieldNode pField = innerClass.addFieldFirst(ve.getName(), ACC_PUBLIC | ACC_SYNTHETIC, referenceType, initial); pField.setHolder(true); pField.setOriginType(ClassHelper.getWrapper(var.getOriginType())); } innerClass.addConstructor(ACC_SYNTHETIC, parameters.toArray(Parameter.EMPTY_ARRAY), ClassNode.EMPTY_ARRAY, block); }
Example 5
Source File: VariableScopeVisitor.java From groovy with Apache License 2.0 | 4 votes |
@Override public void visitConstructorCallExpression(final ConstructorCallExpression expression) { boolean oldInSpecialCtorFlag = inSpecialConstructorCall; inSpecialConstructorCall |= expression.isSpecialCall(); super.visitConstructorCallExpression(expression); inSpecialConstructorCall = oldInSpecialCtorFlag; if (!expression.isUsingAnonymousInnerClass()) return; pushState(); InnerClassNode innerClass = (InnerClassNode) expression.getType(); innerClass.setVariableScope(currentScope); currentScope.setClassScope(innerClass); currentScope.setInStaticContext(false); for (MethodNode method : innerClass.getMethods()) { Parameter[] parameters = method.getParameters(); if (parameters.length == 0) { parameters = null; // null means no implicit "it" } visitClosureExpression(new ClosureExpression(parameters, method.getCode())); } for (FieldNode field : innerClass.getFields()) { Expression initExpression = field.getInitialExpression(); pushState(field.isStatic()); if (initExpression != null) { if (initExpression.isSynthetic() && initExpression instanceof VariableExpression && ((VariableExpression) initExpression).getAccessedVariable() instanceof Parameter) { // GROOVY-6834: accessing a parameter which is not yet seen in scope popState(); continue; } initExpression.visit(this); } popState(); } for (Statement initStatement : innerClass.getObjectInitializerStatements()) { initStatement.visit(this); } markClosureSharedVariables(); popState(); }