下面列出了怎么用com.intellij.psi.PsiArrayType的API类实例代码及写法,或者点击链接到github查看源代码。
static TypeName getTypeName(PsiType type) {
if (type instanceof PsiPrimitiveType) {
// float
return getPrimitiveTypeName((PsiPrimitiveType) type);
} else if (type instanceof PsiClassType && ((PsiClassType) type).getParameterCount() > 0) {
// Set<Integer>
PsiClassType classType = (PsiClassType) type;
return ParameterizedTypeName.get(
guessClassName(classType.rawType().getCanonicalText()),
getTypeNameArray(classType.getParameters()));
} else if (type instanceof PsiArrayType) {
// int[]
PsiType componentType = ((PsiArrayType) type).getComponentType();
return ArrayTypeName.of(getTypeName(componentType));
} else if (type.getCanonicalText().contains("?")) {
// ? extends Type
return getWildcardTypeName(type.getCanonicalText());
} else {
return guessClassName(type.getCanonicalText());
}
}
@Nullable
public static PsiType safeGetValidType(@NotNull Module module, @NotNull String fqn) {
try {
// Intellij expects inner classes to be referred via `.` instead of `$`
PsiType type = JavaPsiFacade.getInstance(module.getProject()).getParserFacade()
.createTypeFromText(fqn.replaceAll("\\$", "."), null);
boolean typeValid = isValidType(type);
if (typeValid) {
if (type instanceof PsiClassType) {
return PsiClassType.class.cast(type);
} else if (type instanceof PsiArrayType) {
return PsiArrayType.class.cast(type);
}
}
return null;
} catch (IncorrectOperationException e) {
debug(() -> log.debug("Unable to find class fqn " + fqn));
return null;
}
}
public static boolean isValidType(@NotNull PsiType type) {
if (!type.isValid()) {
TimeoutUtil.sleep(
1); // to see if processing in another thread suddenly makes the type valid again (which is a bug)
if (!type.isValid()) {
return false;
}
}
if (type instanceof PsiArrayType) {
return isValidType(PsiArrayType.class.cast(type).getComponentType());
} else if (type instanceof PsiWildcardType) {
PsiType bound = ((PsiWildcardType) type).getBound();
return bound != null && isValidType(bound);
} else if (type instanceof PsiCapturedWildcardType) {
PsiType lowerBound = ((PsiCapturedWildcardType) type).getLowerBound();
type = (lowerBound != NULL ? lowerBound : ((PsiCapturedWildcardType) type).getUpperBound());
return type != NULL && isValidType(type);
} else if (type instanceof PsiClassType) {
PsiClassType.ClassResolveResult classResolveResult = ((PsiClassType) type).resolveGenerics();
return classResolveResult.isValidResult() && isValidElement(
requireNonNull(classResolveResult.getElement())) && !hasUnresolvedComponents(type);
}
return true;
}
@NotNull
public static MetadataProxy newMetadataProxy(Module module, @NotNull PsiType type) {
if (type instanceof PsiArrayType) {
return new ArrayMetadataProxy(module, (PsiArrayType) type);
} else if (type instanceof PsiPrimitiveType) {
PsiPrimitiveType primitiveType = (PsiPrimitiveType) type;
type = getBoxedTypeFromPrimitiveType(module, primitiveType);
}
if (type instanceof PsiClassType) {
SuggestionNodeType suggestionNodeType = getSuggestionNodeType(type);
if (suggestionNodeType == SuggestionNodeType.MAP) {
return new MapClassMetadataProxy((PsiClassType) type);
} else {
return new ClassMetadataProxy((PsiClassType) type);
}
}
throw new IllegalAccessError(
"Supports only PsiArrayType, PsiPrimitiveType & PsiClassType types");
}
public static String getResolvedResultTypeName(PsiMethod method) {
//return method.getReturnType().getCanonicalText();
PsiType type = method.getReturnType();
while (type instanceof PsiArrayType) {
type = ((PsiArrayType)type).getComponentType();
}
return type.getCanonicalText();
}
@Nullable
public static Map<PsiTypeParameter, PsiType> getTypeParameters(PsiType type) {
if (type instanceof PsiArrayType) {
return getTypeParameters(((PsiArrayType) type).getComponentType());
} else if (type instanceof PsiPrimitiveType) {
return null;
} else if (type instanceof PsiClassType) {
PsiClassType.ClassResolveResult resolveResult =
PsiClassType.class.cast(type).resolveGenerics();
if (resolveResult.isValidResult()) {
return resolveResult.getSubstitutor().getSubstitutionMap();
}
}
return null;
}
@Nullable
public static String toClassFqn(@NotNull PsiType type) {
if (type instanceof PsiArrayType) {
String componentLongName = toClassFqn(PsiArrayType.class.cast(type).getComponentType());
if (componentLongName != null) {
return componentLongName + "[]";
}
} else if (type instanceof PsiPrimitiveType) {
return type.getPresentableText();
} else if (type instanceof PsiClassType) {
return type.getCanonicalText();
}
return null;
}
@Nullable
public static String toClassNonQualifiedName(@NotNull PsiType type) {
if (type instanceof PsiArrayType) {
String componentLongName =
toClassNonQualifiedName(PsiArrayType.class.cast(type).getComponentType());
if (componentLongName != null) {
return componentLongName + "[]";
}
} else if (type instanceof PsiPrimitiveType) {
return type.getPresentableText();
} else if (type instanceof PsiClassType) {
return ((PsiClassType) type).getClassName();
}
return null;
}
@Nullable
public static String typeToFqn(Module module, @NotNull PsiType type) {
if (isValidType(type)) {
if (type instanceof PsiArrayType) {
type = PsiArrayType.class.cast(type).getComponentType();
return type.getCanonicalText();
} else if (type instanceof PsiPrimitiveType) {
return getBoxedTypeFromPrimitiveType(module, (PsiPrimitiveType) type).getCanonicalText();
} else if (type instanceof PsiClassType) {
return type.getCanonicalText();
}
}
return null;
}
@Nullable
public static PsiType getComponentType(@NotNull PsiType type) {
if (type instanceof PsiArrayType) {
return ((PsiArrayType) type).getComponentType();
}
return extractIterableTypeParameter(type, true);
}
ArrayMetadataProxy(Module module, @NotNull PsiArrayType type) {
this.type = type;
delegate = newMetadataProxy(module, type.getComponentType());
}