@Override public Optional<Value> solveSymbolAsValue(String name) { if (typeSolver == null) throw new IllegalArgumentException(); if (this.getDeclaration().hasVisibleField(name)) { return Optional.of(Value.from(this.getDeclaration().getVisibleField(name))); } // then to parent return getParent().solveSymbolAsValue(name); }
@Test(expected = MethodAmbiguityException.class) public void solveMethodAsUsageWithAmbiguosCall() throws ParseException { CompilationUnit cu = parseSample("ClassWithMethods"); ClassOrInterfaceDeclaration classOrInterfaceDeclaration = Navigator.demandClass(cu, "A"); Context context = new ClassOrInterfaceDeclarationContext(classOrInterfaceDeclaration, typeSolver); Optional<MethodUsage> ref = context.solveMethodAsUsage("foo5", ImmutableList.of(NullType.INSTANCE), new ReflectionTypeSolver()); } }
@Test public void solveSymbolAsValueReferringToInterfaceInheritedInstanceField() throws ParseException { CompilationUnit cu = parseSample("ClassWithSymbols"); ClassOrInterfaceDeclaration classOrInterfaceDeclaration = Navigator.demandClass(cu, "A"); ClassOrInterfaceDeclarationContext context = new ClassOrInterfaceDeclarationContext(classOrInterfaceDeclaration, typeSolver); Optional<Value> ref = context.solveSymbolAsValue("o", new MemoryTypeSolver()); assertEquals(true, ref.isPresent()); assertEquals("int", ref.get().getType().describe()); }
public ClassOrInterfaceDeclarationContext(ClassOrInterfaceDeclaration wrappedNode, TypeSolver typeSolver) { super(wrappedNode, typeSolver); this.javaParserTypeDeclarationAdapter = new JavaParserTypeDeclarationAdapter(wrappedNode, typeSolver, getDeclaration(), this); }
@Override public Optional<ResolvedType> solveGenericType(String name) { for (com.github.javaparser.ast.type.TypeParameter tp : wrappedNode.getTypeParameters()) { if (tp.getName().getId().equals(name)) { return Optional.of(new ResolvedTypeVariable(new JavaParserTypeParameter(tp, typeSolver))); } } return getParent().solveGenericType(name); }
@Test(expected = MethodAmbiguityException.class) public void solveMethodWithAmbiguosCall() throws ParseException { CompilationUnit cu = parseSample("ClassWithMethods"); ClassOrInterfaceDeclaration classOrInterfaceDeclaration = Navigator.demandClass(cu, "A"); Context context = new ClassOrInterfaceDeclarationContext(classOrInterfaceDeclaration, typeSolver); SymbolReference<ResolvedMethodDeclaration> ref = context.solveMethod("foo5", ImmutableList.of(NullType.INSTANCE), false, new ReflectionTypeSolver()); }
public ClassOrInterfaceDeclarationContext(ClassOrInterfaceDeclaration wrappedNode, TypeSolver typeSolver) { super(wrappedNode, typeSolver); this.javaParserTypeDeclarationAdapter = new JavaParserTypeDeclarationAdapter(wrappedNode, typeSolver, getDeclaration(), this); }
@Override public Optional<ResolvedType> solveGenericType(String name, TypeSolver typeSolver) { for (com.github.javaparser.ast.type.TypeParameter tp : wrappedNode.getTypeParameters()) { if (tp.getName().getId().equals(name)) { return Optional.of(new ResolvedTypeVariable(new JavaParserTypeParameter(tp, typeSolver))); } } return getParent().solveGenericType(name, typeSolver); }
@Test public void solveSymbolAsValueReferringToUnknownElement() throws ParseException { CompilationUnit cu = parseSample("ClassWithSymbols"); ClassOrInterfaceDeclaration classOrInterfaceDeclaration = Navigator.demandClass(cu, "A"); Context context = new ClassOrInterfaceDeclarationContext(classOrInterfaceDeclaration, typeSolver); Optional<Value> ref = context.solveSymbolAsValue("zzz", new MemoryTypeSolver()); assertEquals(false, ref.isPresent()); }
@Override public Optional<Value> solveSymbolAsValue(String name, TypeSolver typeSolver) { if (typeSolver == null) throw new IllegalArgumentException(); if (this.getDeclaration().hasVisibleField(name)) { return Optional.of(Value.from(this.getDeclaration().getVisibleField(name))); } // then to parent return getParent().solveSymbolAsValue(name, typeSolver); }
public ClassOrInterfaceDeclarationContext(ClassOrInterfaceDeclaration wrappedNode, TypeSolver typeSolver) { super(wrappedNode, typeSolver); this.javaParserTypeDeclarationAdapter = new JavaParserTypeDeclarationAdapter(wrappedNode, typeSolver, getDeclaration(), this); }
@Override public Optional<ResolvedType> solveGenericType(String name, TypeSolver typeSolver) { for (com.github.javaparser.ast.type.TypeParameter tp : wrappedNode.getTypeParameters()) { if (tp.getName().getId().equals(name)) { return Optional.of(new ResolvedTypeVariable(new JavaParserTypeParameter(tp, typeSolver))); } } return getParent().solveGenericType(name, typeSolver); }
@Test public void solveUnexistingGenericType() throws ParseException { CompilationUnit cu = parseSample("ClassWithTypeVariables"); ClassOrInterfaceDeclaration classOrInterfaceDeclaration = Navigator.demandClass(cu, "A"); Context context = new ClassOrInterfaceDeclarationContext(classOrInterfaceDeclaration, typeSolver); Optional<ResolvedType> d = context.solveGenericType("D", new MemoryTypeSolver()); assertEquals(false, d.isPresent()); }
@Override public Optional<Value> solveSymbolAsValue(String name, TypeSolver typeSolver) { if (typeSolver == null) throw new IllegalArgumentException(); if (this.getDeclaration().hasVisibleField(name)) { return Optional.of(Value.from(this.getDeclaration().getVisibleField(name))); } // then to parent return getParent().solveSymbolAsValue(name, typeSolver); }
@Test public void solveMethodAsUsageInheritedCase() throws ParseException { CompilationUnit cu = parseSample("ClassWithMethods"); ClassOrInterfaceDeclaration classOrInterfaceDeclaration = Navigator.demandClass(cu, "A"); Context context = new ClassOrInterfaceDeclarationContext(classOrInterfaceDeclaration, typeSolver); Optional<MethodUsage> ref = context.solveMethodAsUsage("foo2", ImmutableList.of(), new ReflectionTypeSolver()); assertEquals(true, ref.isPresent()); assertEquals("Super", ref.get().declaringType().getName()); assertEquals(0, ref.get().getNoParams()); }
@Override public SymbolReference<? extends ResolvedValueDeclaration> solveSymbol(String name, TypeSolver typeSolver) { if (typeSolver == null) throw new IllegalArgumentException(); if (this.getDeclaration().hasVisibleField(name)) { return SymbolReference.solved(this.getDeclaration().getVisibleField(name)); } // then to parent return getParent().solveSymbol(name, typeSolver); }
@Test public void solveMethodAsUsageOverrideCase() throws ParseException { CompilationUnit cu = parseSample("ClassWithMethods"); ClassOrInterfaceDeclaration classOrInterfaceDeclaration = Navigator.demandClass(cu, "A"); Context context = new ClassOrInterfaceDeclarationContext(classOrInterfaceDeclaration, typeSolver); Optional<MethodUsage> ref = context.solveMethodAsUsage("foo1", ImmutableList.of(), new ReflectionTypeSolver()); assertEquals(true, ref.isPresent()); assertEquals("A", ref.get().declaringType().getName()); assertEquals(0, ref.get().getNoParams()); }
@Override public SymbolReference<? extends ResolvedValueDeclaration> solveSymbol(String name) { if (typeSolver == null) throw new IllegalArgumentException(); if (this.getDeclaration().hasVisibleField(name)) { return SymbolReference.solved(this.getDeclaration().getVisibleField(name)); } // then to parent return getParent().solveSymbol(name); }
@Test public void solveMethodAsUsageWithPrimitiveParameters() throws ParseException { CompilationUnit cu = parseSample("ClassWithMethods"); ClassOrInterfaceDeclaration classOrInterfaceDeclaration = Navigator.demandClass(cu, "A"); Context context = new ClassOrInterfaceDeclarationContext(classOrInterfaceDeclaration, typeSolver); ResolvedType intType = ResolvedPrimitiveType.INT; Optional<MethodUsage> ref = context.solveMethodAsUsage("foo3", ImmutableList.of(intType), new ReflectionTypeSolver()); assertEquals(true, ref.isPresent()); assertEquals("A", ref.get().declaringType().getName()); assertEquals(1, ref.get().getNoParams()); }
@Override public SymbolReference<? extends ResolvedValueDeclaration> solveSymbol(String name, TypeSolver typeSolver) { if (typeSolver == null) throw new IllegalArgumentException(); if (this.getDeclaration().hasVisibleField(name)) { return SymbolReference.solved(this.getDeclaration().getVisibleField(name)); } // then to parent return getParent().solveSymbol(name, typeSolver); }