下面列出了怎么用com.intellij.psi.impl.source.PsiClassReferenceType的API类实例代码及写法,或者点击链接到github查看源代码。
private boolean isPsiMethodCamelLanguage(PsiMethod method) {
PsiType type = method.getReturnType();
if (type != null && type instanceof PsiClassReferenceType) {
PsiClassReferenceType clazz = (PsiClassReferenceType) type;
PsiClass resolved = clazz.resolve();
if (resolved != null) {
boolean language = getCamelIdeaUtils().isCamelExpressionOrLanguage(resolved);
// try parent using some weird/nasty stub stuff which is how complex IDEA AST
// is when its parsing the Camel route builder
if (!language) {
PsiElement elem = resolved.getParent();
if (elem instanceof PsiTypeParameterList) {
elem = elem.getParent();
}
if (elem instanceof PsiClass) {
language = getCamelIdeaUtils().isCamelExpressionOrLanguage((PsiClass) elem);
}
}
return language;
}
}
return false;
}
@Nullable
public static InflateContainer matchInflate(PsiLocalVariable psiLocalVariable) {
PsiType psiType = psiLocalVariable.getType();
if(psiType instanceof PsiClassReferenceType) {
PsiMethodCallExpression psiMethodCallExpression = PsiTreeUtil.findChildOfType(psiLocalVariable, PsiMethodCallExpression.class);
if(psiMethodCallExpression != null) {
PsiMethod psiMethod = psiMethodCallExpression.resolveMethod();
// @TODO: replace "inflate"; resolve method and check nethod calls
if(psiMethod != null && psiMethod.getName().equals("inflate")) {
PsiExpression[] expressions = psiMethodCallExpression.getArgumentList().getExpressions();
if(expressions.length > 0 && expressions[0] instanceof PsiReferenceExpression) {
PsiFile xmlFile = AndroidUtils.findXmlResource((PsiReferenceExpression) expressions[0]);
if(xmlFile != null) {
return new InflateContainer(xmlFile, ((PsiLocalVariable) psiLocalVariable));
}
}
}
}
}
return null;
}
private static String processDiamondOperator(PsiElement element, TranslationContext ctx) {
String result;
// Java 7 diamond operator not possible in TypeScript
if (element instanceof PsiField) {
PsiField field = (PsiField) element;
result = process((PsiClassReferenceType) field.getType(), ctx, true);
} else if (element instanceof PsiLocalVariable) {
System.out.println("DIAMOND LOCALVAR> " + ((PsiLocalVariable) element).getType());
PsiLocalVariable localVar = (PsiLocalVariable) element;
result = process((PsiClassReferenceType) localVar.getType(), ctx, true);
} else if (element instanceof PsiAssignmentExpression) {
PsiAssignmentExpression assign = (PsiAssignmentExpression) element;
result = process((PsiClassReferenceType) assign.getLExpression().getType(), ctx, true);
} else {
System.out.println("DIAMOND > "+element);
result = "<any>";
}
return result;
}
public Optional<PsiType> getObtainViaFieldVariableType() {
PsiVariable psiVariable = variableInClass;
if (StringUtil.isNotEmpty(viaFieldName)) {
final PsiField fieldByName = getPsiClass().findFieldByName(viaFieldName, false);
if (fieldByName != null) {
psiVariable = fieldByName;
}
}
final PsiType psiVariableType = psiVariable.getType();
if (psiVariableType instanceof PsiClassReferenceType) {
final PsiClass resolvedPsiVariableClass = ((PsiClassReferenceType) psiVariableType).resolve();
if (resolvedPsiVariableClass instanceof PsiTypeParameter) {
return Optional.of(psiVariableType);
}
}
return Optional.empty();
}
private PsiClass findClass(final PsiType type) {
if (PsiClass.class.isInstance(type)) {
return PsiClass.class.cast(type);
}
if (PsiClassReferenceType.class.isInstance(type)) {
return PsiClassReferenceType.class.cast(type).resolve();
}
return null;
}
public PsiClass resolveClassReference(@NotNull PsiReference reference) {
final PsiElement resolveElement = reference.resolve();
if (resolveElement instanceof PsiClass) {
return (PsiClass) resolveElement;
} else if (resolveElement instanceof PsiField) {
final PsiType psiType = PsiUtil.getTypeByPsiElement(resolveElement);
if (psiType != null) {
return ((PsiClassReferenceType) psiType).resolve();
}
}
return null;
}
private static void translateClassField(PsiField element, TranslationContext ctx, DocMeta docMeta) {
PsiModifierList modifierList = element.getModifierList();
if (modifierList != null && modifierList.hasModifierProperty("private")) {
ctx.print("private ");
} else {
ctx.print("public ");
}
if (modifierList != null && modifierList.hasModifierProperty("static")) {
ctx.append("static ");
}
ctx.append(element.getName()).append(": ");
if (element.hasInitializer() && (element.getInitializer() instanceof PsiLambdaExpression)) {
if (element.getType() instanceof PsiClassReferenceType) {
try {
MethodTranslator.translateToLambdaType(((PsiClassReferenceType) element.getType()).rawType().resolve().getMethods()[0], ctx, docMeta);
} catch (Exception e){
((PsiClassReferenceType) element.getType()).rawType()
.resolve();
e.printStackTrace();
}
} else {
System.err.println("FieldTranslator:: Type not instance of PsiClassReferenceType. Could not translate lambda expression. (" + element.getType().getClass().getName() + ")");
}
} else {
ctx.append(TypeHelper.printType(element.getType(), ctx));
}
if (element.hasInitializer()) {
ctx.append(" = ");
ExpressionTranslator.translate(element.getInitializer(), ctx);
ctx.append(";\n");
} else {
ctx.append(";\n");
}
}
@NotNull
private String getTypeWithParameter(@NotNull PsiClassType psiClassType) {
if (psiClassType instanceof PsiClassReferenceType) {
return ((PsiClassReferenceType) psiClassType).getReference().getText();
}
return psiClassType.getName();
}
@Override
public TypeSerializer getSerializer(PsiType psiType) {
if (psiType instanceof PsiClassReferenceType && ((PsiClassReferenceType) psiType).resolve().isEnum()) {
return mSerializer;
}
return null;
}
@NotNull
private String getGenericType(SerializableValue field) {
String genericType = "";
try {
PsiType[] parameters = ((PsiClassReferenceType) field.getType()).getParameters();
if (parameters.length > 0) {
genericType = parameters[0].getCanonicalText();
}
} catch (Exception ignored) {
}
return genericType;
}
void processMethodInvocations(List<PsiExpression> methodInvocations) {
for (PsiExpression expression : methodInvocations) {
if (expression instanceof PsiMethodCallExpression) {
PsiMethodCallExpression methodInvocation = (PsiMethodCallExpression) expression;
PsiMethod resolveMethod = methodInvocation.resolveMethod();
String originClassName = "";
if (resolveMethod != null && resolveMethod.getContainingClass() != null) {
originClassName = resolveMethod.getContainingClass().getQualifiedName();
}
if (resolveMethod == null || methodInvocation.getMethodExpression().getQualifierExpression() != null) {
PsiReferenceExpression qualifierExpression = getFirstQualifierInAChain(methodInvocation);
if (qualifierExpression == null) {
PsiMethod resolvedMethod = resolveMethod(methodInvocation);
if (resolvedMethod != null && resolvedMethod.getContainingClass() != null) {
boolean isStatic = resolvedMethod.hasModifierProperty(PsiModifier.STATIC);
if (originClassName == null || originClassName.equals("")) {
originClassName = resolvedMethod.getContainingClass().getQualifiedName();
}
processMethodInvocation(methodInvocation, originClassName, isStatic);
}
} else {
if (originClassName == null || originClassName.equals("")) {
PsiElement resolvedElement = qualifierExpression.resolve();
if (resolvedElement instanceof PsiVariable) {
PsiType resolvedQualifierType = ((PsiVariable) resolvedElement).getType();
if (resolvedQualifierType instanceof PsiClassReferenceType) {
PsiClass resolvedClass = ((PsiClassReferenceType) resolvedQualifierType).resolve();
if (resolvedClass != null) {
originClassName = resolvedClass.getQualifiedName();
}
}
}
}
if (originClassName != null && !originClassName.equals("")) {
processMethodInvocation(methodInvocation, originClassName, false);
}
}
} else {
boolean isMethodStatic = resolveMethod.hasModifierProperty(PsiModifier.STATIC);
if (resolveMethod.getContainingClass() != null) {
if (originClassName == null || originClassName.equals("")) {
originClassName = resolveMethod.getContainingClass().getQualifiedName();
}
}
processMethodInvocation(methodInvocation, originClassName, isMethodStatic);
}
}
}
}
public static String process(PsiClassReferenceType classRefType, TranslationContext ctx, boolean withGenerics) {
String result = "";
PsiJavaCodeReferenceElement ref = classRefType.getReference();
if (ref.getText().endsWith("<>")) {
result = processDiamondOperator(ref.getParent().getParent(), ctx);
} else {
ArrayList<String> genericParameterNames = ctx.getGenericParameterNames();
if (classRefType.getParameterCount() > 0) {
String[] generics = new String[classRefType.getParameterCount()];
PsiType[] genericTypes = classRefType.getParameters();
for (int i = 0; i < genericTypes.length; i++) {
if (genericTypes[i] instanceof PsiWildcardType) {
if (((PsiWildcardType) genericTypes[i]).getBound() != null) {
if (classRefType.getReference().getParent().getParent() instanceof PsiParameter) {
PsiParameter param = (PsiParameter) classRefType.getReference().getParent().getParent();
PsiParameterList paramList = (PsiParameterList) param.getParent();
generics[i] = genericParameterNames.get(paramList.getParameterIndex(param));
}
} else {
generics[i] = "any";
}
} else {
generics[i] = TypeHelper.printType(genericTypes[i], ctx, true, false);
}
}
result += "<" + String.join(", ", generics) + ">";
} else if (withGenerics) {
PsiClass clazz = classRefType.resolve();
if (clazz != null) {
if (clazz.getTypeParameters().length > 0) {
String[] genericParams = new String[clazz.getTypeParameters().length];
for (int i=0; i < clazz.getTypeParameters().length; i++) {
genericParams[i] = "any";
}
result = "<" + String.join(", ", genericParams) + ">";
}
} else {
if (ref.getText().endsWith("<>")) {
result = processDiamondOperator(ref.getParent().getParent(), ctx);
}
}
} else {
if (classRefType.getPresentableText().contains("<")) {
result += "<any>";
}
}
}
return result;
}
public static String printType(PsiType element, TranslationContext ctx, boolean withGenericParams, boolean avoidNativeOptim) {
String result = element.getPresentableText();
if (result.equals("Throwable") || result.endsWith("Exception")) {
return "Error";
}
if (result.equals("Pattern")) {
return "RegExp";
}
if (objects.contains(result) || classes.contains(result)) {
return "any";
} else if (primitiveNumbers.contains(result) || objectNumbers.contains(result)) {
return "number";
} else if (strings.contains(result)) {
return "string";
} else if (booleans.contains(result)) {
return "boolean";
}
/*
if (ctx.NATIVE_ARRAY && !avoidNativeOptim && element.getArrayDimensions() == 1) {
return printArrayBaseType(element);
}*/
if (element instanceof PsiPrimitiveType) {
if (result.equals("null")) {
System.err.println("TypeHelper::printType -> Result null with elem:" + element.toString());
}
return result;
} else if (element instanceof PsiArrayType) {
PsiArrayType typedElement = (PsiArrayType) element;
String partialResult = printArrayBaseType(typedElement);
if (partialResult != null) {
result = "";
for (int i = 1; i < typedElement.getArrayDimensions(); i++) {
result += "Array<";
}
result += partialResult;
for (int i = 1; i < typedElement.getArrayDimensions(); i++) {
result += ">";
}
} else {
partialResult = printType(typedElement.getComponentType(), ctx, withGenericParams, avoidNativeOptim);
if (withGenericParams) {
result = partialResult + "[]";
} else {
result = partialResult;
}
}
return result;
} else if (element instanceof PsiClassReferenceType) {
PsiClassReferenceType elementClassRefType = ((PsiClassReferenceType) element);
PsiClass resolvedClass = elementClassRefType.resolve();
if (resolvedClass != null) {
if (resolvedClass.getQualifiedName() == null) {
result = resolvedClass.getName();
} else {
result = resolvedClass.getQualifiedName();
result += GenericHelper.process(elementClassRefType, ctx, withGenericParams);
}
} else {
if (((PsiClassReferenceType) element).getClassName().startsWith("Class")) {
// "Class" concept does not exists in TypeScript => any
result = "any";
} else {
String tryJavaUtil = javaTypes.get(elementClassRefType.getClassName());
if (tryJavaUtil != null) {
ctx.needsJava(tryJavaUtil);
result = tryJavaUtil;
} else {
result = elementClassRefType.getReference().getQualifiedName();
}
result += GenericHelper.process(elementClassRefType, ctx, withGenericParams);
}
}
} else if (element instanceof PsiWildcardType) {
PsiType bound = ((PsiWildcardType) element).getBound();
if (bound != null) {
result = "T";
} else {
result = "any";
}
} else {
System.out.println("TypeHelper: unhandled type -> " + element);
}
if (result.equals("null")) {
// this is kind of desperate but well...
result = element.getPresentableText();
}
return ctx.packageTransform(result);
}