@Override public String getContainerQualifiedName() { if (this.container instanceof ResolvedReferenceTypeDeclaration) { return ((ResolvedReferenceTypeDeclaration) this.container).getQualifiedName(); } else if (this.container instanceof ResolvedMethodLikeDeclaration) { return ((ResolvedMethodLikeDeclaration) this.container).getQualifiedName(); } throw new UnsupportedOperationException(); }
@Override public String getContainerQualifiedName() { if (container instanceof ResolvedReferenceTypeDeclaration) { return ((ResolvedReferenceTypeDeclaration) container).getQualifiedName(); } else { return ((ResolvedMethodLikeDeclaration) container).getQualifiedSignature(); } }
@Override public String getContainerQualifiedName() { if (container instanceof ResolvedReferenceTypeDeclaration) { return ((ResolvedReferenceTypeDeclaration) container).getQualifiedName(); } else { return ((ResolvedMethodLikeDeclaration) container).getQualifiedSignature(); } }
@Override public String getContainerQualifiedName() { if (container instanceof ResolvedReferenceTypeDeclaration) { return ((ResolvedReferenceTypeDeclaration) container).getQualifiedName(); } else { return ((ResolvedMethodLikeDeclaration) container).getQualifiedSignature(); } }
@Override public String getContainerQualifiedName() { if (this.container instanceof ResolvedReferenceTypeDeclaration) { return ((ResolvedReferenceTypeDeclaration) this.container).getQualifiedName(); } else if (this.container instanceof ResolvedMethodLikeDeclaration) { return ((ResolvedMethodLikeDeclaration) this.container).getQualifiedName(); } throw new UnsupportedOperationException(); }
@Override public String getContainerQualifiedName() { if (this.container instanceof ResolvedReferenceTypeDeclaration) { return ((ResolvedReferenceTypeDeclaration) this.container).getQualifiedName(); } else if (this.container instanceof ResolvedMethodLikeDeclaration) { return ((ResolvedMethodLikeDeclaration) this.container).getQualifiedName(); } throw new UnsupportedOperationException(); }
@Override public String getContainerQualifiedName() { ResolvedTypeParametrizable container = getContainer(); if (container instanceof ResolvedReferenceTypeDeclaration) { return ((ResolvedReferenceTypeDeclaration) container).getQualifiedName(); } else if (container instanceof JavaParserConstructorDeclaration) { return ((JavaParserConstructorDeclaration) container).getQualifiedSignature(); } else { return ((JavaParserMethodDeclaration) container).getQualifiedSignature(); } }
@Override public String getContainerQualifiedName() { ResolvedTypeParametrizable container = getContainer(); if (container instanceof ResolvedReferenceTypeDeclaration) { return ((ResolvedReferenceTypeDeclaration) container).getQualifiedName(); } else if (container instanceof JavaParserConstructorDeclaration) { return ((JavaParserConstructorDeclaration) container).getQualifiedSignature(); } else { return ((JavaParserMethodDeclaration) container).getQualifiedSignature(); } }
@Override public String getContainerQualifiedName() { ResolvedTypeParametrizable container = getContainer(); if (container instanceof ResolvedReferenceTypeDeclaration) { return ((ResolvedReferenceTypeDeclaration) container).getQualifiedName(); } else if (container instanceof JavaParserConstructorDeclaration) { return ((JavaParserConstructorDeclaration) container).getQualifiedSignature(); } else { return ((JavaParserMethodDeclaration) container).getQualifiedSignature(); } }
@Override public boolean canBeAssignedTo(ResolvedReferenceTypeDeclaration other) { if (other instanceof LambdaArgumentTypePlaceholder) { return isFunctionalInterface(); } if (other.getQualifiedName().equals(getQualifiedName())) { return true; } if (this.clazz.getSuperclass() != null && new ReflectionClassDeclaration(clazz.getSuperclass(), typeSolver).canBeAssignedTo(other)) { return true; } for (Class<?> interfaze : clazz.getInterfaces()) { if (new ReflectionInterfaceDeclaration(interfaze, typeSolver).canBeAssignedTo(other)) { return true; } } return false; }
@Override public boolean canBeAssignedTo(ResolvedReferenceTypeDeclaration other) { if (other instanceof LambdaArgumentTypePlaceholder) { return isFunctionalInterface(); } if (other.getQualifiedName().equals(getQualifiedName())) { return true; } if (this.clazz.getSuperclass() != null && new ReflectionClassDeclaration(clazz.getSuperclass(), typeSolver).canBeAssignedTo(other)) { return true; } for (Class<?> interfaze : clazz.getInterfaces()) { if (new ReflectionInterfaceDeclaration(interfaze, typeSolver).canBeAssignedTo(other)) { return true; } } return false; }
private void assertCanSolveGenericMethodCallMustUseProvidedTypeArgs(String callMethodName) throws ParseException { MethodCallExpr methodCallExpr = getMethodCallExpr("genericMethodTest", callMethodName); CombinedTypeSolver typeSolver = createTypeSolver(); MethodCallExprContext context = new MethodCallExprContext(methodCallExpr, typeSolver); Optional<MethodUsage> ref = context.solveMethodAsUsage(callMethodName, Collections.emptyList(), typeSolver); assertTrue(ref.isPresent()); assertEquals("MethodCalls", ref.get().declaringType().getQualifiedName()); assertEquals(Collections.singletonList("java.lang.Integer"), ref.get().typeParametersMap().getTypes().stream().map(ty -> ty.asReferenceType().describe()).collect(Collectors.toList())); }
private void assertCanSolveGenericMethodCallCanInferFromArguments(String callMethodName) throws ParseException { MethodCallExpr methodCallExpr = getMethodCallExpr("genericMethodTest", callMethodName); CombinedTypeSolver typeSolver = createTypeSolver(); MethodCallExprContext context = new MethodCallExprContext(methodCallExpr, typeSolver); ResolvedReferenceTypeDeclaration stringType = typeSolver.solveType("java.lang.String"); List<ResolvedType> argumentsTypes = new ArrayList<>(); argumentsTypes.add(new ReferenceTypeImpl(stringType, typeSolver)); Optional<MethodUsage> ref = context.solveMethodAsUsage(callMethodName, argumentsTypes, typeSolver); assertTrue(ref.isPresent()); assertEquals("MethodCalls", ref.get().declaringType().getQualifiedName()); assertEquals(Collections.singletonList("java.lang.String"), ref.get().typeParametersMap().getTypes().stream().map(ty -> ty.asReferenceType().describe()).collect(Collectors.toList())); } }
@Test public void solveNestedMethodCallExprContextWithoutScope() throws ParseException { MethodCallExpr methodCallExpr = getMethodCallExpr("bar1", "foo"); CombinedTypeSolver typeSolver = createTypeSolver(); Context context = new MethodCallExprContext(methodCallExpr, typeSolver); Optional<MethodUsage> ref = context.solveMethodAsUsage("foo", Collections.emptyList(), typeSolver); assertTrue(ref.isPresent()); assertEquals("MethodCalls", ref.get().declaringType().getQualifiedName()); }
@Test public void solveTypeInSamePackage() throws ParseException { CompilationUnit cu = parseSample("CompilationUnitWithImports"); Context context = new CompilationUnitContext(cu, typeSolver); ResolvedReferenceTypeDeclaration otherClass = EasyMock.createMock(ResolvedReferenceTypeDeclaration.class); EasyMock.expect(otherClass.getQualifiedName()).andReturn("com.foo.OtherClassInSamePackage"); MemoryTypeSolver memoryTypeSolver = new MemoryTypeSolver(); memoryTypeSolver.addDeclaration("com.foo.OtherClassInSamePackage", otherClass); EasyMock.replay(otherClass); SymbolReference<ResolvedTypeDeclaration> ref = context.solveType("OtherClassInSamePackage", memoryTypeSolver); assertEquals(true, ref.isSolved()); assertEquals("com.foo.OtherClassInSamePackage", ref.getCorrespondingDeclaration().getQualifiedName()); }
@Test public void resolveReferenceToLambdaParamSimplified() throws ParseException, IOException { CompilationUnit cu = parseSample("NavigatorSimplified"); com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Navigator"); MethodDeclaration method = Navigator.demandMethod(clazz, "findType"); MethodCallExpr call = Navigator.findMethodCall(method, "isEmpty"); TypeSolver typeSolver = new ReflectionTypeSolver(); SymbolSolver symbolSolver = new SymbolSolver(typeSolver); MethodUsage ref = symbolSolver.solveMethod("isEmpty", Collections.emptyList(), call); assertEquals("isEmpty", ref.getName()); assertEquals("java.lang.String", ref.declaringType().getQualifiedName()); }
@Test public void resolveReferenceToMethodCalledOnArrayAccess() throws ParseException, IOException { CompilationUnit cu = parseSample("ArrayAccess"); com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "ArrayAccess"); MethodDeclaration method = Navigator.demandMethod(clazz, "access"); MethodCallExpr callToTrim = Navigator.findMethodCall(method, "trim"); File src = adaptPath(new File("src/test/resources")); TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(), new JavaParserTypeSolver(src)); SymbolSolver symbolSolver = new SymbolSolver(typeSolver); MethodUsage ref = symbolSolver.solveMethod("trim", Collections.emptyList(), callToTrim); assertEquals("trim", ref.getName()); assertEquals("java.lang.String", ref.declaringType().getQualifiedName()); }
@Test public void resolveCascadeOfReferencesToMethod() throws ParseException, IOException { CompilationUnit cu = parseSample("Navigator"); com.github.javaparser.ast.body.ClassOrInterfaceDeclaration referencesToField = Navigator.demandClass(cu, "Navigator"); MethodDeclaration method = Navigator.demandMethod(referencesToField, "findType"); MethodCallExpr callToStream = Navigator.findMethodCall(method, "stream"); String pathToJar = adaptPath("src/test/resources/javaparser-core-2.1.0.jar"); TypeSolver typeSolver = new CombinedTypeSolver(new JarTypeSolver(pathToJar), new ReflectionTypeSolver(true)); SymbolSolver symbolSolver = new SymbolSolver(typeSolver); MethodUsage ref = symbolSolver.solveMethod("stream", Collections.emptyList(), callToStream); assertEquals("stream", ref.getName()); assertEquals("java.util.Collection", ref.declaringType().getQualifiedName()); }
@Test public void resolveReferenceToMethod() throws ParseException, IOException { CompilationUnit cu = parseSample("Navigator"); com.github.javaparser.ast.body.ClassOrInterfaceDeclaration referencesToField = Navigator.demandClass(cu, "Navigator"); MethodDeclaration method = Navigator.demandMethod(referencesToField, "findType"); MethodCallExpr callToGetTypes = Navigator.findMethodCall(method, "getTypes"); String pathToJar = adaptPath("src/test/resources/javaparser-core-2.1.0.jar"); TypeSolver typeSolver = new CombinedTypeSolver(new JarTypeSolver(pathToJar), new ReflectionTypeSolver(true)); SymbolSolver symbolSolver = new SymbolSolver(typeSolver); MethodUsage ref = symbolSolver.solveMethod("getTypes", Collections.emptyList(), callToGetTypes); assertEquals("getTypes", ref.getName()); assertEquals("com.github.javaparser.ast.CompilationUnit", ref.declaringType().getQualifiedName()); //verify(typeSolver); }
@Test public void resolveReferenceToCallOnLambdaParam() 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 callToGetName = Navigator.findMethodCall(method, "getName"); String pathToJar = adaptPath("src/test/resources/javaparser-core-2.1.0.jar"); TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(), new JarTypeSolver(pathToJar)); MethodUsage methodUsage = JavaParserFacade.get(typeSolver).solveMethodAsUsage(callToGetName); assertEquals("getName", methodUsage.getName()); assertEquals("com.github.javaparser.ast.body.TypeDeclaration", methodUsage.declaringType().getQualifiedName()); }