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));
}
}
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());
}
}
}
}
}
Aggregations