com.github.javaparser.symbolsolver.javassistmodel.JavassistInterfaceDeclaration Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javaparser-symbol-solver-core Show documentation
Show all versions of javaparser-symbol-solver-core Show documentation
A Symbol Solver for Java, built on top of JavaParser (core)
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2020 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver.javassistmodel;
import com.github.javaparser.ast.AccessSpecifier;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.resolution.MethodUsage;
import com.github.javaparser.resolution.UnsolvedSymbolException;
import com.github.javaparser.resolution.declarations.*;
import com.github.javaparser.resolution.types.ResolvedReferenceType;
import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.symbolsolver.core.resolution.Context;
import com.github.javaparser.symbolsolver.core.resolution.MethodUsageResolutionCapability;
import com.github.javaparser.symbolsolver.core.resolution.SymbolResolutionCapability;
import com.github.javaparser.symbolsolver.logic.AbstractTypeDeclaration;
import com.github.javaparser.symbolsolver.logic.MethodResolutionCapability;
import com.github.javaparser.symbolsolver.model.resolution.SymbolReference;
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.resolution.SymbolSolver;
import javassist.CtClass;
import javassist.CtField;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author Federico Tomassetti
*/
public class JavassistInterfaceDeclaration extends AbstractTypeDeclaration
implements ResolvedInterfaceDeclaration, MethodResolutionCapability, MethodUsageResolutionCapability,
SymbolResolutionCapability {
private CtClass ctClass;
private TypeSolver typeSolver;
private JavassistTypeDeclarationAdapter javassistTypeDeclarationAdapter;
@Override
public String toString() {
return "JavassistInterfaceDeclaration{" +
"ctClass=" + ctClass.getName() +
", typeSolver=" + typeSolver +
'}';
}
public JavassistInterfaceDeclaration(CtClass ctClass, TypeSolver typeSolver) {
if (!ctClass.isInterface()) {
throw new IllegalArgumentException("Not an interface: " + ctClass.getName());
}
this.ctClass = ctClass;
this.typeSolver = typeSolver;
this.javassistTypeDeclarationAdapter = new JavassistTypeDeclarationAdapter(ctClass, typeSolver, this);
}
@Override
public List getInterfacesExtended() {
return javassistTypeDeclarationAdapter.getInterfaces();
}
@Override
public String getPackageName() {
return ctClass.getPackageName();
}
@Override
public String getClassName() {
String className = ctClass.getName().replace('$', '.');
if (getPackageName() != null) {
return className.substring(getPackageName().length() + 1);
}
return className;
}
@Override
public String getQualifiedName() {
return ctClass.getName().replace('$', '.');
}
@Deprecated
public Optional solveMethodAsUsage(String name, List argumentsTypes,
Context invokationContext, List typeParameterValues) {
return JavassistUtils.solveMethodAsUsage(name, argumentsTypes, typeSolver, invokationContext, typeParameterValues, this, ctClass);
}
@Override
@Deprecated
public SymbolReference solveMethod(String name, List argumentsTypes, boolean staticOnly) {
return JavassistUtils.solveMethod(name, argumentsTypes, staticOnly, typeSolver, this, ctClass);
}
@Override
public boolean isAssignableBy(ResolvedType type) {
return javassistTypeDeclarationAdapter.isAssignableBy(type);
}
@Override
public List getAllFields() {
return javassistTypeDeclarationAdapter.getDeclaredFields();
}
@Override
public boolean isAssignableBy(ResolvedReferenceTypeDeclaration other) {
return javassistTypeDeclarationAdapter.isAssignableBy(other);
}
@Override
public List getAncestors(boolean acceptIncompleteList) {
return javassistTypeDeclarationAdapter.getAncestors(acceptIncompleteList);
}
@Override
public Set getDeclaredMethods() {
return Arrays.stream(ctClass.getDeclaredMethods())
.map(m -> new JavassistMethodDeclaration(m, typeSolver))
.collect(Collectors.toSet());
}
@Override
public boolean hasDirectlyAnnotation(String canonicalName) {
return ctClass.hasAnnotation(canonicalName);
}
@Override
public String getName() {
String[] nameElements = ctClass.getSimpleName().replace('$', '.').split("\\.");
return nameElements[nameElements.length - 1];
}
@Override
public List getTypeParameters() {
return javassistTypeDeclarationAdapter.getTypeParameters();
}
@Override
public AccessSpecifier accessSpecifier() {
return JavassistFactory.modifiersToAccessLevel(ctClass.getModifiers());
}
@Override
public ResolvedInterfaceDeclaration asInterface() {
return this;
}
@Override
public SymbolReference extends ResolvedValueDeclaration> solveSymbol(String name, TypeSolver typeSolver) {
for (CtField field : ctClass.getDeclaredFields()) {
if (field.getName().equals(name)) {
return SymbolReference.solved(new JavassistFieldDeclaration(field, typeSolver));
}
}
String[] interfaceFQNs = getInterfaceFQNs();
for (String interfaceFQN : interfaceFQNs) {
SymbolReference extends ResolvedValueDeclaration> interfaceRef = solveSymbolForFQN(name, interfaceFQN);
if (interfaceRef.isSolved()) {
return interfaceRef;
}
}
return SymbolReference.unsolved();
}
private SymbolReference extends ResolvedValueDeclaration> solveSymbolForFQN(String symbolName, String fqn) {
if (fqn == null) {
return SymbolReference.unsolved();
}
ResolvedReferenceTypeDeclaration fqnTypeDeclaration = typeSolver.solveType(fqn);
return new SymbolSolver(typeSolver).solveSymbolInType(fqnTypeDeclaration, symbolName);
}
private String[] getInterfaceFQNs() {
return ctClass.getClassFile().getInterfaces();
}
@Override
public Optional containerType() {
return javassistTypeDeclarationAdapter.containerType();
}
@Override
public Set internalTypes() {
return javassistTypeDeclarationAdapter.internalTypes();
}
@Override
public ResolvedReferenceTypeDeclaration getInternalType(String name) {
/*
The name of the ReferenceTypeDeclaration could be composed on the internal class and the outer class, e.g. A$B. That's why we search the internal type in the ending part.
In case the name is composed of the internal type only, i.e. f.getName() returns B, it will also works.
*/
Optional type =
this.internalTypes().stream().filter(f -> f.getName().endsWith(name)).findFirst();
return type.orElseThrow(() ->
new UnsolvedSymbolException("Internal type not found: " + name));
}
@Override
public boolean hasInternalType(String name) {
/*
The name of the ReferenceTypeDeclaration could be composed on the internal class and the outer class, e.g. A$B. That's why we search the internal type in the ending part.
In case the name is composed of the internal type only, i.e. f.getName() returns B, it will also works.
*/
return this.internalTypes().stream().anyMatch(f -> f.getName().endsWith(name));
}
@Override
public List getConstructors() {
return Collections.emptyList();
}
@Override
public Optional toAst() {
return Optional.empty();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy