Search in sources :

Example 1 with ResolvedValueDeclaration

use of com.github.javaparser.resolution.declarations.ResolvedValueDeclaration in project structr by structr.

the class JavaParserModule method parseJavaFilesAndSolveTypes.

private void parseJavaFilesAndSolveTypes(final Folder folder) {
    if (ignoreTests && "test".equals(folder.getName())) {
        return;
    }
    for (final File file : folder.getFiles()) {
        if (file.getContentType().equals("text/x-java")) {
            final String javaFileName = file.getName();
            if (javaFileName.equals("package-info.java") || javaFileName.equals("testPackage-info.java")) {
            } else {
                final String javaContent = file.getFavoriteContent();
                ClassOrInterface clsOrIface = null;
                CompilationUnit cu = null;
                try {
                    cu = JavaParser.parse(javaContent);
                    for (final TypeDeclaration type : cu.findAll(TypeDeclaration.class)) {
                        SymbolReference<? extends ResolvedValueDeclaration> decl = facade.solve(type.getName());
                        if (type.isClassOrInterfaceDeclaration()) {
                            org.structr.javaparser.entity.Package pkg = null;
                            if (cu.getPackageDeclaration().isPresent()) {
                                pkg = handlePackage(cu.getPackageDeclaration().get());
                            }
                            clsOrIface = handleClassOrInterface(type, pkg);
                        }
                    }
                    for (final BodyDeclaration t : cu.findAll(BodyDeclaration.class)) {
                        if (t instanceof CallableDeclaration) {
                            final CallableDeclaration callable = t.asCallableDeclaration();
                            if (t instanceof ConstructorDeclaration) {
                            // final ConstructorDeclaration cd = t.asConstructorDeclaration();
                            // logger.info("Constructor found: " + cd.getNameAsString());
                            // 
                            // final SymbolReference<ResolvedReferenceTypeDeclaration> constructorRef = typeSolver.tryToSolveType(cd.getNameAsString());
                            // if (constructorRef.isSolved()) {
                            // 
                            // logger.info("Solved constructor: " + cd.getNameAsString());
                            // //final ResolvedReferenceTypeDeclaration decl = constructorRef.getCorrespondingDeclaration();
                            // }
                            } else if (t instanceof MethodDeclaration) {
                                final MethodDeclaration md = t.asMethodDeclaration();
                                final String methodName = md.getNameAsString();
                                logger.info("Method found: " + methodName);
                                // Create methods and link to class
                                final PropertyMap identifyingMethodProperties = new PropertyMap();
                                identifyingMethodProperties.put(Method.name, methodName);
                                final PropertyMap methodProperties = new PropertyMap();
                                methodProperties.putAll(identifyingMethodProperties);
                                methodProperties.put(Method.classOrInterface, clsOrIface);
                                methodProperties.put(Method.declaration, md.getDeclarationAsString());
                                final Optional<BlockStmt> block = md.getBody();
                                if (block.isPresent()) {
                                    methodProperties.put(Method.body, block.get().toString());
                                }
                                final String symbolName = StringUtils.substringAfterLast(clsOrIface.getName(), ".") + "." + md.getNameAsString();
                                try {
                                    final SymbolReference<? extends ResolvedValueDeclaration> methodRef = facade.solve(md.getName());
                                    if (methodRef.isSolved()) {
                                        final ResolvedValueDeclaration decl = methodRef.getCorrespondingDeclaration();
                                        if (decl.isMethod()) {
                                            final String mName = decl.asMethod().getName();
                                            final String signature = decl.asMethod().getSignature();
                                            logger.info("Solved method: " + methodRef.toString() + ", signature: " + signature);
                                            methodProperties.put(Method.resolved, true);
                                        }
                                    }
                                } catch (final UnsolvedSymbolException ignore) {
                                }
                                getOrCreate(Method.class, identifyingMethodProperties, methodProperties);
                                logger.info("Created (or found) method " + symbolName);
                            }
                        // final NodeList<Parameter> parameters = callable.getParameters();
                        // 
                        // List<JsonResult> parameterList = new ArrayList<>();
                        // 
                        // parameters.forEach((p) -> {
                        // 
                        // JsonResult param = new JsonResult();
                        // 
                        // param.addName(p);
                        // param.addType(p.getType());
                        // param.addModifiers(p);
                        // 
                        // parameterList.add(param);
                        // });
                        }
                    }
                } catch (Throwable ignore) {
                }
            }
        }
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) UnsolvedSymbolException(com.github.javaparser.symbolsolver.javaparsermodel.UnsolvedSymbolException) ClassOrInterface(org.structr.javaparser.entity.ClassOrInterface) Package(org.structr.javaparser.entity.Package) Optional(java.util.Optional) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) SymbolReference(com.github.javaparser.symbolsolver.model.resolution.SymbolReference) Method(org.structr.javaparser.entity.Method) PropertyMap(org.structr.core.property.PropertyMap) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) CallableDeclaration(com.github.javaparser.ast.body.CallableDeclaration) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) ResolvedValueDeclaration(com.github.javaparser.resolution.declarations.ResolvedValueDeclaration) File(org.structr.web.entity.File) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration)

Example 2 with ResolvedValueDeclaration

use of com.github.javaparser.resolution.declarations.ResolvedValueDeclaration in project javaparser by javaparser.

the class LambdaExprContext method solveSymbolAsValue.

@Override
public Optional<Value> solveSymbolAsValue(String name, TypeSolver typeSolver) {
    for (Parameter parameter : wrappedNode.getParameters()) {
        SymbolDeclarator sb = JavaParserFactory.getSymbolDeclarator(parameter, typeSolver);
        int index = 0;
        for (ResolvedValueDeclaration decl : sb.getSymbolDeclarations()) {
            if (decl.getName().equals(name)) {
                if (requireParentNode(wrappedNode) instanceof MethodCallExpr) {
                    MethodCallExpr methodCallExpr = (MethodCallExpr) requireParentNode(wrappedNode);
                    MethodUsage methodUsage = JavaParserFacade.get(typeSolver).solveMethodAsUsage(methodCallExpr);
                    int i = pos(methodCallExpr, wrappedNode);
                    ResolvedType lambdaType = methodUsage.getParamTypes().get(i);
                    // Get the functional method in order for us to resolve it's type arguments properly
                    Optional<MethodUsage> functionalMethodOpt = FunctionalInterfaceLogic.getFunctionalMethod(lambdaType);
                    if (functionalMethodOpt.isPresent()) {
                        MethodUsage functionalMethod = functionalMethodOpt.get();
                        InferenceContext inferenceContext = new InferenceContext(MyObjectProvider.INSTANCE);
                        // Resolve each type variable of the lambda, and use this later to infer the type of each
                        // implicit parameter
                        inferenceContext.addPair(lambdaType, new ReferenceTypeImpl(lambdaType.asReferenceType().getTypeDeclaration(), typeSolver));
                        // Find the position of this lambda argument
                        boolean found = false;
                        int lambdaParamIndex;
                        for (lambdaParamIndex = 0; lambdaParamIndex < wrappedNode.getParameters().size(); lambdaParamIndex++) {
                            if (wrappedNode.getParameter(lambdaParamIndex).getName().getIdentifier().equals(name)) {
                                found = true;
                                break;
                            }
                        }
                        if (!found) {
                            return Optional.empty();
                        }
                        // Now resolve the argument type using the inference context
                        ResolvedType argType = inferenceContext.resolve(inferenceContext.addSingle(functionalMethod.getParamType(lambdaParamIndex)));
                        ResolvedLambdaConstraintType conType;
                        if (argType.isWildcard()) {
                            conType = ResolvedLambdaConstraintType.bound(argType.asWildcard().getBoundedType());
                        } else {
                            conType = ResolvedLambdaConstraintType.bound(argType);
                        }
                        Value value = new Value(conType, name);
                        return Optional.of(value);
                    } else {
                        return Optional.empty();
                    }
                } else if (requireParentNode(wrappedNode) instanceof VariableDeclarator) {
                    VariableDeclarator variableDeclarator = (VariableDeclarator) requireParentNode(wrappedNode);
                    ResolvedType t = JavaParserFacade.get(typeSolver).convertToUsageVariableType(variableDeclarator);
                    Optional<MethodUsage> functionalMethod = FunctionalInterfaceLogic.getFunctionalMethod(t);
                    if (functionalMethod.isPresent()) {
                        ResolvedType lambdaType = functionalMethod.get().getParamType(index);
                        // Replace parameter from declarator
                        Map<ResolvedTypeParameterDeclaration, ResolvedType> inferredTypes = new HashMap<>();
                        if (lambdaType.isReferenceType()) {
                            for (com.github.javaparser.utils.Pair<ResolvedTypeParameterDeclaration, ResolvedType> entry : lambdaType.asReferenceType().getTypeParametersMap()) {
                                if (entry.b.isTypeVariable() && entry.b.asTypeParameter().declaredOnType()) {
                                    ResolvedType ot = t.asReferenceType().typeParametersMap().getValue(entry.a);
                                    lambdaType = lambdaType.replaceTypeVariables(entry.a, ot, inferredTypes);
                                }
                            }
                        } else if (lambdaType.isTypeVariable() && lambdaType.asTypeParameter().declaredOnType()) {
                            lambdaType = t.asReferenceType().typeParametersMap().getValue(lambdaType.asTypeParameter());
                        }
                        Value value = new Value(lambdaType, name);
                        return Optional.of(value);
                    } else {
                        throw new UnsupportedOperationException();
                    }
                } else {
                    throw new UnsupportedOperationException();
                }
            }
            index++;
        }
    }
    // if nothing is found we should ask the parent context
    return getParent().solveSymbolAsValue(name, typeSolver);
}
Also used : SymbolDeclarator(com.github.javaparser.symbolsolver.resolution.SymbolDeclarator) InferenceContext(com.github.javaparser.symbolsolver.logic.InferenceContext) ResolvedLambdaConstraintType(com.github.javaparser.resolution.types.ResolvedLambdaConstraintType) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) Value(com.github.javaparser.symbolsolver.model.resolution.Value) Parameter(com.github.javaparser.ast.body.Parameter) MethodUsage(com.github.javaparser.resolution.MethodUsage) ResolvedValueDeclaration(com.github.javaparser.resolution.declarations.ResolvedValueDeclaration) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 3 with ResolvedValueDeclaration

use of com.github.javaparser.resolution.declarations.ResolvedValueDeclaration in project javaparser by javaparser.

the class LambdaExprContext method solveSymbol.

@Override
public SymbolReference<? extends ResolvedValueDeclaration> solveSymbol(String name, TypeSolver typeSolver) {
    for (Parameter parameter : wrappedNode.getParameters()) {
        SymbolDeclarator sb = JavaParserFactory.getSymbolDeclarator(parameter, typeSolver);
        SymbolReference<ResolvedValueDeclaration> symbolReference = solveWith(sb, name);
        if (symbolReference.isSolved()) {
            return symbolReference;
        }
    }
    // if nothing is found we should ask the parent context
    return getParent().solveSymbol(name, typeSolver);
}
Also used : SymbolDeclarator(com.github.javaparser.symbolsolver.resolution.SymbolDeclarator) Parameter(com.github.javaparser.ast.body.Parameter) ResolvedValueDeclaration(com.github.javaparser.resolution.declarations.ResolvedValueDeclaration)

Aggregations

ResolvedValueDeclaration (com.github.javaparser.resolution.declarations.ResolvedValueDeclaration)3 Parameter (com.github.javaparser.ast.body.Parameter)2 SymbolDeclarator (com.github.javaparser.symbolsolver.resolution.SymbolDeclarator)2 CompilationUnit (com.github.javaparser.ast.CompilationUnit)1 BodyDeclaration (com.github.javaparser.ast.body.BodyDeclaration)1 CallableDeclaration (com.github.javaparser.ast.body.CallableDeclaration)1 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)1 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)1 TypeDeclaration (com.github.javaparser.ast.body.TypeDeclaration)1 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)1 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)1 MethodUsage (com.github.javaparser.resolution.MethodUsage)1 ResolvedLambdaConstraintType (com.github.javaparser.resolution.types.ResolvedLambdaConstraintType)1 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)1 UnsolvedSymbolException (com.github.javaparser.symbolsolver.javaparsermodel.UnsolvedSymbolException)1 InferenceContext (com.github.javaparser.symbolsolver.logic.InferenceContext)1 SymbolReference (com.github.javaparser.symbolsolver.model.resolution.SymbolReference)1 Value (com.github.javaparser.symbolsolver.model.resolution.Value)1 ReferenceTypeImpl (com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl)1 Optional (java.util.Optional)1