Skip to content

Commit

Permalink
Merge 9ac1cd8 into 9266589
Browse files Browse the repository at this point in the history
  • Loading branch information
iTakeshi committed May 25, 2020
2 parents 9266589 + 9ac1cd8 commit f003e42
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ public SimpleName getName() {
return name;
}

public String getNameWithScope() {
StringBuilder str = new StringBuilder();
getScope().ifPresent(s -> str.append(s.getNameWithScope()).append("."));
str.append(name.asString());
return str.toString();
}

@Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
public Optional<ClassOrInterfaceType> getScope() {
return Optional.ofNullable(scope);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,21 @@ public Optional<ResolvedType> solveGenericType(String name) {

@Override
public SymbolReference<ResolvedTypeDeclaration> solveType(String name) {
// First attempt to resolve against implemented classes - cf. issue #2195
if (wrappedNode.getName().getId().equals(name)) {
return SymbolReference.solved(JavaParserFacade.get(typeSolver).getTypeDeclaration(wrappedNode));
}

for (ClassOrInterfaceType implementedType : wrappedNode.getImplementedTypes()) {
if (implementedType.getName().getId().equals(name)) {
return JavaParserFactory.getContext(wrappedNode.getParentNode().orElse(null), typeSolver).solveType(name);
return JavaParserFactory.getContext(wrappedNode.getParentNode().orElse(null), typeSolver)
.solveType(implementedType.getNameWithScope());
}
}

for (ClassOrInterfaceType extendedType : wrappedNode.getExtendedTypes()) {
if (extendedType.getName().getId().equals(name)) {
return JavaParserFactory.getContext(wrappedNode.getParentNode().orElse(null), typeSolver).solveType(name);
return JavaParserFactory.getContext(wrappedNode.getParentNode().orElse(null), typeSolver)
.solveType(extendedType.getNameWithScope());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2019 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/

package com.github.javaparser.symbolsolver.resolution;

import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.symbolsolver.JavaSymbolSolver;
import com.github.javaparser.symbolsolver.javaparser.Navigator;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.io.IOException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

/**
* Tests resolution of implemented/extended types
*
* @author Takeshi D. Itoh
*/
class ImplementedOrExtendedTypeResolutionTest extends AbstractResolutionTest {

@AfterEach
void unConfigureSymbolSolver() {
// unconfigure symbol solver so as not to potentially disturb tests in other classes
StaticJavaParser.getConfiguration().setSymbolResolver(null);
}

@Test
void solveImplementedTypes() {
StaticJavaParser.getConfiguration().setSymbolResolver(new JavaSymbolSolver(new ReflectionTypeSolver()));

CompilationUnit cu = parseSample("ImplementedOrExtendedTypeResolution/ImplementedOrExtendedTypeResolution");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "InterfaceTest");
assertEquals(clazz.getFieldByName("field_i1").get().resolve().getType().describe(), "I1");
assertEquals(clazz.getFieldByName("field_i2").get().resolve().getType().describe(), "I2.I2_1");
assertEquals(clazz.getFieldByName("field_i3").get().resolve().getType().describe(), "I3.I3_1.I3_1_1");
}

@Test
void solveExtendedType1() {
StaticJavaParser.getConfiguration().setSymbolResolver(new JavaSymbolSolver(new ReflectionTypeSolver()));

CompilationUnit cu = parseSample("ImplementedOrExtendedTypeResolution/ImplementedOrExtendedTypeResolution");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "ClassTest1");
assertEquals(clazz.getFieldByName("field_c1").get().resolve().getType().describe(), "C1");
}

@Test
void solveExtendedType2() {
StaticJavaParser.getConfiguration().setSymbolResolver(new JavaSymbolSolver(new ReflectionTypeSolver()));

CompilationUnit cu = parseSample("ImplementedOrExtendedTypeResolution/ImplementedOrExtendedTypeResolution");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "ClassTest2");
assertEquals(clazz.getFieldByName("field_c2").get().resolve().getType().describe(), "C2.C2_1");
}

@Test
void solveExtendedType3() {
StaticJavaParser.getConfiguration().setSymbolResolver(new JavaSymbolSolver(new ReflectionTypeSolver()));

CompilationUnit cu = parseSample("ImplementedOrExtendedTypeResolution/ImplementedOrExtendedTypeResolution");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "ClassTest3");
assertEquals(clazz.getFieldByName("field_c3").get().resolve().getType().describe(), "C3.C3_1.C3_1_1");
}

@Test
void solveImplementedTypeWithSameName() throws IOException {
StaticJavaParser.getConfiguration().setSymbolResolver(new JavaSymbolSolver(
new JavaParserTypeSolver(adaptPath("src/test/resources/ImplementedOrExtendedTypeResolution/pkg"))));

CompilationUnit cu = StaticJavaParser.parse(adaptPath("src/test/resources/ImplementedOrExtendedTypeResolution/pkg/main/A.java"));
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "A");
String actual = clazz.getFieldByName("field_a").get().resolve().getType().describe();
assertEquals("main.A", actual);
assertNotEquals("another.A", actual);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
interface I1 {
}

interface I2 {
interface I2_1 {
}
}

interface I3 {
interface I3_1 {
interface I3_1_1 {
}
}
}

class C1 {
}

class C2 {
class C2_1 {
}
}

class C3 {
class C3_1 {
class C3_1_1 {
}
}
}

class InterfaceTest implements I1, I2.I2_1, I3.I3_1.I3_1_1 {
I1 field_i1;
I2_1 field_i2;
I3_1_1 field_i3;
}

class ClassTest1 extends C1 {
C1 field_c1;
}

class ClassTest2 extends C2.C2_1 {
C2_1 field_c2;
}

class ClassTest3 extends C3.C3_1.C3_1_1 {
C3_1_1 field_c3;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package another;

interface A {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main;

class A implements another.A {
A field_a;
}

0 comments on commit f003e42

Please sign in to comment.