Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue 322: Implement symbol solving in JavassistEnumDeclaration, anal…
…ogous to how it works in JavassistClassDeclaration, and add tests for that.
- Loading branch information
1 parent
849a30b
commit 19b626c
Showing
9 changed files
with
164 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...java/com/github/javaparser/symbolsolver/resolution/SymbolSolverWithJavassistEnumTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,108 @@ | |||
package com.github.javaparser.symbolsolver.resolution; | |||
|
|||
import com.github.javaparser.resolution.declarations.ResolvedValueDeclaration; | |||
import com.github.javaparser.symbolsolver.AbstractTest; | |||
import com.github.javaparser.symbolsolver.javassistmodel.JavassistEnumDeclaration; | |||
import com.github.javaparser.symbolsolver.model.resolution.SymbolReference; | |||
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver; | |||
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver; | |||
import com.github.javaparser.symbolsolver.resolution.typesolvers.JarTypeSolver; | |||
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver; | |||
import javassist.NotFoundException; | |||
import org.junit.Before; | |||
import org.junit.Ignore; | |||
import org.junit.Test; | |||
|
|||
import java.io.IOException; | |||
|
|||
import static org.junit.Assert.*; | |||
import static org.junit.Assert.fail; | |||
|
|||
public class SymbolSolverWithJavassistEnumTest extends AbstractTest { | |||
private TypeSolver typeSolver; | |||
private SymbolSolver symbolSolver; | |||
private JavassistEnumDeclaration enumDeclarationConcrete; | |||
private JavassistEnumDeclaration enumDeclarationInterfaceUserOwnJar; | |||
private JavassistEnumDeclaration enumDeclarationInterfaceUserIncludedJar; | |||
private JavassistEnumDeclaration enumDeclarationInterfaceUserExcludedJar; | |||
|
|||
@Before | |||
public void setup() throws IOException { | |||
final String pathToMainJar = adaptPath("src/test/resources/javassist_symbols/main_jar/main_jar.jar"); | |||
final String pathToIncludedJar = adaptPath("src/test/resources/javassist_symbols/included_jar/included_jar.jar"); | |||
typeSolver = new CombinedTypeSolver(new JarTypeSolver(pathToIncludedJar), new JarTypeSolver(pathToMainJar), new ReflectionTypeSolver()); | |||
|
|||
symbolSolver = new SymbolSolver(typeSolver); | |||
|
|||
enumDeclarationConcrete = (JavassistEnumDeclaration) typeSolver.solveType("com.github.javaparser.javasymbolsolver.javassist_symbols.main_jar.ConcreteEnum"); | |||
enumDeclarationInterfaceUserOwnJar = (JavassistEnumDeclaration) typeSolver.solveType("com.github.javaparser.javasymbolsolver.javassist_symbols.main_jar.EnumInterfaceUserOwnJar"); | |||
enumDeclarationInterfaceUserIncludedJar = (JavassistEnumDeclaration) typeSolver.solveType("com.github.javaparser.javasymbolsolver.javassist_symbols.main_jar.EnumInterfaceUserIncludedJar"); | |||
enumDeclarationInterfaceUserExcludedJar = (JavassistEnumDeclaration) typeSolver.solveType("com.github.javaparser.javasymbolsolver.javassist_symbols.main_jar.EnumInterfaceUserExcludedJar"); | |||
} | |||
|
|||
@Test | |||
public void testSolveSymbolInTypeCanResolveFirstEnumValue() { | |||
assertCanSolveSymbol("ENUM_VAL_ONE", enumDeclarationConcrete); | |||
} | |||
|
|||
@Test | |||
public void testSolveSymbolInTypeCanResolveSecondEnumValue() { | |||
assertCanSolveSymbol("ENUM_VAL_TWO", enumDeclarationConcrete); | |||
} | |||
|
|||
@Test | |||
public void testSolveSymbolInTypeCanResolveFirstNormalField() { | |||
assertCanSolveSymbol("STATIC_STRING", enumDeclarationConcrete); | |||
} | |||
|
|||
@Test | |||
public void testSolveSymbolInTypeCanResolveSecondNormalField() { | |||
assertCanSolveSymbol("SECOND_STRING", enumDeclarationConcrete); | |||
} | |||
|
|||
@Test | |||
public void testSolveSymbolInTypeCantResolveNonExistentField() { | |||
SymbolReference<? extends ResolvedValueDeclaration> solvedSymbol = symbolSolver.solveSymbolInType(enumDeclarationConcrete, "FIELD_THAT_DOES_NOT_EXIST"); | |||
|
|||
assertFalse(solvedSymbol.isSolved()); | |||
|
|||
try { | |||
solvedSymbol.getCorrespondingDeclaration(); | |||
} catch (Exception e) { | |||
assertTrue(e instanceof UnsupportedOperationException); | |||
assertNull(e.getMessage()); | |||
return; | |||
} | |||
fail("Expected UnsupportedOperationException when requesting CorrespondingDeclaration on unsolved SymbolRefernce"); | |||
} | |||
|
|||
@Test | |||
public void testSolveSymbolInTypeCanResolveFieldInInterface() { | |||
assertCanSolveSymbol("INTERFACE_FIELD", enumDeclarationInterfaceUserOwnJar); | |||
} | |||
|
|||
@Test | |||
@Ignore // TODO This fails at the moment, I think it might be an issue -- discussion ongoing on Gitter | |||
public void testSolveSymbolInTypeCanResolveFieldInInterfaceIncludedJar() { | |||
assertCanSolveSymbol("INTERFACE_FIELD", enumDeclarationInterfaceUserIncludedJar); | |||
} | |||
|
|||
@Test | |||
public void testSolveSymbolInTypeThrowsExceptionOnResolveFieldInInterfaceExcludedJar() { | |||
try { | |||
symbolSolver.solveSymbolInType(enumDeclarationInterfaceUserExcludedJar, "INTERFACE_FIELD"); | |||
} catch (Exception e) { | |||
assertTrue(e.getCause() instanceof NotFoundException); | |||
assertEquals("com.github.javaparser.javasymbolsolver.javassist_symbols.excluded_jar.InterfaceExcludedJar", e.getCause().getMessage()); | |||
return; | |||
} | |||
fail("Excepted NotFoundException wrapped in a RuntimeException, but got no exception."); | |||
} | |||
|
|||
private void assertCanSolveSymbol(String symbolName, JavassistEnumDeclaration enumDeclaration) { | |||
SymbolReference<? extends ResolvedValueDeclaration> solvedSymbol = symbolSolver.solveSymbolInType(enumDeclaration, symbolName); | |||
|
|||
assertTrue(solvedSymbol.isSolved()); | |||
assertEquals(symbolName, solvedSymbol.getCorrespondingDeclaration().asField().getName()); | |||
} | |||
} |
Binary file modified
BIN
+3.22 KB
(150%)
java-symbol-solver-testing/src/test/resources/javassist_symbols/main_jar/main_jar.jar
Binary file not shown.
10 changes: 10 additions & 0 deletions
10
...r/src/com/github/javaparser/javasymbolsolver/javassist_symbols/main_jar/ConcreteEnum.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,10 @@ | |||
package com.github.javaparser.javasymbolsolver.javassist_symbols.main_jar; | |||
|
|||
public enum ConcreteEnum { | |||
ENUM_VAL_ONE, | |||
ENUM_VAL_TWO; | |||
|
|||
public static final String STATIC_STRING = "ThisIsAString"; | |||
public static final String SECOND_STRING = "ThisIsASecondString"; | |||
|
|||
} |
7 changes: 7 additions & 0 deletions
7
.../javaparser/javasymbolsolver/javassist_symbols/main_jar/EnumInterfaceUserExcludedJar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,7 @@ | |||
package com.github.javaparser.javasymbolsolver.javassist_symbols.main_jar; | |||
|
|||
import com.github.javaparser.javasymbolsolver.javassist_symbols.excluded_jar.InterfaceExcludedJar; | |||
|
|||
public enum EnumInterfaceUserExcludedJar implements InterfaceExcludedJar { | |||
OWN_ENUM_VAL | |||
} |
7 changes: 7 additions & 0 deletions
7
.../javaparser/javasymbolsolver/javassist_symbols/main_jar/EnumInterfaceUserIncludedJar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,7 @@ | |||
package com.github.javaparser.javasymbolsolver.javassist_symbols.main_jar; | |||
|
|||
import com.github.javaparser.javasymbolsolver.javassist_symbols.included_jar.InterfaceIncludedJar; | |||
|
|||
public enum EnumInterfaceUserIncludedJar implements InterfaceIncludedJar { | |||
OWN_ENUM_VAL | |||
} |
5 changes: 5 additions & 0 deletions
5
...ithub/javaparser/javasymbolsolver/javassist_symbols/main_jar/EnumInterfaceUserOwnJar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,5 @@ | |||
package com.github.javaparser.javasymbolsolver.javassist_symbols.main_jar; | |||
|
|||
public enum EnumInterfaceUserOwnJar implements InterfaceOwnJar { | |||
OWN_ENUM_VAL | |||
} |