private void newifyMethodOrField(AnnotatedNode parent, boolean autoFlag, ListExpression list, final Pattern cnPattern) { final ListExpression oldClassesToNewify = classesToNewify; final boolean oldAuto = auto; final Pattern oldCnPattern = classNamePattern; checkClassLevelClashes(list); checkAutoClash(autoFlag, parent); classesToNewify = list; auto = autoFlag; classNamePattern = cnPattern; if (parent instanceof FieldNode) { super.visitField((FieldNode) parent); } else { super.visitMethod((MethodNode) parent); } classesToNewify = oldClassesToNewify; auto = oldAuto; classNamePattern = oldCnPattern; }
} else if (expr instanceof VariableExpression && searchSourceUnit) { VariableExpression ve = (VariableExpression) expr; ClassNode fromSourceUnit = getSourceUnitClass(ve); if (fromSourceUnit != null) { ClassExpression found = classX(fromSourceUnit); list.addExpression(found); } else { addError(BASE_BAD_PARAM_ERROR + "an unresolvable reference to '" + ve.getName() + "'.", expr); if (next instanceof VariableExpression && searchSourceUnit) { VariableExpression ve = (VariableExpression) next; ClassNode fromSourceUnit = getSourceUnitClass(ve); if (fromSourceUnit != null) { ClassExpression found = classX(fromSourceUnit); expressions.set(i, found); } else { addError(BASE_BAD_PARAM_ERROR + "a list containing an unresolvable reference to '" + ve.getName() + "'.", next); addError(BASE_BAD_PARAM_ERROR + "a list containing type: " + next.getType().getName() + ".", next); checkDuplicateNameClashes(list); } else if (expr != null) { addError(BASE_BAD_PARAM_ERROR + "a type: " + expr.getType().getName() + ".", expr);
public void visit(ASTNode[] nodes, SourceUnit source) { this.source = source; if (nodes.length != 2 || !(nodes[0] instanceof AnnotationNode) || !(nodes[1] instanceof AnnotatedNode)) { internalError("Expecting [AnnotationNode, AnnotatedClass] but got: " + Arrays.asList(nodes)); } AnnotatedNode parent = (AnnotatedNode) nodes[1]; AnnotationNode node = (AnnotationNode) nodes[0]; if (!MY_TYPE.equals(node.getClassNode())) { internalError("Transformation called from wrong annotation: " + node.getClassNode().getName()); } final boolean autoFlag = determineAutoFlag(node.getMember("auto")); final Expression classNames = node.getMember("value"); final Pattern cnPattern = determineClassNamePattern(node.getMember("pattern")); if (parent instanceof ClassNode) { newifyClass((ClassNode) parent, autoFlag, determineClasses(classNames, false), cnPattern); } else if (parent instanceof MethodNode || parent instanceof FieldNode) { newifyMethodOrField(parent, autoFlag, determineClasses(classNames, false), cnPattern); } else if (parent instanceof DeclarationExpression) { newifyDeclaration((DeclarationExpression) parent, autoFlag, determineClasses(classNames, true), cnPattern); } }
private void checkClassLevelClashes(ListExpression list) { @SuppressWarnings("unchecked") final List<ClassExpression> classes = (List) list.getExpressions(); for (ClassExpression ce : classes) { final String name = ce.getType().getNameWithoutPackage(); if (findClassWithMatchingBasename(name)) { addError("Error during @" + MY_NAME + " processing. Class '" + name + "' can't appear at " + "method/constructor/field level if it already appears at the class level.", ce); } } }
public void visit(ASTNode[] nodes, SourceUnit source) { this.source = source; if (nodes.length != 2 || !(nodes[0] instanceof AnnotationNode) || !(nodes[1] instanceof AnnotatedNode)) { internalError("Expecting [AnnotationNode, AnnotatedClass] but got: " + Arrays.asList(nodes)); } AnnotatedNode parent = (AnnotatedNode) nodes[1]; AnnotationNode node = (AnnotationNode) nodes[0]; if (!MY_TYPE.equals(node.getClassNode())) { internalError("Transformation called from wrong annotation: " + node.getClassNode().getName()); } boolean autoFlag = determineAutoFlag(node.getMember("auto")); ListExpression list = determineClassesToNewify(node.getMember("value")); if (parent instanceof ClassNode) { newifyClass(parent, autoFlag, list); } else if (parent instanceof MethodNode || parent instanceof FieldNode) { newifyMethodOrField(parent, autoFlag, list); } }
public Expression transform(Expression expr) { if (expr == null) return null; if (expr instanceof MethodCallExpression) { MethodCallExpression mce = (MethodCallExpression) expr; Expression args = transform(mce.getArguments()); Expression method = transform(mce.getMethod()); Expression object = transform(mce.getObjectExpression()); if (isNewifyCandidate(mce)) { return transformMethodCall(mce, args); } return new MethodCallExpression(object, method, args); } return expr.transformExpression(this); }
if (expr instanceof MethodCallExpression && candidate == null) { MethodCallExpression mce = (MethodCallExpression) expr; Expression args = transform(mce.getArguments()); if (isNewifyCandidate(mce)) { Expression transformed = transformMethodCall(mce, args); transformed.setSourcePosition(mce); return transformed; Expression method = transform(mce.getMethod()); Expression object = transform(mce.getObjectExpression()); MethodCallExpression transformed = callX(object, method, args); transformed.setImplicitThis(mce.isImplicitThis()); if (shouldTransform(de)) { candidate = null; Expression left = de.getLeftExpression(); Expression right = transform(de.getRightExpression()); DeclarationExpression newDecl = new DeclarationExpression(left, de.getOperation(), right); newDecl.addAnnotations(de.getAnnotations());
private Expression transformMethodCall(MethodCallExpression mce, Expression argsExp) { ClassNode classType; if (isNewMethodStyle(mce)) { classType = mce.getObjectExpression().getType(); } else { classType = findMatchingCandidateClass(mce); } if (classType != null) { Expression argsToUse = argsExp; if (classType.getOuterClass() != null && ((classType.getModifiers() & org.objectweb.asm.Opcodes.ACC_STATIC) == 0)) { if (!(argsExp instanceof ArgumentListExpression)) { addError("Non-static inner constructor arguments must be an argument list expression; pass 'this' pointer explicitely as first constructor argument otherwise.", mce); return mce; } final ArgumentListExpression argsListExp = (ArgumentListExpression) argsExp; final List<Expression> argExpList = argsListExp.getExpressions(); final VariableExpression thisVarExp = new VariableExpression("this"); final List<Expression> expressionsWithThis = new ArrayList<Expression>(argExpList.size() + 1); expressionsWithThis.add(thisVarExp); expressionsWithThis.addAll(argExpList); argsToUse = new ArgumentListExpression(expressionsWithThis); } return new ConstructorCallExpression(classType, argsToUse); } // set the args as they might have gotten Newify transformed GROOVY-3491 mce.setArguments(argsExp); return mce; }
private Expression transformMethodCall(MethodCallExpression mce, Expression args) { ClassNode classType; if (isNewMethodStyle(mce)) { classType = mce.getObjectExpression().getType(); } else { classType = findMatchingCandidateClass(mce); } if (classType != null) { return new ConstructorCallExpression(classType, args); } // set the args as they might have gotten Newify transformed GROOVY-3491 mce.setArguments(args); return mce; }
private boolean isNewifyCandidate(MethodCallExpression mce) { return mce.getObjectExpression() == VariableExpression.THIS_EXPRESSION || (auto && isNewMethodStyle(mce)); }
private void checkClassLevelClashes(ListExpression list) { final List<ClassExpression> classes = list.getExpressions(); for (ClassExpression ce : classes) { final String name = ce.getType().getNameWithoutPackage(); if (findClassWithMatchingBasename(name)) { throw new RuntimeException("Error during @" + MY_NAME + " processing. Class '" + name + "' can't appear at method/constructor/field level if it already appears at the class level."); } } }
private ListExpression determineClassesToNewify(Expression expr) { ListExpression list = new ListExpression(); if (expr instanceof ClassExpression) { list.addExpression(expr); } else if (expr instanceof ListExpression) { list = (ListExpression) expr; final List<Expression> expressions = list.getExpressions(); for (Expression ex : expressions) { if (!(ex instanceof ClassExpression)) { throw new RuntimeException("Error during @" + MY_NAME + " processing. Annotation parameter must be a list of classes."); } } checkDuplicateNameClashes(list); } return list; }
public void visit(ASTNode[] nodes, SourceUnit source) { this.source = source; if (nodes.length != 2 || !(nodes[0] instanceof AnnotationNode) || !(nodes[1] instanceof AnnotatedNode)) { internalError("Expecting [AnnotationNode, AnnotatedClass] but got: " + Arrays.asList(nodes)); } AnnotatedNode parent = (AnnotatedNode) nodes[1]; AnnotationNode node = (AnnotationNode) nodes[0]; if (!MY_TYPE.equals(node.getClassNode())) { internalError("Transformation called from wrong annotation: " + node.getClassNode().getName()); } boolean autoFlag = determineAutoFlag(node.getMember("auto")); ListExpression list = determineClassesToNewify(node.getMember("value")); if (parent instanceof ClassNode) { newifyClass(parent, autoFlag, list); } else if (parent instanceof MethodNode || parent instanceof FieldNode) { newifyMethodOrField(parent, autoFlag, list); } }
public Expression transform(Expression expr) { if (expr == null) return null; if (expr instanceof MethodCallExpression) { MethodCallExpression mce = (MethodCallExpression) expr; Expression args = transform(mce.getArguments()); Expression method = transform(mce.getMethod()); Expression object = transform(mce.getObjectExpression()); if (isNewifyCandidate(mce)) { return transformMethodCall(mce, args); } return new MethodCallExpression(object, method, args); } else if (expr instanceof PropertyExpression) { PropertyExpression pe = (PropertyExpression) expr; pe.setObjectExpression(transform(pe.getObjectExpression())); return pe; } return expr.transformExpression(this); }
private Expression transformMethodCall(MethodCallExpression mce, Expression args) { ClassNode classType; if (isNewMethodStyle(mce)) { classType = mce.getObjectExpression().getType(); } else { classType = findMatchingCandidateClass(mce); } if (classType != null) { return new ConstructorCallExpression(classType, args); } // set the args as they might have gotten Newify transformed GROOVY-3491 mce.setArguments(args); return mce; }
private void checkClassLevelClashes(ListExpression list) { @SuppressWarnings("unchecked") final List<ClassExpression> classes = (List)list.getExpressions(); for (ClassExpression ce : classes) { final String name = ce.getType().getNameWithoutPackage(); if (findClassWithMatchingBasename(name)) { addError("Error during @" + MY_NAME + " processing. Class '" + name + "' can't appear at " + "method/constructor/field level if it already appears at the class level.", ce); } } }
private boolean isNewifyCandidate(MethodCallExpression mce) { return mce.getObjectExpression() == VariableExpression.THIS_EXPRESSION || (auto && isNewMethodStyle(mce)); }
private void checkClassLevelClashes(ListExpression list) { final List<ClassExpression> classes = list.getExpressions(); for (ClassExpression ce : classes) { final String name = ce.getType().getNameWithoutPackage(); if (findClassWithMatchingBasename(name)) { throw new RuntimeException("Error during @" + MY_NAME + " processing. Class '" + name + "' can't appear at method/constructor/field level if it already appears at the class level."); } } }
private ListExpression determineClassesToNewify(Expression expr) { ListExpression list = new ListExpression(); if (expr instanceof ClassExpression) { list.addExpression(expr); } else if (expr instanceof ListExpression) { list = (ListExpression) expr; final List<Expression> expressions = list.getExpressions(); for (Expression ex : expressions) { if (!(ex instanceof ClassExpression)) { throw new RuntimeException("Error during @" + MY_NAME + " processing. Annotation parameter must be a list of classes."); } } checkDuplicateNameClashes(list); } return list; }
public void visit(ASTNode[] nodes, SourceUnit source) { this.source = source; if (nodes.length != 2 || !(nodes[0] instanceof AnnotationNode) || !(nodes[1] instanceof AnnotatedNode)) { internalError("Expecting [AnnotationNode, AnnotatedClass] but got: " + Arrays.asList(nodes)); } AnnotatedNode parent = (AnnotatedNode) nodes[1]; AnnotationNode node = (AnnotationNode) nodes[0]; if (!MY_TYPE.equals(node.getClassNode())) { internalError("Transformation called from wrong annotation: " + node.getClassNode().getName()); } boolean autoFlag = determineAutoFlag(node.getMember("auto")); Expression value = node.getMember("value"); if (parent instanceof ClassNode) { newifyClass((ClassNode) parent, autoFlag, determineClasses(value, false)); } else if (parent instanceof MethodNode || parent instanceof FieldNode) { newifyMethodOrField(parent, autoFlag, determineClasses(value, false)); } else if (parent instanceof DeclarationExpression) { newifyDeclaration((DeclarationExpression) parent, autoFlag, determineClasses(value, true)); } }