private static String createInitializerReplacementText( PsiType varType, PsiExpression initializer) { final PsiType initializerType = initializer.getType(); final PsiClassType rawType = initializerType instanceof PsiClassType ? ((PsiClassType) initializerType).rawType() : null; final PsiClassType rawVarType = varType instanceof PsiClassType ? ((PsiClassType) varType).rawType() : null; if (rawType != null && rawVarType != null && rawType.equalsToText(CommonClassNames.JAVA_UTIL_ARRAY_LIST) && rawVarType.equalsToText(CommonClassNames.JAVA_UTIL_LIST)) { return "toList()"; } else if (rawType != null && rawVarType != null && rawType.equalsToText(CommonClassNames.JAVA_UTIL_HASH_SET) && rawVarType.equalsToText(CommonClassNames.JAVA_UTIL_SET)) { return "toSet()"; } else if (rawType != null) { return "toCollection(" + rawType.getClassName() + "::new)"; } else { return "toCollection(() -> " + initializer.getText() + ")"; } }
@Override public Boolean visitClassType(PsiClassType classType) { if (classType.getParameters().length > 0) { PsiClassType rawType = classType.rawType(); return rawType.equalsToText(CommonClassNames.JAVA_LANG_CLASS); } PsiClass aClass = classType.resolve(); if (aClass != null && (aClass.isAnnotationType() || aClass.isEnum())) { return Boolean.TRUE; } return classType.equalsToText(CommonClassNames.JAVA_LANG_CLASS) || classType.equalsToText(CommonClassNames.JAVA_LANG_STRING); }
@NotNull @Override public PsiTypeParameter createTypeParameter(String name, PsiClassType[] superTypes) { @NonNls StringBuilder builder = new StringBuilder(); builder.append("public <").append(name); if (superTypes.length > 1 || superTypes.length == 1 && !superTypes[0].equalsToText(CommonClassNames.JAVA_LANG_OBJECT)) { builder.append(" extends "); for (PsiClassType type : superTypes) { if (type.equalsToText(CommonClassNames.JAVA_LANG_OBJECT)) continue; builder.append(type.getCanonicalText()).append('&'); } builder.delete(builder.length() - 1, builder.length()); } builder.append("> void foo(){}"); try { return createMethodFromText(builder.toString(), null).getTypeParameters()[0]; } catch (RuntimeException e) { throw new IncorrectOperationException("type parameter text: " + builder.toString()); } }
@Override public boolean equalsToText(@NotNull @NonNls String text) { return myDelegate.equalsToText(text); }