use of com.intellij.codeInsight.ExpectedTypeInfo in project intellij-community by JetBrains.
the class ExpectedTypeMacro method getExpectedTypes.
@Nullable
private static PsiType[] getExpectedTypes(Expression[] params, final ExpressionContext context) {
if (params.length != 0)
return null;
final Project project = context.getProject();
PsiType[] types = null;
PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(context.getEditor().getDocument());
assert file != null;
final PsiFile fileCopy = (PsiFile) file.copy();
BlockSupport.getInstance(project).reparseRange(fileCopy, context.getTemplateStartOffset(), context.getTemplateEndOffset(), CompletionUtil.DUMMY_IDENTIFIER);
PsiElement element = fileCopy.findElementAt(context.getTemplateStartOffset());
if (element instanceof PsiIdentifier && element.getParent() instanceof PsiExpression) {
ExpectedTypeInfo[] infos = ExpectedTypesProvider.getExpectedTypes((PsiExpression) element.getParent(), true);
if (infos.length > 0) {
types = new PsiType[infos.length];
for (int i = 0; i < infos.length; i++) {
ExpectedTypeInfo info = infos[i];
types[i] = info.getType();
}
}
}
return types;
}
use of com.intellij.codeInsight.ExpectedTypeInfo in project intellij-community by JetBrains.
the class ExpectedTypesGetter method extractTypes.
@NotNull
public static PsiType[] extractTypes(ExpectedTypeInfo[] infos, boolean defaultTypes) {
Set<PsiType> result = new THashSet<>(infos.length);
for (ExpectedTypeInfo info : infos) {
final PsiType type = info.getType();
final PsiType defaultType = info.getDefaultType();
if (!defaultTypes && !defaultType.equals(type)) {
result.add(type);
}
result.add(defaultType);
}
return result.toArray(PsiType.createArray(result.size()));
}
use of com.intellij.codeInsight.ExpectedTypeInfo in project intellij-community by JetBrains.
the class CreateClassFromNewFix method setupInheritance.
private static void setupInheritance(PsiNewExpression element, PsiClass targetClass) throws IncorrectOperationException {
if (element.getParent() instanceof PsiReferenceExpression)
return;
ExpectedTypeInfo[] expectedTypes = ExpectedTypesProvider.getExpectedTypes(element, false);
for (ExpectedTypeInfo expectedType : expectedTypes) {
PsiType type = expectedType.getType();
if (!(type instanceof PsiClassType))
continue;
final PsiClassType classType = (PsiClassType) type;
PsiClass aClass = classType.resolve();
if (aClass == null)
continue;
if (aClass.equals(targetClass) || aClass.hasModifierProperty(PsiModifier.FINAL))
continue;
PsiElementFactory factory = JavaPsiFacade.getInstance(aClass.getProject()).getElementFactory();
if (aClass.isInterface()) {
PsiReferenceList implementsList = targetClass.getImplementsList();
assert implementsList != null : targetClass;
implementsList.add(factory.createReferenceElementByType(classType));
} else {
PsiReferenceList extendsList = targetClass.getExtendsList();
assert extendsList != null : targetClass;
if (extendsList.getReferencedTypes().length == 0 && !CommonClassNames.JAVA_LANG_OBJECT.equals(classType.getCanonicalText())) {
extendsList.add(factory.createReferenceElementByType(classType));
}
}
}
}
use of com.intellij.codeInsight.ExpectedTypeInfo in project intellij-community by JetBrains.
the class CreateMethodFromMethodReferenceFix method invokeImpl.
@Override
protected void invokeImpl(final PsiClass targetClass) {
if (targetClass == null)
return;
PsiMethodReferenceExpression expression = getMethodReference();
if (expression == null)
return;
if (isValidElement(expression))
return;
PsiClass parentClass = PsiTreeUtil.getParentOfType(expression, PsiClass.class);
PsiMember enclosingContext = PsiTreeUtil.getParentOfType(expression, PsiMethod.class, PsiField.class, PsiClassInitializer.class);
String methodName = expression.getReferenceName();
LOG.assertTrue(methodName != null);
final Project project = targetClass.getProject();
JVMElementFactory elementFactory = JVMElementFactories.getFactory(targetClass.getLanguage(), project);
if (elementFactory == null)
elementFactory = JavaPsiFacade.getElementFactory(project);
PsiMethod method = expression.isConstructor() ? (PsiMethod) targetClass.add(elementFactory.createConstructor()) : CreateMethodFromUsageFix.createMethod(targetClass, parentClass, enclosingContext, methodName);
if (method == null) {
return;
}
if (!expression.isConstructor()) {
setupVisibility(parentClass, targetClass, method.getModifierList());
}
expression = getMethodReference();
LOG.assertTrue(expression.isValid());
boolean shouldBeAbstract = false;
if (!expression.isConstructor()) {
if (shouldCreateStaticMember(expression, targetClass)) {
PsiUtil.setModifierProperty(method, PsiModifier.STATIC, true);
} else if (targetClass.isInterface()) {
shouldBeAbstract = true;
PsiCodeBlock body = method.getBody();
assert body != null;
body.delete();
}
}
final PsiElement context = PsiTreeUtil.getParentOfType(expression, PsiClass.class, PsiMethod.class);
final PsiType functionalInterfaceType = expression.getFunctionalInterfaceType();
final PsiClassType.ClassResolveResult classResolveResult = PsiUtil.resolveGenericsClassInType(functionalInterfaceType);
final PsiMethod interfaceMethod = LambdaUtil.getFunctionalInterfaceMethod(classResolveResult);
LOG.assertTrue(interfaceMethod != null);
final PsiType interfaceReturnType = LambdaUtil.getFunctionalInterfaceReturnType(functionalInterfaceType);
LOG.assertTrue(interfaceReturnType != null);
final PsiSubstitutor substitutor = LambdaUtil.getSubstitutor(interfaceMethod, classResolveResult);
final ExpectedTypeInfo[] expectedTypes = { new ExpectedTypeInfoImpl(interfaceReturnType, ExpectedTypeInfo.TYPE_OR_SUBTYPE, interfaceReturnType, TailType.NONE, null, ExpectedTypeInfoImpl.NULL) };
CreateMethodFromUsageFix.doCreate(targetClass, method, shouldBeAbstract, ContainerUtil.map2List(interfaceMethod.getParameterList().getParameters(), parameter -> Pair.create(null, substitutor.substitute(parameter.getType()))), PsiSubstitutor.EMPTY, expectedTypes, context);
}
use of com.intellij.codeInsight.ExpectedTypeInfo in project intellij-community by JetBrains.
the class CreateEnumConstantFromUsageFix method isAvailableImpl.
@Override
protected boolean isAvailableImpl(int offset) {
if (!super.isAvailableImpl(offset))
return false;
PsiElement element = getElement();
final List<PsiClass> classes = getTargetClasses(element);
if (classes.size() != 1 || !classes.get(0).isEnum())
return false;
ExpectedTypeInfo[] typeInfos = CreateFromUsageUtils.guessExpectedTypes(myReferenceExpression, false);
PsiType enumType = JavaPsiFacade.getInstance(myReferenceExpression.getProject()).getElementFactory().createType(classes.get(0));
for (final ExpectedTypeInfo typeInfo : typeInfos) {
if (ExpectedTypeUtil.matches(enumType, typeInfo))
return true;
}
return false;
}
Aggregations