Search in sources :

Example 31 with MemoryTypeSolver

use of com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver in project javaparser by javaparser.

the class EnumDeclarationContextResolutionTest method solveSymbolReferringToDeclaredInstanceField.

@Test
public void solveSymbolReferringToDeclaredInstanceField() {
    CompilationUnit cu = parseSample("AnEnum");
    com.github.javaparser.ast.body.EnumDeclaration enumDeclaration = Navigator.demandEnum(cu, "MyEnum");
    Context context = new EnumDeclarationContext(enumDeclaration, typeSolver);
    SymbolReference<? extends ResolvedValueDeclaration> ref = context.solveSymbol("i", new MemoryTypeSolver());
    assertEquals(true, ref.isSolved());
    assertEquals("int", ref.getCorrespondingDeclaration().getType().describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) EnumDeclarationContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.EnumDeclarationContext) Context(com.github.javaparser.symbolsolver.core.resolution.Context) MemoryTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver) EnumDeclarationContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.EnumDeclarationContext) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 32 with MemoryTypeSolver

use of com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver in project javaparser by javaparser.

the class EnumDeclarationContextResolutionTest method solveSymbolReferringToDeclaredStaticField.

@Test
public void solveSymbolReferringToDeclaredStaticField() {
    CompilationUnit cu = parseSample("AnEnum");
    com.github.javaparser.ast.body.EnumDeclaration enumDeclaration = Navigator.demandEnum(cu, "MyEnum");
    Context context = new EnumDeclarationContext(enumDeclaration, typeSolver);
    SymbolReference<? extends ResolvedValueDeclaration> ref = context.solveSymbol("j", new MemoryTypeSolver());
    assertEquals(true, ref.isSolved());
    assertEquals("long", ref.getCorrespondingDeclaration().getType().describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) EnumDeclarationContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.EnumDeclarationContext) Context(com.github.javaparser.symbolsolver.core.resolution.Context) MemoryTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver) EnumDeclarationContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.EnumDeclarationContext) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 33 with MemoryTypeSolver

use of com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver in project javaparser by javaparser.

the class EnumDeclarationContextResolutionTest method solveSymbolAsValueReferringToDeclaredStaticField.

@Test
public void solveSymbolAsValueReferringToDeclaredStaticField() {
    CompilationUnit cu = parseSample("AnEnum");
    com.github.javaparser.ast.body.EnumDeclaration enumDeclaration = Navigator.demandEnum(cu, "MyEnum");
    Context context = new EnumDeclarationContext(enumDeclaration, typeSolver);
    Optional<Value> ref = context.solveSymbolAsValue("j", new MemoryTypeSolver());
    assertEquals(true, ref.isPresent());
    assertEquals("long", ref.get().getType().describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) EnumDeclarationContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.EnumDeclarationContext) Context(com.github.javaparser.symbolsolver.core.resolution.Context) MemoryTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver) EnumDeclarationContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.EnumDeclarationContext) Value(com.github.javaparser.symbolsolver.model.resolution.Value) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 34 with MemoryTypeSolver

use of com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver in project javaparser by javaparser.

the class EnumDeclarationContextResolutionTest method solveSymbolReferringToValue.

@Test
public void solveSymbolReferringToValue() {
    CompilationUnit cu = parseSample("AnEnum");
    com.github.javaparser.ast.body.EnumDeclaration enumDeclaration = Navigator.demandEnum(cu, "MyEnum");
    Context context = new EnumDeclarationContext(enumDeclaration, typeSolver);
    SymbolReference<? extends ResolvedValueDeclaration> ref = context.solveSymbol("E1", new MemoryTypeSolver());
    assertEquals(true, ref.isSolved());
    assertEquals("MyEnum", ref.getCorrespondingDeclaration().getType().describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) EnumDeclarationContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.EnumDeclarationContext) Context(com.github.javaparser.symbolsolver.core.resolution.Context) MemoryTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver) EnumDeclarationContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.EnumDeclarationContext) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 35 with MemoryTypeSolver

use of com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver in project javaparser by javaparser.

the class DefaultPackageTest method aClassInDefaultPackageCanBeAccessedFromOutsideTheDefaultPackageWithoutImportingIt.

@Test(expected = UnsolvedSymbolException.class)
public void aClassInDefaultPackageCanBeAccessedFromOutsideTheDefaultPackageWithoutImportingIt() {
    String code = "package myPackage; class A extends B {}";
    MemoryTypeSolver memoryTypeSolver = new MemoryTypeSolver();
    memoryTypeSolver.addDeclaration("B", new MyClassDeclaration("B"));
    ResolvedType resolvedType = JavaParserFacade.get(memoryTypeSolver).convertToUsage(JavaParser.parse(code).getClassByName("A").get().getExtendedTypes(0));
    assertEquals("B", resolvedType.asReferenceType().getQualifiedName());
}
Also used : MemoryTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) Test(org.junit.Test)

Aggregations

MemoryTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver)36 Test (org.junit.Test)36 CompilationUnit (com.github.javaparser.ast.CompilationUnit)33 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)33 Context (com.github.javaparser.symbolsolver.core.resolution.Context)32 CompilationUnitContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext)25 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)24 ClassOrInterfaceDeclarationContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.ClassOrInterfaceDeclarationContext)23 ResolvedTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration)11 Value (com.github.javaparser.symbolsolver.model.resolution.Value)9 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)7 EnumDeclarationContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.EnumDeclarationContext)6 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)2 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)1 ResolvedReferenceTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration)1 MethodContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.MethodContext)1