Search in sources :

Example 21 with CombinedTypeSolver

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

the class JavaParserModule method addJarsToIndex.

/**
 * Add compilation units of all jar files found in the given folder to the index.
 *
 * @param folderPath
 */
public void addJarsToIndex(final String folderPath) {
    logger.info("Starting adding jar files in " + folderPath);
    final CombinedTypeSolver typeSolver = new CombinedTypeSolver();
    typeSolver.add(new ReflectionTypeSolver());
    final AtomicLong count = new AtomicLong(0);
    try {
        Files.newDirectoryStream(Paths.get(folderPath), path -> path.toString().endsWith(".jar")).forEach((file) -> {
            try {
                typeSolver.add(new JarTypeSolver(new FileInputStream(file.toFile())));
                count.addAndGet(1L);
            } catch (IOException ex) {
            }
        });
    } catch (IOException ex) {
    }
    logger.info("Added " + count.toString() + " jar files to the type solver");
    typeSolver.add(structrTypeSolver);
    facade = JavaParserFacade.get(typeSolver);
    logger.info("Done with adding jar files in " + folderPath);
}
Also used : JsonObject(com.google.gson.JsonObject) Package(org.structr.javaparser.entity.Package) XPathExpressionException(javax.xml.xpath.XPathExpressionException) UnsolvedSymbolException(com.github.javaparser.symbolsolver.javaparsermodel.UnsolvedSymbolException) LoggerFactory(org.slf4j.LoggerFactory) SecurityContext(org.structr.common.SecurityContext) StringUtils(org.apache.commons.lang3.StringUtils) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) Actions(org.structr.schema.action.Actions) FrameworkException(org.structr.common.error.FrameworkException) App(org.structr.core.app.App) NodeWithSimpleName(com.github.javaparser.ast.nodeTypes.NodeWithSimpleName) Type(com.github.javaparser.ast.type.Type) Document(org.w3c.dom.Document) Map(java.util.Map) CompilationUnit(com.github.javaparser.ast.CompilationUnit) JavaInterface(org.structr.javaparser.entity.JavaInterface) NodeList(com.github.javaparser.ast.NodeList) Folder(org.structr.web.entity.Folder) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) Set(java.util.Set) GraphObject(org.structr.core.GraphObject) ResolvedValueDeclaration(com.github.javaparser.resolution.declarations.ResolvedValueDeclaration) Functions(org.structr.core.function.Functions) JsonArray(com.google.gson.JsonArray) List(java.util.List) StructrModule(org.structr.module.StructrModule) JarTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.JarTypeSolver) SAXException(org.xml.sax.SAXException) LicenseManager(org.structr.api.service.LicenseManager) Optional(java.util.Optional) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) QName(javax.xml.namespace.QName) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NodeWithOptionalBlockStmt(com.github.javaparser.ast.nodeTypes.NodeWithOptionalBlockStmt) StructrApp(org.structr.core.app.StructrApp) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) Method(org.structr.javaparser.entity.Method) PackageDeclaration(com.github.javaparser.ast.PackageDeclaration) XPath(javax.xml.xpath.XPath) XPathConstants(javax.xml.xpath.XPathConstants) Parameter(com.github.javaparser.ast.body.Parameter) HashMap(java.util.HashMap) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) ArrayList(java.util.ArrayList) PropertyMap(org.structr.core.property.PropertyMap) File(org.structr.web.entity.File) JavaClass(org.structr.javaparser.entity.JavaClass) JsonPrimitive(com.google.gson.JsonPrimitive) ClassOrInterface(org.structr.javaparser.entity.ClassOrInterface) LinkedHashSet(java.util.LinkedHashSet) InputSource(org.xml.sax.InputSource) Logger(org.slf4j.Logger) NodeWithModifiers(com.github.javaparser.ast.nodeTypes.NodeWithModifiers) Files(java.nio.file.Files) AbstractSchemaNode(org.structr.core.entity.AbstractSchemaNode) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) CallableDeclaration(com.github.javaparser.ast.body.CallableDeclaration) AddJarsToIndexFunction(org.structr.javaparser.entity.AddJarsToIndexFunction) AtomicLong(java.util.concurrent.atomic.AtomicLong) XPathFactory(javax.xml.xpath.XPathFactory) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) StringReader(java.io.StringReader) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) SymbolReference(com.github.javaparser.symbolsolver.model.resolution.SymbolReference) Paths(java.nio.file.Paths) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) Module(org.structr.javaparser.entity.Module) JavaParser(com.github.javaparser.JavaParser) InputStream(java.io.InputStream) AtomicLong(java.util.concurrent.atomic.AtomicLong) JarTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.JarTypeSolver) IOException(java.io.IOException) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) FileInputStream(java.io.FileInputStream)

Example 22 with CombinedTypeSolver

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

the class Issue144 method issue144WithCombinedTypeSolver.

@Test
public void issue144WithCombinedTypeSolver() {
    CompilationUnit cu = parseSampleWithStandardExtension("issue144/HelloWorld");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "HelloWorld");
    ExpressionStmt expressionStmt = (ExpressionStmt) clazz.getMethodsByName("main").get(0).getBody().get().getStatement(0);
    MethodCallExpr methodCallExpr = (MethodCallExpr) expressionStmt.getExpression();
    Expression firstParameter = methodCallExpr.getArgument(0);
    JavaParserFacade javaParserFacade = JavaParserFacade.get(new CombinedTypeSolver(typeSolver, new ReflectionTypeSolver(true)));
    assertEquals(true, javaParserFacade.solve(firstParameter).isSolved());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Expression(com.github.javaparser.ast.expr.Expression) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 23 with CombinedTypeSolver

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

the class SymbolSolverTest method setup.

@Before
public void setup() {
    File srcNewCode = adaptPath(new File("src/test/test_sourcecode/javaparser_new_src/javaparser-core"));
    CombinedTypeSolver combinedTypeSolverNewCode = new CombinedTypeSolver();
    combinedTypeSolverNewCode.add(new ReflectionTypeSolver());
    combinedTypeSolverNewCode.add(new JavaParserTypeSolver(srcNewCode));
    combinedTypeSolverNewCode.add(new JavaParserTypeSolver(adaptPath(new File("src/test/test_sourcecode/javaparser_new_src/javaparser-generated-sources"))));
    typeSolverNewCode = combinedTypeSolverNewCode;
    symbolSolver = new SymbolSolver(typeSolverNewCode);
}
Also used : JavaParserTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) File(java.io.File) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) Before(org.junit.Before)

Example 24 with CombinedTypeSolver

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

the class SymbolSolverWithJavassistEnumTest method setup.

@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");
}
Also used : JarTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.JarTypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) Before(org.junit.Before)

Example 25 with CombinedTypeSolver

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

the class VariadicResolutionTest method selectMostSpecificVariadic.

@Test
public void selectMostSpecificVariadic() {
    CompilationUnit cu = parseSample("MethodCalls");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "MethodCalls");
    MethodDeclaration method = Navigator.demandMethod(clazz, "variadicTest");
    List<MethodCallExpr> calls = method.findAll(MethodCallExpr.class);
    File src = adaptPath(new File("src/test/resources"));
    TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(), new JavaParserTypeSolver(src));
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    MethodUsage call1 = javaParserFacade.solveMethodAsUsage(calls.get(0));
    MethodUsage call2 = javaParserFacade.solveMethodAsUsage(calls.get(1));
    assertEquals("int", call1.returnType().describe());
    assertEquals("void", call2.returnType().describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) JavaParserTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) JavaParserTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) MethodUsage(com.github.javaparser.resolution.MethodUsage) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) File(java.io.File) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Test(org.junit.Test)

Aggregations

CombinedTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver)49 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)46 CompilationUnit (com.github.javaparser.ast.CompilationUnit)23 JavaParserTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver)22 Test (org.junit.Test)21 File (java.io.File)20 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)19 JarTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.JarTypeSolver)15 Before (org.junit.Before)15 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)12 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)11 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)9 MethodUsage (com.github.javaparser.resolution.MethodUsage)9 Context (com.github.javaparser.symbolsolver.core.resolution.Context)9 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)6 CompilationUnitContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext)6 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)6 Value (com.github.javaparser.symbolsolver.model.resolution.Value)4 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)3 SourceFileInfoExtractor (com.github.javaparser.symbolsolver.SourceFileInfoExtractor)3