Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ee35b15
commit 6a15cd5
Showing
14 changed files
with
142 additions
and
7 deletions.
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
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
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
34 changes: 34 additions & 0 deletions
34
src/main/java/me/tomassetti/symbolsolver/model/javaparser/contexts/SwitchContext.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,34 @@ | |||
package me.tomassetti.symbolsolver.model.javaparser.contexts; | |||
|
|||
import com.github.javaparser.ast.stmt.SwitchStmt; | |||
import me.tomassetti.symbolsolver.JavaParserFacade; | |||
import me.tomassetti.symbolsolver.model.SymbolReference; | |||
import me.tomassetti.symbolsolver.model.TypeSolver; | |||
import me.tomassetti.symbolsolver.model.declarations.EnumDeclaration; | |||
import me.tomassetti.symbolsolver.model.declarations.ValueDeclaration; | |||
import me.tomassetti.symbolsolver.model.usages.TypeUsage; | |||
import me.tomassetti.symbolsolver.model.usages.TypeUsageOfTypeDeclaration; | |||
|
|||
/** | |||
* Created by federico on 24/08/15. | |||
*/ | |||
public class SwitchContext extends StatementContext<SwitchStmt> { | |||
|
|||
public SwitchContext(SwitchStmt wrappedNode) { | |||
super(wrappedNode); | |||
} | |||
|
|||
@Override | |||
public SymbolReference<? extends ValueDeclaration> solveSymbol(String name, TypeSolver typeSolver) { | |||
TypeUsage typeUsage = JavaParserFacade.get(typeSolver).getType(wrappedNode.getSelector()); | |||
if (typeUsage.isEnum()) { | |||
TypeUsageOfTypeDeclaration typeUsageOfTypeDeclaration = (TypeUsageOfTypeDeclaration)typeUsage; | |||
EnumDeclaration enumDeclaration = (EnumDeclaration)typeUsageOfTypeDeclaration.getTypeDeclaration(); | |||
if (enumDeclaration.hasField(name, typeSolver)) { | |||
return SymbolReference.solved(enumDeclaration.getField(name, typeSolver)); | |||
} | |||
} | |||
return super.solveSymbol(name, typeSolver); | |||
} | |||
|
|||
} |
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
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
37 changes: 37 additions & 0 deletions
37
src/test/java/me/tomassetti/symbolsolver/model/EnumTest.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,37 @@ | |||
package me.tomassetti.symbolsolver.model; | |||
|
|||
import com.github.javaparser.ParseException; | |||
import com.github.javaparser.ast.CompilationUnit; | |||
import com.github.javaparser.ast.body.MethodDeclaration; | |||
import com.github.javaparser.ast.expr.Expression; | |||
import com.github.javaparser.ast.expr.MethodCallExpr; | |||
import com.github.javaparser.ast.expr.NameExpr; | |||
import com.github.javaparser.ast.stmt.SwitchStmt; | |||
import me.tomassetti.symbolsolver.JavaParserFacade; | |||
import me.tomassetti.symbolsolver.javaparser.Navigator; | |||
import me.tomassetti.symbolsolver.model.declarations.ValueDeclaration; | |||
import me.tomassetti.symbolsolver.model.typesolvers.JreTypeSolver; | |||
import me.tomassetti.symbolsolver.model.usages.MethodUsage; | |||
import org.junit.Test; | |||
|
|||
import static org.junit.Assert.assertEquals; | |||
import static org.junit.Assert.assertTrue; | |||
|
|||
public class EnumTest extends AbstractTest { | |||
|
|||
@Test | |||
public void switchOnEnum() throws ParseException { | |||
CompilationUnit cu = parseSample("SwitchOnEnum"); | |||
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "SwitchOnEnum"); | |||
MethodDeclaration method = Navigator.demandMethod(clazz, "foo"); | |||
SwitchStmt switchStmt = Navigator.findSwitch(method); | |||
Expression expression = switchStmt.getEntries().get(0).getLabel(); | |||
|
|||
SymbolReference<? extends ValueDeclaration> ref = JavaParserFacade.get(new JreTypeSolver()).solve(expression); | |||
assertTrue(ref.isSolved()); | |||
assertEquals("SwitchOnEnum.MyEnum", ref.getCorrespondingDeclaration().getType(new JreTypeSolver()).getQualifiedName()); | |||
} | |||
|
|||
|
|||
|
|||
} |
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,15 @@ | |||
class SwitchOnEnum { | |||
|
|||
enum MyEnum { | |||
E1, E2, E3 | |||
} | |||
|
|||
public void foo() { | |||
MyEnum e; | |||
switch (e){ | |||
case E1: | |||
break; | |||
} | |||
} | |||
|
|||
} |