Search in sources :

Example 1 with JavaLanguageFrontend

use of de.fraunhofer.aisec.cpg.frontends.java.JavaLanguageFrontend in project cpg by Fraunhofer-AISEC.

the class VariableUsageResolver method resolveFieldUsages.

protected void resolveFieldUsages(Node current, RecordDeclaration curClass) {
    if (current instanceof MemberExpression) {
        MemberExpression memberExpression = (MemberExpression) current;
        Declaration baseTarget = null;
        if (memberExpression.getBase() instanceof DeclaredReferenceExpression) {
            DeclaredReferenceExpression base = (DeclaredReferenceExpression) memberExpression.getBase();
            if (lang instanceof JavaLanguageFrontend && base.getName().equals("super")) {
                if (curClass != null && !curClass.getSuperClasses().isEmpty()) {
                    var superType = curClass.getSuperClasses().get(0);
                    var superRecord = recordMap.get(superType);
                    if (superRecord == null) {
                        log.error("Could not find referring super type {} for {} in the record map. Will set the super type to java.lang.Object", superType.getTypeName(), curClass.getName());
                        base.setType(TypeParser.createFrom(Object.class.getName(), true));
                    } else {
                        baseTarget = superRecord.getThis();
                        base.setRefersTo(baseTarget);
                    }
                } else {
                    // no explicit super type -> java.lang.Object
                    Type objectType = TypeParser.createFrom(Object.class.getName(), true);
                    base.setType(objectType);
                }
            } else {
                baseTarget = resolveBase((DeclaredReferenceExpression) memberExpression.getBase());
                base.setRefersTo(baseTarget);
            }
            if (baseTarget instanceof EnumDeclaration) {
                String name = memberExpression.getName();
                Optional<EnumConstantDeclaration> memberTarget = ((EnumDeclaration) baseTarget).getEntries().stream().filter(e -> e.getName().equals(name)).findFirst();
                if (memberTarget.isPresent()) {
                    memberExpression.setRefersTo(memberTarget.get());
                    return;
                }
            } else if (baseTarget instanceof RecordDeclaration) {
                Type baseType = TypeParser.createFrom(baseTarget.getName(), true);
                if (!recordMap.containsKey(baseType)) {
                    final Type containingT = baseType;
                    Optional<Type> fqnResolvedType = recordMap.keySet().stream().filter(t -> t.getName().endsWith("." + containingT.getName())).findFirst();
                    if (fqnResolvedType.isPresent()) {
                        baseType = fqnResolvedType.get();
                    }
                }
                memberExpression.setRefersTo(resolveMember(baseType, memberExpression));
                return;
            }
        }
        Type baseType = memberExpression.getBase().getType();
        if (!recordMap.containsKey(baseType)) {
            final Type containingT = baseType;
            Optional<Type> fqnResolvedType = recordMap.keySet().stream().filter(t -> t.getName().endsWith("." + containingT.getName())).findFirst();
            if (fqnResolvedType.isPresent()) {
                baseType = fqnResolvedType.get();
            }
        }
        memberExpression.setRefersTo(resolveMember(baseType, memberExpression));
    }
}
Also used : Util.warnWithFileLocation(de.fraunhofer.aisec.cpg.helpers.Util.warnWithFileLocation) java.util(java.util) DeclaredReferenceExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.DeclaredReferenceExpression) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) de.fraunhofer.aisec.cpg.graph(de.fraunhofer.aisec.cpg.graph) ScopedWalker(de.fraunhofer.aisec.cpg.helpers.SubgraphWalker.ScopedWalker) NodeBuilder.newRecordDeclaration(de.fraunhofer.aisec.cpg.graph.NodeBuilder.newRecordDeclaration) Collectors(java.util.stream.Collectors) TranslationResult(de.fraunhofer.aisec.cpg.TranslationResult) MemberCallExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberCallExpression) Matcher(java.util.regex.Matcher) de.fraunhofer.aisec.cpg.graph.declarations(de.fraunhofer.aisec.cpg.graph.declarations) MemberExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberExpression) Util(de.fraunhofer.aisec.cpg.helpers.Util) de.fraunhofer.aisec.cpg.graph.types(de.fraunhofer.aisec.cpg.graph.types) Pattern(java.util.regex.Pattern) JavaLanguageFrontend(de.fraunhofer.aisec.cpg.frontends.java.JavaLanguageFrontend) Nullable(org.checkerframework.checker.nullness.qual.Nullable) MemberExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberExpression) NodeBuilder.newRecordDeclaration(de.fraunhofer.aisec.cpg.graph.NodeBuilder.newRecordDeclaration) DeclaredReferenceExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.DeclaredReferenceExpression) NodeBuilder.newRecordDeclaration(de.fraunhofer.aisec.cpg.graph.NodeBuilder.newRecordDeclaration) JavaLanguageFrontend(de.fraunhofer.aisec.cpg.frontends.java.JavaLanguageFrontend)

Example 2 with JavaLanguageFrontend

use of de.fraunhofer.aisec.cpg.frontends.java.JavaLanguageFrontend in project cpg by Fraunhofer-AISEC.

the class JavaExternalTypeHierarchyResolver method accept.

@Override
public void accept(TranslationResult translationResult) {
    // Run only for Java.
    if (this.lang instanceof JavaLanguageFrontend) {
        TypeSolver resolver = ((JavaLanguageFrontend) this.lang).getNativeTypeResolver();
        TypeManager tm = TypeManager.getInstance();
        // Iterate over all known types and add their (direct) supertypes.
        for (Type t : new HashSet<>(tm.getFirstOrderTypes())) {
            SymbolReference<ResolvedReferenceTypeDeclaration> symbol = resolver.tryToSolveType(t.getTypeName());
            if (symbol.isSolved()) {
                try {
                    List<ResolvedReferenceType> resolvedSuperTypes = symbol.getCorrespondingDeclaration().getAncestors(true);
                    for (ResolvedReferenceType anc : resolvedSuperTypes) {
                        // Add all resolved supertypes to the type.
                        Type superType = TypeParser.createFrom(anc.getQualifiedName(), false);
                        superType.setTypeOrigin(Type.Origin.RESOLVED);
                        t.getSuperTypes().add(superType);
                    }
                } catch (UnsolvedSymbolException e) {
                    // Even if the symbol itself is resolved, "getAnchestors()" may throw exception.
                    LOGGER.warn("Could not resolve supertypes of {}", symbol.getCorrespondingDeclaration());
                }
            }
        }
    }
}
Also used : UnsolvedSymbolException(com.github.javaparser.resolution.UnsolvedSymbolException) Type(de.fraunhofer.aisec.cpg.graph.types.Type) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) TypeManager(de.fraunhofer.aisec.cpg.graph.TypeManager) JavaLanguageFrontend(de.fraunhofer.aisec.cpg.frontends.java.JavaLanguageFrontend) HashSet(java.util.HashSet)

Aggregations

JavaLanguageFrontend (de.fraunhofer.aisec.cpg.frontends.java.JavaLanguageFrontend)2 UnsolvedSymbolException (com.github.javaparser.resolution.UnsolvedSymbolException)1 ResolvedReferenceTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration)1 ResolvedReferenceType (com.github.javaparser.resolution.types.ResolvedReferenceType)1 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)1 TranslationResult (de.fraunhofer.aisec.cpg.TranslationResult)1 de.fraunhofer.aisec.cpg.graph (de.fraunhofer.aisec.cpg.graph)1 NodeBuilder.newRecordDeclaration (de.fraunhofer.aisec.cpg.graph.NodeBuilder.newRecordDeclaration)1 TypeManager (de.fraunhofer.aisec.cpg.graph.TypeManager)1 de.fraunhofer.aisec.cpg.graph.declarations (de.fraunhofer.aisec.cpg.graph.declarations)1 DeclaredReferenceExpression (de.fraunhofer.aisec.cpg.graph.statements.expressions.DeclaredReferenceExpression)1 MemberCallExpression (de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberCallExpression)1 MemberExpression (de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberExpression)1 de.fraunhofer.aisec.cpg.graph.types (de.fraunhofer.aisec.cpg.graph.types)1 Type (de.fraunhofer.aisec.cpg.graph.types.Type)1 ScopedWalker (de.fraunhofer.aisec.cpg.helpers.SubgraphWalker.ScopedWalker)1 Util (de.fraunhofer.aisec.cpg.helpers.Util)1 Util.warnWithFileLocation (de.fraunhofer.aisec.cpg.helpers.Util.warnWithFileLocation)1 java.util (java.util)1 HashSet (java.util.HashSet)1