private static void processSpecialNodeTypes(MethodDeclaration node) { processSpecialNodeTypes(node, node.getNameAsString()); }
String name = declaration.getNameAsString(); if (oldMethod.get(name) == null) { MethodDeclaration newMethod = old.getType(0)
String methodName = method.getNameAsString(); if (!methodName.endsWith(methodNameSuffix)) {
String methodName = method.getNameAsString(); if (!methodName.endsWith(methodNameSuffix)) {
protected void prepareReturnStmt(MethodDeclaration methodDeclaration, ReturnStmt returnStmt) { returnStmt.getExpression().ifPresent(expression -> { String returnStmtStr = expression.toString(); Matcher matcher = RETURN_STMT_PATTERN.matcher(returnStmtStr); if (!returnMap.containsKey(methodDeclaration.getNameAsString())) { returnMap.put(methodDeclaration.getNameAsString(), DfCollectionUtil.newArrayList()); } returnMap.get(methodDeclaration.getNameAsString()).add(matcher.find() ? matcher.group(0) : "##unanalyzable##"); }); } }
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.getNameAsString().equals(name)) { if (found != null) { throw new IllegalStateException("Ambiguous getName"); } found = md; } } } if (found == null) { throw new IllegalStateException("No method called " + name); } return found; }
@Override public Map<String, Object> initMap() { if (node instanceof NodeWithIdentifier) { NodeWithIdentifier<?> nwi = (NodeWithIdentifier<?>) node; map.put("name", nwi.getIdentifier()); } if (node instanceof MethodDeclaration) { MethodDeclaration md = (MethodDeclaration) node; map.put("name", md.getNameAsString()); } map.put("hashCode", node.hashCode()); map.put("node", node); return map; }
private Method findMethod(MethodDeclaration n) { Class<?> currentVisitedClass = getCurrentVisitedClass(); for (Method method : currentVisitedClass.getDeclaredMethods()) { if (method.getName().equals(n.getNameAsString()) && argsMatch(method.getParameterTypes(), n.getParameters())) { return method; } } return null; }
public void visit(MethodDeclaration n, Void arg) { // only display public methods if (!n.getModifiers().contains(Modifier.PUBLIC)) { return; } javadoc.append(String.format("\nh4. Operation: %s\n", n.getNameAsString())); try { Comment comment = n.getComment().get(); javadoc.append(formatJavadoc(comment.getContent())); } catch (NoSuchElementException e) { } } }
private Method findMethod(MethodDeclaration n) { Class<?> currentVisitedClass = getCurrentVisitedClass(); for (Method method : currentVisitedClass.getDeclaredMethods()) { if (method.getName().equals(n.getNameAsString()) && argsMatch(method.getParameterTypes(), n.getParameters())) { return method; } } return null; }
public void visit(MethodDeclaration n, Void arg) { try { n.getAnnotationByClass(Test.class).get(); } catch (NoSuchElementException e) { return; } javadoc.append(String.format("\nh4. Test: %s\n", n.getNameAsString())); try { Comment comment = n.getComment().get(); javadoc.append(formatJavadoc(comment.getContent())); } catch (NoSuchElementException e) { } } }
/** * <b>Note:</b> This will not return the actual identifier but only the simple names of the types (return type & parameter types). * Doing a full type resolving with all imports adds too much complexity at this point. * This is a best-effort approach. */ private MethodIdentifier calculateMethodIdentifier(MethodDeclaration method) { String[] parameters = method.getParameters().stream() .map(p -> p.getType().asString()) .map(p -> p.replace('.', '/')) .toArray(String[]::new); String returnType = method.getType().asString().replace('.', '/'); if (method.isStatic()) { return ofStatic(className, method.getNameAsString(), returnType, parameters); } return ofNonStatic(className, method.getNameAsString(), returnType, parameters); }
/** * <b>Note:</b> This will not return the actual identifier but only the simple names of the types (return type & parameter types). * Doing a full type resolving with all imports adds too much complexity at this point. * This is a best-effort approach. */ private MethodIdentifier calculateMethodIdentifier(MethodDeclaration method) { String[] parameters = method.getParameters().stream() .map(p -> p.getType().asString()) .map(p -> p.replace('.', '/')) .toArray(String[]::new); String returnType = method.getType().asString().replace('.', '/'); if (method.isStatic()) { return ofStatic(className, method.getNameAsString(), returnType, parameters); } return ofNonStatic(className, method.getNameAsString(), returnType, parameters); }
public String getKey() { if (declaration instanceof FieldDeclaration) { FieldDeclaration field = (FieldDeclaration) declaration; if (field.getVariables().size() != 1) { throw new RuntimeException( "Multiple fields in a single manual field declaration is not yet supported: " + field.toString()); } return "F:" + field.getVariable(0).getNameAsString(); } else if (declaration instanceof MethodDeclaration) { MethodDeclaration method = (MethodDeclaration) declaration; return "M:" + method.getNameAsString() + ":" + method.getParameters().stream() .map(p -> p.getType().toString()).collect(Collectors.joining(",")); } else if (declaration instanceof ConstructorDeclaration) { ConstructorDeclaration constructor = (ConstructorDeclaration) declaration; return "C:" + constructor.getParameters().stream().map(p -> p.getType().toString()) .collect(Collectors.joining(",")); } throw new RuntimeException( "Unsupported manual member type encountered: " + declaration.getClass().getName()); }
public String getName() { if (declaration instanceof MethodDeclaration) { return ((MethodDeclaration) declaration).getNameAsString(); } else if (declaration instanceof FieldDeclaration) { NodeList<VariableDeclarator> vars = ((FieldDeclaration) declaration).getVariables(); if (vars.size() == 1) { return vars.get(0).getNameAsString(); } } return null; } }
@Override public void visit(MethodDeclaration methodDeclaration, JobDocMeta jobDocMeta) { if (!methodDeclaration.getNameAsString().equals(jobDocMeta.getMethodName())) { return; } methodDeclaration.getBegin().ifPresent(begin -> { methodDeclaration.getEnd().ifPresent(end -> { jobDocMeta.setMethodLineCount(end.line - begin.line); }); }); String comment = adjustComment(methodDeclaration); if (DfStringUtil.is_NotNull_and_NotEmpty(comment)) { jobDocMeta.setMethodComment(comment); } super.visit(methodDeclaration, jobDocMeta); } }
public MethodImp parse(final String signature) { final StringBuilder sb = new StringBuilder(); sb.append("class A {\n"); if (!signature.contains("(")) { sb.append(signature.contains("()") ? signature : signature.concat("()")); } else { sb.append(signature); } if (!signature.contains("{}")) { sb.append(" {}"); } sb.append("\n}"); final MethodImp method = new MethodImp("NOT_DEFINED"); final CompilationUnit compilationUnit = JavaParser.parse(sb.toString()); compilationUnit.getClassByName("A").ifPresent(clazz -> { clazz.getMethods().forEach(md -> { method.name(md.getNameAsString()); method.visibility(this.getVisibility(md)); method.modifiers(this.getModifiers(md)); method.returnType(this.getReturnType(md)); method.exceptions(this.getExceptions(md)); md.getAnnotations().forEach(an -> this.getAnnotationMethod(an) .ifPresent(x -> method.annotation(x))); md.getParameters().forEach(p -> this.getField(p) .ifPresent(x -> method.parameters().add(x))); }); }); return method; }
@Override public void visit(MethodDeclaration method, ClassBuilder builder) { super.visit(method, builder); TypeFactory factory = new TypeFactory(packageName, imports); if (method.getParentNode().isPresent() && method.getParentNode().get() instanceof ClassOrInterfaceDeclaration && isClass((ClassOrInterfaceDeclaration) method.getParentNode().get()) && isGetter(method.getNameAsString(), method.getType().asString()) && !skipFieldOrGetter(method.getModifiers())) { builder.withGetter(new Method( method.getNameAsString(), factory.build(method.getType().asString(), method.getType() instanceof PrimitiveType) )); } }
public void index(MethodDeclaration methodDeclaration, int typeId) { List<Parameter> parameters = methodDeclaration.getParameters(); String name = methodDeclaration.getNameAsString(); int methodId = methodDao.save(new Method(name, methodDeclaration.isPublic(), methodDeclaration.isStatic(), methodDeclaration.isFinal(), methodDeclaration.isAbstract(), false, typeId)); for (Parameter parameter : parameters) { parameterIndexer.index(parameter, methodId); } }
@Test public void testSolveStaticallyImportedMemberType() throws FileNotFoundException { CompilationUnit cu = JavaParser.parse(new File(adaptPath("src/test/resources/issue276/foo/C.java"))); ClassOrInterfaceDeclaration cls = Navigator.demandClassOrInterface(cu, "C"); TypeSolver typeSolver = new CombinedTypeSolver( new ReflectionTypeSolver(), new JavaParserTypeSolver(adaptPath(new File("src/test/resources/issue276")))); List<MethodDeclaration> methods = Navigator.findAllNodesOfGivenClass(cls, MethodDeclaration.class); boolean isSolved = false; for (MethodDeclaration method: methods) { if (method.getNameAsString().equals("overrideMe")) { MethodContext context = new MethodContext(method, typeSolver); isSolved = context.solveType("FindMeIfYouCan", typeSolver).isSolved(); } } Assert.assertTrue(isSolved); } }