/** * This method returns the global signature of a method as a string. * * @param methodDeclaration * @return */ public static String getFullMethodSignature(MethodDeclaration methodDeclaration) { ResolvedMethodDeclaration resolvedMethod = methodDeclaration.resolve(); return resolvedMethod.getQualifiedSignature(); }
/** * This method returns the global signature of a method as a string. * * @param methodDeclaration * @param position * @return * @throws BotRefactoringException */ private String getFullMethodSignature(MethodDeclaration methodDeclaration, Integer position) throws BotRefactoringException { // If method is at the refactored position if (methodDeclaration.getName().getBegin().isPresent()) { if (position == methodDeclaration.getName().getBegin().get().line) { try { ResolvedMethodDeclaration resolvedMethod = methodDeclaration.resolve(); return resolvedMethod.getQualifiedSignature(); } catch (Exception e) { throw new BotRefactoringException("Method '" + methodDeclaration.getSignature().asString() + "' can't be resolved. It might have parameters from external projects/libraries or method might be inside a class that extends a generic class! Error: " + e); } } } return null; }
resolvedMethodLikeDeclaration = callableDeclaration.asConstructorDeclaration().resolve(); } else { resolvedMethodLikeDeclaration = callableDeclaration.asMethodDeclaration().resolve();
resolvedMethodLikeDeclaration = callableDeclaration.asConstructorDeclaration().resolve(); } else { resolvedMethodLikeDeclaration = callableDeclaration.asMethodDeclaration().resolve();
/** * This method returns the global signature of a method as a string. * * @param methodDeclaration * @param position * @return * @throws BotRefactoringException */ private String getFullMethodSignature(MethodDeclaration methodDeclaration, Integer position, String parameterName) throws BotRefactoringException { // If method is at the refactored position if (methodDeclaration.getName().getBegin().isPresent()) { if (position == methodDeclaration.getName().getBegin().get().line) { // Check if method has parameter if (!methodDeclaration.getParameterByName(parameterName).isPresent()) { throw new BotRefactoringException("Method '" + methodDeclaration.getSignature() + "' does not have parameter '" + parameterName + "'!"); } try { ResolvedMethodDeclaration resolvedMethod = methodDeclaration.resolve(); return resolvedMethod.getQualifiedSignature(); } catch (Exception e) { throw new BotRefactoringException("Method '" + methodDeclaration.getSignature().asString() + "' can't be resolved. It might have parameters from external projects/libraries or method might be inside a class that extends a generic class! Error: " + e); } } } return null; }
resolvedMethodLikeDeclaration = callableDeclaration.asConstructorDeclaration().resolve(); } else { resolvedMethodLikeDeclaration = callableDeclaration.asMethodDeclaration().resolve();
@Test public void methodDeclarationResolve() throws IOException { File f = adaptPath(new File("src/test/resources/javaparser_new_src/javaparser-core/com/github/javaparser/ast/CompilationUnit.java")); CompilationUnit cu = parseWithSymbolResolution(f); ClassOrInterfaceDeclaration classDeclaration = (ClassOrInterfaceDeclaration) cu.getType(0); assertEquals("CompilationUnit", classDeclaration.getNameAsString()); MethodDeclaration declaration = classDeclaration.getMethodsByName("getComments").get(0); ResolvedMethodDeclaration resolvedDeclaration = declaration.resolve(); assertEquals("getComments", resolvedDeclaration.getName()); assertEquals(0, resolvedDeclaration.getNumberOfParams()); }
@Test public void resolveMethodDeclaration() throws ParseException { TypeSolver typeSolver = new ReflectionTypeSolver(); CompilationUnit cu = parseSample("SymbolResolverExample"); new JavaSymbolSolver(typeSolver).inject(cu); MethodDeclaration methodDeclaration = cu.getClassByName("A").get().getMethods().get(0); ResolvedMethodDeclaration resolvedMethodDeclaration = methodDeclaration.resolve(); assertEquals("foo", resolvedMethodDeclaration.getName()); assertEquals("A[]", resolvedMethodDeclaration.getReturnType().describe()); assertEquals("java.lang.String[]", resolvedMethodDeclaration.getParam(0).getType().describe()); assertEquals("int[]", resolvedMethodDeclaration.getParam(1).getType().describe()); }