Java Code Examples for org.codehaus.groovy.ast.expr.ConstructorCallExpression#getType()
The following examples show how to use
org.codehaus.groovy.ast.expr.ConstructorCallExpression#getType() .
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: GroovyASTUtils.java From groovy-language-server with Apache License 2.0 | 6 votes |
public static List<MethodNode> getMethodOverloadsFromCallExpression(MethodCall node, ASTNodeVisitor astVisitor) { if (node instanceof MethodCallExpression) { MethodCallExpression methodCallExpr = (MethodCallExpression) node; ClassNode leftType = getTypeOfNode(methodCallExpr.getObjectExpression(), astVisitor); if (leftType != null) { return leftType.getMethods(methodCallExpr.getMethod().getText()); } } else if (node instanceof ConstructorCallExpression) { ConstructorCallExpression constructorCallExpr = (ConstructorCallExpression) node; ClassNode constructorType = constructorCallExpr.getType(); if (constructorType != null) { return constructorType.getDeclaredConstructors().stream().map(constructor -> (MethodNode) constructor) .collect(Collectors.toList()); } } return Collections.emptyList(); }
Example 2
Source File: ResolveVisitor.java From groovy with Apache License 2.0 | 6 votes |
private void findPossibleOuterClassNodeForNonStaticInnerClassInstantiation(final ConstructorCallExpression cce) { // GROOVY-8947: Fail to resolve non-static inner class outside of outer class // `new Computer().new Cpu(4)` will be parsed to `new Cpu(new Computer(), 4)` // so non-static inner class instantiation expression's first argument is a constructor call of outer class // but the first argument is constructor call can not be non-static inner class instantiation expression, e.g. // `new HashSet(new ArrayList())`, so we add "possible" to the variable name Expression argumentExpression = cce.getArguments(); if (argumentExpression instanceof ArgumentListExpression) { ArgumentListExpression argumentListExpression = (ArgumentListExpression) argumentExpression; List<Expression> expressionList = argumentListExpression.getExpressions(); if (!expressionList.isEmpty()) { Expression firstExpression = expressionList.get(0); if (firstExpression instanceof ConstructorCallExpression) { ConstructorCallExpression constructorCallExpression = (ConstructorCallExpression) firstExpression; ClassNode possibleOuterClassNode = constructorCallExpression.getType(); possibleOuterClassNodeMap.put(cce.getType(), possibleOuterClassNode); } } } }
Example 3
Source File: NamedParamsCompletion.java From netbeans with Apache License 2.0 | 5 votes |
private void completeNamedParams( List<CompletionProposal> proposals, int anchor, ConstructorCallExpression constructorCall, NamedArgumentListExpression namedArguments) { ClassNode type = constructorCall.getType(); String prefix = context.getPrefix(); for (FieldNode fieldNode : type.getFields()) { if (fieldNode.getLineNumber() < 0 || fieldNode.getColumnNumber() < 0) { continue; } String typeName = fieldNode.getType().getNameWithoutPackage(); String name = fieldNode.getName(); // If the prefix is empty, complete only missing parameters if ("".equals(prefix)) { if (isAlreadyPresent(namedArguments, name)) { continue; } // Otherwise check if the field is starting with (and not equal to) the prefix } else { if (name.equals(prefix) || !name.startsWith(prefix)) { continue; } } proposals.add(new CompletionItem.NamedParameter(typeName, name, anchor)); } }
Example 4
Source File: GroovyClassFilterTransformer.java From jasperreports with GNU Lesser General Public License v3.0 | 5 votes |
@Override protected Expression transformConstructorCall(ConstructorCallExpression exp) { if (allowed(exp.getType())) { Expression originalArgs = exp.getArguments(); Expression transformedArgs = transformArguments(originalArgs); Expression unwrappedArgs = unwrapTransformedArguments(transformedArgs, originalArgs); if (unwrappedArgs.equals(originalArgs)) { if (log.isDebugEnabled()) { log.debug("allowed constructor call " + exp); } return exp; } ConstructorCallExpression transformedCall = new ConstructorCallExpression( exp.getType(), unwrappedArgs); if (log.isDebugEnabled()) { log.debug("transformed constructor call " + transformedCall); } return transformedCall; } return super.transformConstructorCall(exp); }
Example 5
Source File: MacroClassTransformation.java From groovy with Apache License 2.0 | 5 votes |
@Override public void visitConstructorCallExpression(final ConstructorCallExpression call) { ClassNode type = call.getType(); if (type instanceof InnerClassNode) { if (((InnerClassNode) type).isAnonymous() && MACROCLASS_TYPE.getNameWithoutPackage().equals(type.getSuperClass().getNameWithoutPackage())) { try { String source = convertInnerClassToSource(type); MethodCallExpression macroCall = callX( propX(classX(ClassHelper.makeWithoutCaching(MacroBuilder.class, false)), "INSTANCE"), MACRO_METHOD, args( constX(source), MacroGroovyMethods.buildSubstitutions(sourceUnit, type), classX(ClassHelper.make(ClassNode.class)) ) ); macroCall.setSpreadSafe(false); macroCall.setSafe(false); macroCall.setImplicitThis(false); call.putNodeMetaData(MacroTransformation.class, macroCall); List<ClassNode> classes = sourceUnit.getAST().getClasses(); for (Iterator<ClassNode> iterator = classes.iterator(); iterator.hasNext(); ) { final ClassNode aClass = iterator.next(); if (aClass == type || type == aClass.getOuterClass()) { iterator.remove(); } } } catch (Exception e) { // FIXME e.printStackTrace(); } return; } } super.visitConstructorCallExpression(call); }
Example 6
Source File: ResolveVisitor.java From groovy with Apache License 2.0 | 5 votes |
protected Expression transformConstructorCallExpression(final ConstructorCallExpression cce) { findPossibleOuterClassNodeForNonStaticInnerClassInstantiation(cce); ClassNode type = cce.getType(); resolveOrFail(type, cce); if (Modifier.isAbstract(type.getModifiers())) { addError("You cannot create an instance from the abstract " + getDescription(type) + ".", cce); } return cce.transformExpression(this); }
Example 7
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 8
Source File: InvocationWriter.java From groovy with Apache License 2.0 | 5 votes |
protected void writeNormalConstructorCall(final ConstructorCallExpression call) { Expression arguments = call.getArguments(); if (arguments instanceof TupleExpression) { TupleExpression tupleExpression = (TupleExpression) arguments; int size = tupleExpression.getExpressions().size(); if (size == 0) { arguments = MethodCallExpression.NO_ARGUMENTS; } } Expression receiver = new ClassExpression(call.getType()); controller.getCallSiteWriter().makeCallSite(receiver, CallSiteWriter.CONSTRUCTOR, arguments, false, false, false, false); }
Example 9
Source File: FindMethodUsagesVisitor.java From netbeans with Apache License 2.0 | 4 votes |
private void addConstructorUsages(MethodCallExpression methodCall, ConstructorCallExpression constructorCallExpression) { final ClassNode type = constructorCallExpression.getType(); if (methodType.equals(ElementUtils.getDeclaringClassName(type))) { findAndAdd(type, methodCall); } }
Example 10
Source File: GenericsVisitor.java From groovy with Apache License 2.0 | 4 votes |
@Override public void visitConstructorCallExpression(ConstructorCallExpression call) { ClassNode type = call.getType(); boolean isAnon = type instanceof InnerClassNode && ((InnerClassNode) type).isAnonymous(); checkGenericsUsage(type, type.redirect(), isAnon); }
Example 11
Source File: VerifierCodeVisitor.java From groovy with Apache License 2.0 | 4 votes |
public void visitConstructorCallExpression(ConstructorCallExpression call) { ClassNode callType = call.getType(); if (callType.isEnum() && !callType.equals(classNode)) { throw new RuntimeParserException("Enum constructor calls are only allowed inside the enum class", call); } }
Example 12
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 13
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(); }