@Override public void visitMethod(MethodNode node) { addNode(methods, node.getName(), node); super.visitMethod(node); }
public Method(Spec parent, MethodNode code) { super(parent, code); setName(code.getName()); }
public static void deleteMethod(ClassNode clazz, MethodNode method) { // as of 1.8.6, this is the best possible implementation clazz.getMethods().remove(method); clazz.getDeclaredMethods(method.getName()).remove(method); }
public void addMethod(MethodNode node) { node.setDeclaringClass(this); ClassNode base = redirect(); if (base.methodsList.isEmpty()) { base.methodsList = new ArrayList<MethodNode>(); } base.methodsList.add(node); base.methods.put(node.getName(), node); }
private boolean isFixtureMethod(MethodNode method) { String name = method.getName(); for (String fmName : FIXTURE_METHODS) { if (!fmName.equalsIgnoreCase(name)) continue; // assertion: is (meant to be) a fixture method, so we'll return true in the end if (method.isStatic()) errorReporter.error(method, "Fixture methods must not be static"); if (!fmName.equals(name)) errorReporter.error(method, "Misspelled '%s()' method (wrong capitalization)", fmName); return true; } return false; }
private static boolean isCustomScriptBodyMethod(MethodNode node) { return node != null && !(node.getDeclaringClass().equals(ClassHelper.SCRIPT_TYPE) && "run".equals(node.getName()) && node.getParameters().length == 0); } }
private boolean isCustomScriptBodyMethod(MethodNode node) { return node != null && !(node.getDeclaringClass().equals(ClassHelper.SCRIPT_TYPE) && "run".equals(node.getName()) && node.getParameters().length == 0); } }
public static MethodCallExpression createDirectMethodCall(Expression target, MethodNode method, Expression arguments) { MethodCallExpression result = new MethodCallExpression(target, method.getName(), arguments); result.setMethodTarget(method); result.setImplicitThis(false); // see http://groovy.329449.n5.nabble.com/Problem-with-latest-2-0-beta-3-snapshot-and-Spock-td5496353.html return result; }
private void buildFixtureMethod(MethodNode method) { FixtureMethod fixtureMethod = new FixtureMethod(spec, method); Block block = new AnonymousBlock(fixtureMethod); fixtureMethod.addBlock(block); List<Statement> stats = AstUtil.getStatements(method); block.getAst().addAll(stats); stats.clear(); String name = method.getName(); if (name.equals(SETUP)) spec.setSetupMethod(fixtureMethod); else if (name.equals(CLEANUP)) spec.setCleanupMethod(fixtureMethod); else if (name.equals(SETUP_SPEC_METHOD)) spec.setSetupSpecMethod(fixtureMethod); else spec.setCleanupSpecMethod(fixtureMethod); }
private void checkInterfaceMethodVisibility(ClassNode node) { if (!node.isInterface()) return; for (MethodNode method : node.getMethods()) { if (method.isPrivate()) { addError("Method '" + method.getName() + "' is private but should be public in " + getDescription(currentClass) + ".", method); } else if (method.isProtected()) { addError("Method '" + method.getName() + "' is protected but should be public in " + getDescription(currentClass) + ".", method); } } }
private static void createBuildeeMethods(ClassNode buildee, MethodNode mNode, ClassNode builder, List<FieldNode> fields) { ClassNode paramType = makeClassSafeWithGenerics(builder, setGenTypes(fields.size())); List<Expression> argsList = new ArrayList<Expression>(); Parameter initParam = param(paramType, "initializer"); for (FieldNode fieldNode : fields) { argsList.add(propX(varX(initParam), fieldNode.getName())); } String newName = "$" + mNode.getName(); // can't have private and public methods of the same name, so rename original buildee.addMethod(mNode.getName(), PUBLIC_STATIC, mNode.getReturnType(), params(param(paramType, "initializer")), NO_EXCEPTIONS, block(stmt(callX(buildee, newName, args(argsList))))); renameMethod(buildee, mNode, newName); }
private static MethodNode getDeclaredMethodCorrected(Map genericsSpec, MethodNode mn, ClassNode correctedNext) { for (MethodNode orig : correctedNext.getDeclaredMethods(mn.getName())) { MethodNode method = correctToGenericsSpec(genericsSpec, orig); if (ParameterUtils.parametersEqual(method.getParameters(), mn.getParameters())) { return method; } } return null; }
private static MethodNode getDeclaredMethodCorrected(Map<String, ClassNode> genericsSpec, MethodNode origMethod, ClassNode correctedClass) { for (MethodNode nameMatch : correctedClass.getDeclaredMethods(origMethod.getName())) { MethodNode correctedMethod = correctToGenericsSpec(genericsSpec, nameMatch); if (ParameterUtils.parametersEqual(correctedMethod.getParameters(), origMethod.getParameters())) { return correctedMethod; } } return null; }
private void createMethods(ClassNode cNode, ClassNode exception, String message, ClosureExpression code) { for (MethodNode candidate : getAllCorrectedMethodsMap(cNode).values()) { if (candidate.isAbstract()) { addGeneratedMethod(cNode, candidate.getName(), Opcodes.ACC_PUBLIC, candidate.getReturnType(), candidate.getParameters(), candidate.getExceptions(), methodBody(exception, message, code, candidate.getReturnType())); } } }
private void checkRepetitiveMethod(MethodNode node) { if (isConstructor(node)) return; for (MethodNode method : currentClass.getMethods(node.getName())) { if (method == node) continue; if (!method.getDeclaringClass().equals(node.getDeclaringClass())) continue; Parameter[] p1 = node.getParameters(); Parameter[] p2 = method.getParameters(); if (p1.length != p2.length) continue; addErrorIfParamsAndReturnTypeEqual(p2, p1, node, method); } }
private static boolean hasUsableImplementation(ClassNode c, MethodNode m) { if (c == m.getDeclaringClass()) return false; MethodNode found = c.getDeclaredMethod(m.getName(), m.getParameters()); if (found == null) return false; int asp = found.getModifiers() & ABSTRACT_STATIC_PRIVATE; int visible = found.getModifiers() & VISIBILITY; if (visible != 0 && asp == 0) return true; if (c.equals(OBJECT_TYPE)) return false; return hasUsableImplementation(c.getSuperClass(), m); }
private void checkMethodsForOverridingFinal(ClassNode cn) { for (MethodNode method : cn.getMethods()) { Parameter[] params = method.getParameters(); for (MethodNode superMethod : cn.getSuperClass().getMethods(method.getName())) { Parameter[] superParams = superMethod.getParameters(); if (!hasEqualParameterTypes(params, superParams)) continue; if (!superMethod.isFinal()) break; addInvalidUseOfFinalError(method, params, superMethod.getDeclaringClass()); return; } } }
private void checkAndAddCannotCallPrivateMethodError(MethodNode target, Expression receiver, ClassNode classNode, ClassNode declaringClass) { if (declaringClass != classNode) { controller.getSourceUnit().addError(new SyntaxException("Cannot call private method " + (target.isStatic() ? "static " : "") + declaringClass.toString(false) + "#" + target.getName() + " from class " + classNode.toString(false), receiver.getLineNumber(), receiver.getColumnNumber(), receiver.getLastLineNumber(), receiver.getLastColumnNumber())); } }
public static MethodNode correctToGenericsSpec(Map<String, ClassNode> genericsSpec, MethodNode mn) { ClassNode correctedType = correctToGenericsSpecRecurse(genericsSpec, mn.getReturnType()); Parameter[] origParameters = mn.getParameters(); Parameter[] newParameters = new Parameter[origParameters.length]; for (int i = 0; i < origParameters.length; i++) { Parameter origParameter = origParameters[i]; newParameters[i] = new Parameter(correctToGenericsSpecRecurse(genericsSpec, origParameter.getType()), origParameter.getName(), origParameter.getInitialExpression()); } return new MethodNode(mn.getName(), mn.getModifiers(), correctedType, newParameters, mn.getExceptions(), mn.getCode()); }
private void makeDynamicGetProperty(final Expression receiver, final String methodName, final boolean safe) { MethodNode target = safe?INVOKERHELPER_GETPROPERTYSAFE_METHOD:INVOKERHELPER_GETPROPERTY_METHOD; MethodCallExpression mce = new MethodCallExpression( new ClassExpression(INVOKERHELPER_TYPE), target.getName(), new ArgumentListExpression(receiver, new ConstantExpression(methodName)) ); mce.setSafe(false); mce.setImplicitThis(false); mce.setMethodTarget(target); mce.visit(controller.getAcg()); }