use of com.github.javaparser.ast.body.MethodDeclaration in project javaparser by javaparser.
the class TypeCastingGenerator method generateIfType.
private void generateIfType(CompilationUnit nodeCu, CompilationUnit baseCu, ClassOrInterfaceDeclaration nodeCoid, ClassOrInterfaceDeclaration baseCoid, String typeName) {
final MethodDeclaration ifTypeBaseMethod = (MethodDeclaration) parseBodyDeclaration(f("public void if%s(Consumer<%s> action) { }", typeName, typeName));
final MethodDeclaration ifTypeNodeMethod = (MethodDeclaration) parseBodyDeclaration(f("public void if%s(Consumer<%s> action) { action.accept(this); }", typeName, typeName));
addOrReplaceWhenSameSignature(baseCoid, ifTypeBaseMethod);
addOrReplaceWhenSameSignature(nodeCoid, ifTypeNodeMethod);
baseCu.addImport(Consumer.class);
nodeCu.addImport(Consumer.class);
}
use of com.github.javaparser.ast.body.MethodDeclaration in project javaparser by javaparser.
the class TypeCastingGenerator method generateToType.
private void generateToType(CompilationUnit nodeCu, CompilationUnit baseCu, ClassOrInterfaceDeclaration nodeCoid, ClassOrInterfaceDeclaration baseCoid, String typeName) {
baseCu.addImport(Optional.class);
nodeCu.addImport(Optional.class);
final MethodDeclaration asTypeBaseMethod = (MethodDeclaration) parseBodyDeclaration(f("public Optional<%s> to%s() { return Optional.empty(); }", typeName, typeName, typeName));
final MethodDeclaration asTypeNodeMethod = (MethodDeclaration) parseBodyDeclaration(f("@Override public Optional<%s> to%s() { return Optional.of(this); }", typeName, typeName));
addOrReplaceWhenSameSignature(baseCoid, asTypeBaseMethod);
addOrReplaceWhenSameSignature(nodeCoid, asTypeNodeMethod);
}
use of com.github.javaparser.ast.body.MethodDeclaration in project javaparser by javaparser.
the class ContextTest method resolveReferenceUsingQualifiedName.
@Test
public void resolveReferenceUsingQualifiedName() {
CompilationUnit cu = parseSample("Navigator2");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration referencesToField = Navigator.demandClass(cu, "Navigator");
MethodDeclaration method = Navigator.demandMethod(referencesToField, "findType");
Parameter param = method.getParameters().get(0);
ResolvedClassDeclaration compilationUnitDecl = mock(ResolvedClassDeclaration.class);
when(compilationUnitDecl.getName()).thenReturn("CompilationUnit");
when(compilationUnitDecl.getQualifiedName()).thenReturn("com.github.javaparser.ast.CompilationUnit");
TypeSolver typeSolver = mock(TypeSolver.class);
// when(typeSolver.tryToSolveType("java.lang.com.github.javaparser.ast.CompilationUnit")).thenReturn(SymbolReference.unsolved(ClassDeclaration.class));
when(typeSolver.getRoot()).thenReturn(typeSolver);
when(typeSolver.solveType("java.lang.Object")).thenReturn(new ReflectionClassDeclaration(Object.class, typeSolver));
when(typeSolver.tryToSolveType("com.github.javaparser.ast.CompilationUnit")).thenReturn(SymbolReference.solved(compilationUnitDecl));
SymbolSolver symbolSolver = new SymbolSolver(typeSolver);
SymbolReference<? extends ResolvedTypeDeclaration> ref = symbolSolver.solveType("com.github.javaparser.ast.CompilationUnit", param);
assertEquals(true, ref.isSolved());
assertEquals("CompilationUnit", ref.getCorrespondingDeclaration().getName());
assertEquals("com.github.javaparser.ast.CompilationUnit", ref.getCorrespondingDeclaration().getQualifiedName());
}
use of com.github.javaparser.ast.body.MethodDeclaration in project javaparser by javaparser.
the class ContextTest method resolveReferenceToImportedType.
@Test
public void resolveReferenceToImportedType() {
CompilationUnit cu = parseSample("Navigator");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration referencesToField = Navigator.demandClass(cu, "Navigator");
MethodDeclaration method = Navigator.demandMethod(referencesToField, "findType");
Parameter param = method.getParameters().get(0);
ResolvedClassDeclaration compilationUnitDecl = mock(ResolvedClassDeclaration.class);
when(compilationUnitDecl.getName()).thenReturn("CompilationUnit");
when(compilationUnitDecl.getQualifiedName()).thenReturn("com.github.javaparser.ast.CompilationUnit");
TypeSolver typeSolver = mock(TypeSolver.class);
when(typeSolver.getRoot()).thenReturn(typeSolver);
when(typeSolver.solveType("java.lang.Object")).thenReturn(new ReflectionClassDeclaration(Object.class, typeSolver));
when(typeSolver.tryToSolveType("com.github.javaparser.ast.CompilationUnit")).thenReturn(SymbolReference.solved(compilationUnitDecl));
SymbolSolver symbolSolver = new SymbolSolver(typeSolver);
SymbolReference<? extends ResolvedTypeDeclaration> ref = symbolSolver.solveType("CompilationUnit", param);
assertEquals(true, ref.isSolved());
assertEquals("CompilationUnit", ref.getCorrespondingDeclaration().getName());
assertEquals("com.github.javaparser.ast.CompilationUnit", ref.getCorrespondingDeclaration().getQualifiedName());
}
use of com.github.javaparser.ast.body.MethodDeclaration in project javaparser by javaparser.
the class ContextTest method resolveTypeUsageOfCascadeMethodInGenericClass.
@Test
public void resolveTypeUsageOfCascadeMethodInGenericClass() throws ParseException, IOException {
CompilationUnit cu = parseSample("Navigator");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Navigator");
MethodDeclaration method = Navigator.demandMethod(clazz, "findType");
MethodCallExpr callToFilter = Navigator.findMethodCall(method, "filter").get();
String pathToJar = adaptPath("src/test/resources/javaparser-core-2.1.0.jar");
TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(), new JarTypeSolver(pathToJar));
MethodUsage filterUsage = JavaParserFacade.get(typeSolver).solveMethodAsUsage(callToFilter);
assertEquals("java.util.stream.Stream<com.github.javaparser.ast.body.TypeDeclaration>", filterUsage.returnType().describe());
}
Aggregations