下面列出了怎么用com.intellij.psi.PsiPrimitiveType的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());
}
}
private static TypeName getPrimitiveTypeName(PsiPrimitiveType type) {
switch (type.getCanonicalText()) {
case "void":
return TypeName.VOID;
case "boolean":
return TypeName.BOOLEAN;
case "byte":
return TypeName.BYTE;
case "short":
return TypeName.SHORT;
case "int":
return TypeName.INT;
case "long":
return TypeName.LONG;
case "char":
return TypeName.CHAR;
case "float":
return TypeName.FLOAT;
case "double":
return TypeName.DOUBLE;
default:
throw new UnsupportedOperationException(
"Unknown primitive type: " + type.getCanonicalText());
}
}
@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");
}
@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
private static SuggestionNodeType getSuggestionNodeTypeForPrimitive(PsiType type) {
if (PsiType.BOOLEAN.equals(type) || PsiType.BOOLEAN
.equals(PsiPrimitiveType.getUnboxedType(type))) {
return BOOLEAN;
} else if (PsiType.BYTE.equals(type) || PsiType.BYTE
.equals(PsiPrimitiveType.getUnboxedType(type))) {
return BYTE;
} else if (PsiType.SHORT.equals(type) || PsiType.SHORT
.equals(PsiPrimitiveType.getUnboxedType(type))) {
return SHORT;
} else if (PsiType.INT.equals(type) || PsiType.INT
.equals(PsiPrimitiveType.getUnboxedType(type))) {
return INT;
} else if (PsiType.LONG.equals(type) || PsiType.LONG
.equals(PsiPrimitiveType.getUnboxedType(type))) {
return LONG;
} else if (PsiType.FLOAT.equals(type) || PsiType.FLOAT
.equals(PsiPrimitiveType.getUnboxedType(type))) {
return FLOAT;
} else if (PsiType.DOUBLE.equals(type) || PsiType.DOUBLE
.equals(PsiPrimitiveType.getUnboxedType(type))) {
return DOUBLE;
} else if (PsiType.CHAR.equals(type) || PsiType.CHAR
.equals(PsiPrimitiveType.getUnboxedType(type))) {
return CHAR;
}
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;
}
@NotNull
public static PsiType getBoxedTypeFromPrimitiveType(Module module,
PsiPrimitiveType primitiveType) {
PsiType boxedPrimitiveType = safeGetValidType(module, primitiveType.getBoxedTypeName());
assert boxedPrimitiveType instanceof PsiClassType;
return boxedPrimitiveType;
}
@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;
}
public void testVarModifiersEditing() {
PsiFile file = myFixture.configureByFile(getTestName(false) + ".java");
PsiLocalVariable var = PsiTreeUtil.getParentOfType(file.findElementAt(myFixture.getCaretOffset()), PsiLocalVariable.class);
assertNotNull(var);
assertNotNull(var.getType());
myFixture.type('1');
PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
assertTrue(var.isValid());
assertEquals(PsiPrimitiveType.INT, var.getType());
assertNotNull(var.getModifierList());
boolean isFinal = var.getModifierList().hasModifierProperty(PsiModifier.FINAL);
assertFalse("var doesn't make variable final", isFinal);
}
public static boolean isPrimitive(PsiField psiField) {
return (psiField.getType() instanceof PsiPrimitiveType);
}