下面列出了com.intellij.psi.PsiClassType#resolve ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Make the class implementing Parcelable
*/
private void makeClassImplementParcelable(PsiElementFactory elementFactory, JavaCodeStyleManager styleManager) {
final PsiClassType[] implementsListTypes = psiClass.getImplementsListTypes();
final String implementsType = "android.os.Parcelable";
for (PsiClassType implementsListType : implementsListTypes) {
PsiClass resolved = implementsListType.resolve();
// Already implements Parcelable, no need to add it
if (resolved != null && implementsType.equals(resolved.getQualifiedName())) {
return;
}
}
PsiJavaCodeReferenceElement implementsReference =
elementFactory.createReferenceFromText(implementsType, psiClass);
PsiReferenceList implementsList = psiClass.getImplementsList();
if (implementsList != null) {
styleManager.shortenClassReferences(implementsList.add(implementsReference));
}
}
private void validateCleanUpMethodExists(@NotNull PsiLocalVariable psiVariable, @NotNull String cleanupName, @NotNull ProblemNewBuilder problemNewBuilder) {
final PsiType psiType = psiVariable.getType();
if (psiType instanceof PsiClassType) {
final PsiClassType psiClassType = (PsiClassType) psiType;
final PsiClass psiClassOfField = psiClassType.resolve();
final PsiMethod[] methods;
if (psiClassOfField != null) {
methods = psiClassOfField.findMethodsByName(cleanupName, true);
boolean hasCleanupMethod = false;
for (PsiMethod method : methods) {
if (0 == method.getParameterList().getParametersCount()) {
hasCleanupMethod = true;
}
}
if (!hasCleanupMethod) {
problemNewBuilder.addError("'@Cleanup': method '%s()' not found on target class", cleanupName);
}
}
} else {
problemNewBuilder.addError("'@Cleanup': is legal only on a local variable declaration inside a block");
}
}
@Nullable
public static PsiClass toValidPsiClass(@NotNull PsiClassType type) {
if (isValidType(type)) {
return type.resolve();
}
return null;
}
static List<PsiClass> getExtendsClasses(PsiClass psiClass, int lowDepth, int currentDepth){
List<PsiClass> list = new ArrayList<>();
for(PsiClassType type : psiClass.getExtendsListTypes()) {
PsiClass superPsiClass = type.resolve();
if(superPsiClass != null){
if(currentDepth + 1 >= lowDepth) {
list.add(superPsiClass);
}
list.addAll(getExtendsClasses(superPsiClass, lowDepth, currentDepth + 1));
}
}
return list;
}
static boolean hasSelectable(PsiClass psiClass){
PsiClassType[] listTypes = psiClass.getExtendsListTypes();
for(PsiClassType type : listTypes){
PsiClass superPsiClass = type.resolve();
if (superPsiClass != null && NAME_SELECTABLE.equals(superPsiClass.getQualifiedName())) {
return true;
}
}
return false;
}
public static String getNonGenericType(PsiType type) {
if (type instanceof PsiClassType) {
PsiClassType pct = (PsiClassType) type;
final PsiClass psiClass = pct.resolve();
return psiClass != null ? psiClass.getQualifiedName() : null;
}
return type.getCanonicalText();
}
public static boolean isInstanceOf(PsiClass instance, PsiClass interfaceExtendsClass) {
String className = interfaceExtendsClass.getQualifiedName();
if(className == null) {
return true;
}
if(className.equals(instance.getQualifiedName())) {
return true;
}
for(PsiClassType psiClassType: PsiClassImplUtil.getExtendsListTypes(instance)) {
PsiClass resolve = psiClassType.resolve();
if(resolve != null) {
if(className.equals(resolve.getQualifiedName())) {
return true;
}
}
}
for(PsiClass psiInterface: PsiClassImplUtil.getInterfaces(instance)) {
if(className.equals(psiInterface.getQualifiedName())) {
return true;
}
}
return false;
}
private static boolean isSubclassOf(
@NonNull JavaContext context,
@NonNull UExpression expression,
@NonNull Class<?> cls) {
final PsiType expressionType = expression.getExpressionType();
if (!(expressionType instanceof PsiClassType)) {
return false;
}
final PsiClassType classType = (PsiClassType) expressionType;
final PsiClass resolvedClass = classType.resolve();
return context.getEvaluator().extendsClass(resolvedClass, cls.getName(), false);
}
/**
* Return the appropriate return class for a given method element.
*
* @param psiMethod the method to get the return class from.
* @param expandType set this to true if return types annotated with @Provides(type=?)
* should be expanded to the appropriate collection type.
* @return the appropriate return class for the provided method element.
*/
public static PsiClass getReturnClassFromMethod(PsiMethod psiMethod, boolean expandType) {
if (psiMethod.isConstructor()) {
return psiMethod.getContainingClass();
}
PsiClassType returnType = ((PsiClassType) psiMethod.getReturnType());
if (returnType != null) {
// Check if has @Provides annotation and specified type
if (expandType) {
PsiAnnotationMemberValue attribValue = findTypeAttributeOfProvidesAnnotation(psiMethod);
if (attribValue != null) {
if (attribValue.textMatches(SET_TYPE)) {
String typeName = "java.util.Set<" + returnType.getCanonicalText() + ">";
returnType =
((PsiClassType) PsiElementFactory.SERVICE.getInstance(psiMethod.getProject())
.createTypeFromText(typeName, psiMethod));
} else if (attribValue.textMatches(MAP_TYPE)) {
// TODO(radford): Supporting map will require fetching the key type and also validating
// the qualifier for the provided key.
//
// String typeName = "java.util.Map<String, " + returnType.getCanonicalText() + ">";
// returnType = ((PsiClassType) PsiElementFactory.SERVICE.getInstance(psiMethod.getProject())
// .createTypeFromText(typeName, psiMethod));
}
}
}
return returnType.resolve();
}
return null;
}
public static String getNonGenericType(PsiType type) {
if (type instanceof PsiClassType) {
PsiClassType pct = (PsiClassType) type;
final PsiClass psiClass = pct.resolve();
return psiClass != null ? psiClass.getQualifiedName() : null;
}
return type.getCanonicalText();
}