@Override public void visitConstructorCallExpression(final ConstructorCallExpression call) { super.visitConstructorCallExpression(call); MethodNode target = (MethodNode) 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; i < args.length; i++) { args[i] = typeChooser.resolveType(expressions.get(i), classNode); } MethodNode constructor = findMethodOrFail( call, call.isSuperCall() ? classNode.getSuperClass() : classNode, "<init>", args); call.putNodeMetaData(DIRECT_METHOD_CALL_TARGET, constructor); target = constructor; } } if (target != null) { memorizeInitialExpressions(target); } }
private Expression findStaticPropertyAccessorByFullName( ClassNode staticImportType, String accessorMethodName) { // anything will do as we only check size == 1 ArgumentListExpression dummyArgs = new ArgumentListExpression(); dummyArgs.addExpression(new EmptyExpression()); return findStaticMethod( staticImportType, accessorMethodName, (inLeftExpression ? dummyArgs : ArgumentListExpression.EMPTY_ARGUMENTS)); }
protected void addMockCollaborator( String mockType, ClassExpression targetClass, BlockStatement methodBody) { ArgumentListExpression args = new ArgumentListExpression(); args.addExpression(targetClass); methodBody .getStatements() .add( 0, new ExpressionStatement( new MethodCallExpression(THIS_EXPRESSION, "mock" + mockType, args))); }
private IfStatement getAutowiringIfStatement( ClassExpression targetClass, VariableExpression fieldExpression, BinaryExpression testTargetAssignment) { VariableExpression appCtxVar = new VariableExpression("applicationContext"); BooleanExpression applicationContextCheck = new BooleanExpression( new BinaryExpression( new BinaryExpression( fieldExpression, GrailsASTUtils.EQUALS_OPERATOR, GrailsASTUtils.NULL_EXPRESSION), Token.newSymbol("&&", 0, 0), new BinaryExpression( appCtxVar, GrailsASTUtils.NOT_EQUALS_OPERATOR, GrailsASTUtils.NULL_EXPRESSION))); BlockStatement performAutowireBlock = new BlockStatement(); ArgumentListExpression arguments = new ArgumentListExpression(); arguments.addExpression(fieldExpression); arguments.addExpression(new ConstantExpression(1)); arguments.addExpression(new ConstantExpression(false)); BlockStatement assignFromApplicationContext = new BlockStatement(); ArgumentListExpression argWithClassName = new ArgumentListExpression(); MethodCallExpression getClassNameMethodCall = new MethodCallExpression(targetClass, "getName", new ArgumentListExpression()); argWithClassName.addExpression(getClassNameMethodCall); assignFromApplicationContext.addStatement( new ExpressionStatement( new BinaryExpression( fieldExpression, ASSIGN, new MethodCallExpression(appCtxVar, "getBean", argWithClassName)))); BlockStatement elseBlock = new BlockStatement(); elseBlock.addStatement(new ExpressionStatement(testTargetAssignment)); performAutowireBlock.addStatement( new IfStatement( new BooleanExpression( new MethodCallExpression(appCtxVar, "containsBean", argWithClassName)), assignFromApplicationContext, elseBlock)); performAutowireBlock.addStatement( new ExpressionStatement( new MethodCallExpression( new PropertyExpression(appCtxVar, "autowireCapableBeanFactory"), "autowireBeanProperties", arguments))); return new IfStatement(applicationContextCheck, performAutowireBlock, new BlockStatement()); }