private JavaMethod createMethod (MethodDeclaration method) { String className = classStack.peek().getName(); String name = method.getName(); boolean isStatic = ModifierSet.hasModifier(method.getModifiers(), ModifierSet.STATIC); String returnType = method.getType().toString(); ArrayList<Argument> arguments = new ArrayList<Argument>(); if (method.getParameters() != null) { for (Parameter parameter : method.getParameters()) { arguments.add(new Argument(getArgumentType(parameter), parameter.getId().getName())); } } return new JavaMethod(className, name, isStatic, returnType, null, arguments, method.getBeginLine(), method.getEndLine()); }
private JavaMethod createMethod (MethodDeclaration method) { String className = classStack.peek().getName(); String name = method.getName(); boolean isStatic = ModifierSet.hasModifier(method.getModifiers(), ModifierSet.STATIC); String returnType = method.getType().toString(); ArrayList<Argument> arguments = new ArrayList<Argument>(); if (method.getParameters() != null) { for (Parameter parameter : method.getParameters()) { arguments.add(new Argument(getArgumentType(parameter), parameter.getId().getName())); } } return new JavaMethod(className, name, isStatic, returnType, null, arguments, method.getBeginLine(), method.getEndLine()); }
@Override public String getName() { return wrappedNode.getName().getId(); }
@Override public String getName() { return wrappedNode.getName().getId(); }
@Override public String getName() { return wrappedNode.getName().getId(); }
/** * @param methodDeclaration * @param position * @return true if given method starts at given position, false otherwise */ public static boolean isMethodDeclarationAtLine(MethodDeclaration methodDeclaration, Integer position) { if (methodDeclaration.getName().getBegin().isPresent()) { if (position == methodDeclaration.getName().getBegin().get().line) { return true; } } return false; }
public static MethodDeclaration demandMethod(TypeDeclaration<?> cd, String name) { MethodDeclaration found = null; for (BodyDeclaration<?> bd : cd.getMembers()) { if (bd instanceof MethodDeclaration) { MethodDeclaration md = (MethodDeclaration) bd; if (md.getName().getId().equals(name)) { if (found != null) { throw new IllegalStateException("Ambiguous getName"); } found = md; } } } if (found == null) { throw new IllegalStateException("No method with given name"); } return found; }
public static MethodDeclaration demandMethod(TypeDeclaration<?> cd, String name) { MethodDeclaration found = null; for (BodyDeclaration<?> bd : cd.getMembers()) { if (bd instanceof MethodDeclaration) { MethodDeclaration md = (MethodDeclaration) bd; if (md.getName().getId().equals(name)) { if (found != null) { throw new IllegalStateException("Ambiguous getName"); } found = md; } } } if (found == null) { throw new IllegalStateException("No method with given name"); } return found; }
@Override public void visit(MethodDeclaration n, Void arg) { methods.add(new MethodInfo(n.getBegin().get().line, n.getEnd().get().line, n.getName().toString())); super.visit(n, arg); }
@Override public boolean doIsEquals(MethodDeclaration first, MethodDeclaration second) { if (!StringUtils.equals(first.getName(), second.getName())) return false; if (!isParametersEquals(first.getParameters(), second.getParameters())) return false; if (!isTypeParameterEquals(first.getTypeParameters(), second.getTypeParameters())) return false; return true; } }
@Override public void visit(MethodDeclaration methodDeclaration, Object arg) { super.visit(methodDeclaration, arg); boolean isATestMethod = false; for(AnnotationExpr expr : methodDeclaration.getAnnotations()) { if (expr.getName().getName().equals(Test.class.getSimpleName()) || expr.getName().getName().equals(Test.class.getName())) { isATestMethod = true; break; } } if (!isATestMethod) { return; } methodAnnotationMap.put(methodDeclaration.getName(), MutablePair.of(methodDeclaration, new HashMap<String, AnnotationExpr>())); for(AnnotationExpr expr : methodDeclaration.getAnnotations()) { String[] split = expr.getName().getName().split("\\."); methodAnnotationMap.get(methodDeclaration.getName()).getRight().put(split[split.length - 1], expr); } }
@Override public void visit(MethodDeclaration methodDeclaration, Object arg) { super.visit(methodDeclaration, arg); boolean isATestMethod = false; for(AnnotationExpr expr : methodDeclaration.getAnnotations()) { if (expr.getName().getName().equals(Test.class.getSimpleName()) || expr.getName().getName().equals(Test.class.getName())) { isATestMethod = true; break; } } if (!isATestMethod) { return; } methodAnnotationMap.put(methodDeclaration.getName(), MutablePair.of(methodDeclaration, new HashMap<String, AnnotationExpr>())); for(AnnotationExpr expr : methodDeclaration.getAnnotations()) { String[] split = expr.getName().getName().split("\\."); methodAnnotationMap.get(methodDeclaration.getName()).getRight().put(split[split.length - 1], expr); } }
private void getParameterNames(MethodDeclaration methodDeclaration, boolean isInterface) { final EnumSet<Modifier> modifiers = methodDeclaration.getModifiers(); if (isInterface || modifiers.contains(Modifier.PUBLIC)) { String methodName = methodDeclaration.getName().getIdentifier(); List<Parameter> parameters = methodDeclaration.getParameters(); names.className = this.className; List<List<ParameterName>> parameterNames = names.names.computeIfAbsent(methodName, k -> new ArrayList<>(4)); final List<ParameterName> temp = new ArrayList<>(); for (final Parameter parameter : parameters) { ParameterName parameterName = new ParameterName(); String type = parameter.getType().toString(); String name = parameter.getName().getIdentifier(); if (name.contains("[]")) { type = type + "[]"; name = name.replace("[]", ""); } parameterName.type = type; parameterName.name = name; temp.add(parameterName); } parameterNames.add(temp); } } }
@Override public void visit(MethodDeclaration n, Void arg) { TestStep step = new TestStep(); step.stepMethod = n.getName().toString(); if (n.getJavadocComment().isPresent()) { step.stepDetails = n.getJavadocComment().get().getContent().replaceAll("\\s*\\*\\s+", "\n").trim(); } else { step.stepDetails = ""; } if (n.getAnnotationByName("Given").isPresent()) { step.stepName = getAnnotationString(n.getAnnotationByName("Given").get()); } else if (n.getAnnotationByName("When").isPresent()) { step.stepName = getAnnotationString(n.getAnnotationByName("When").get()); } else if (n.getAnnotationByName("Then").isPresent()) { step.stepName = getAnnotationString(n.getAnnotationByName("Then").get()); } else { step.stepName = n.getDeclarationAsString().split("throws ")[0].trim(); } steps.add(step); }
@Test public void issue113providedCodeDoesNotCrash() throws FileNotFoundException { String pathToSourceFile = adaptPath("src/test/resources/issue113/com/foo/Widget.java"); CompilationUnit cu = JavaParser.parse(new File(pathToSourceFile)); JavaParserFacade parserFacade = JavaParserFacade.get(typeSolver); MethodDeclaration methodDeclaration = cu.getNodesByType(MethodDeclaration.class).stream() .filter(node -> node.getName().getIdentifier().equals("doSomething")).findAny().orElse(null); methodDeclaration.getNodesByType(MethodCallExpr.class).forEach(parserFacade::solve); }
private static MethodInfo getMethodInfo(String packageName, List<String> imports, MethodDeclaration md) { MethodInfo info = new MethodInfo(); for (AnnotationExpr ae : md.getAnnotations()) { AnnotationInfo annotation = getAnnotationInfo(packageName, imports, ae); info.annotations.add(annotation); } info.name = md.getName(); info.isStatic = ModifierSet.isStatic(md.getModifiers()); info.visibility = Visibility.fromModifiers(md.getModifiers()); info.signature = md.getDeclarationAsString(); return info; }
private static MethodInfo getMethodInfo(String packageName, List<String> imports, MethodDeclaration md) { MethodInfo info = new MethodInfo(); for (AnnotationExpr ae : md.getAnnotations()) { AnnotationInfo annotation = getAnnotationInfo(packageName, imports, ae); info.annotations.add(annotation); } info.name = md.getName(); info.isStatic = ModifierSet.isStatic(md.getModifiers()); info.visibility = Visibility.fromModifiers(md.getModifiers()); info.signature = md.getDeclarationAsString(); return info; }
private JavaMethod createMethod (MethodDeclaration method) { String className = classStack.peek().getName(); String name = method.getName(); boolean isStatic = ModifierSet.hasModifier(method.getModifiers(), ModifierSet.STATIC); String returnType = method.getType().toString(); ArrayList<Argument> arguments = new ArrayList<Argument>(); if (method.getParameters() != null) { for (Parameter parameter : method.getParameters()) { arguments.add(new Argument(getArgumentType(parameter), parameter.getId().getName())); } } return new JavaMethod(className, name, isStatic, returnType, null, arguments, method.getBeginLine(), method.getEndLine()); }
public static MethodDeclaration mergeMethod(MethodDeclaration one, MethodDeclaration two) { if (isAllNull(one, two)) return null; MethodDeclaration md = null; if (isAllNotNull(one, two)) { md = new MethodDeclaration(); md.setName(one.getName()); md.setType(mergeSelective(one.getType(), two.getType())); md.setParameters(mergeParameters(one.getParameters(), two.getParameters())); md.setTypeParameters(findFirstNotNull(one.getTypeParameters(), two.getTypeParameters())); md.setThrows(mergeListNoDuplicate(one.getThrows(), two.getThrows())); md.setAnnotations(mergeListNoDuplicate(one.getAnnotations(), two.getAnnotations())); md.setArrayCount(one.getArrayCount()); md.setModifiers(mergeModifiers(one.getModifiers(), two.getModifiers())); md.setBody(mergeBlock(one.getBody(), two.getBody())); md.setJavaDoc(mergeSelective(one.getJavaDoc(), two.getJavaDoc())); LOG.info("merge MethodDeclaration --> {}", md.getName()); } else { md = findFirstNotNull(one, two); LOG.info("add MethodDeclaration --> {}", md.getName()); } return md; }
@Override public MethodDeclaration doMerge(MethodDeclaration first, MethodDeclaration second) { MethodDeclaration md = new MethodDeclaration(); md.setName(first.getName()); md.setType(mergeSingle(first.getType(), second.getType())); md.setJavaDoc(mergeSingle(first.getJavaDoc(), second.getJavaDoc())); md.setModifiers(mergeModifiers(first.getModifiers(), second.getModifiers())); md.setDefault(first.isDefault() || second.isDefault()); md.setArrayCount(Math.max(first.getArrayCount(), second.getArrayCount())); md.setAnnotations(mergeCollections(first.getAnnotations(), second.getAnnotations())); md.setThrows(mergeListNoDuplicate(first.getThrows(), second.getThrows(), false)); md.setParameters(mergeCollectionsInOrder(first.getParameters(), second.getParameters())); md.setTypeParameters(mergeCollectionsInOrder(first.getTypeParameters(), second.getTypeParameters())); md.setBody(mergeSingle(first.getBody(), second.getBody())); return md; }