use of com.intellij.codeInsight.ExpectedTypeInfoImpl in project intellij-community by JetBrains.
the class JavaCompletionSorting method calcMatch.
private static int calcMatch(final List<String> words, int max, ExpectedTypeInfo[] myExpectedInfos) {
for (ExpectedTypeInfo myExpectedInfo : myExpectedInfos) {
String expectedName = ((ExpectedTypeInfoImpl) myExpectedInfo).getExpectedName();
if (expectedName == null)
continue;
max = calcMatch(expectedName, words, max);
max = calcMatch(truncDigits(expectedName), words, max);
}
return max;
}
use of com.intellij.codeInsight.ExpectedTypeInfoImpl in project intellij-community by JetBrains.
the class JavaSmartCompletionContributor method getExpectedTypes.
@NotNull
public static ExpectedTypeInfo[] getExpectedTypes(PsiElement position, boolean voidable) {
if (psiElement().withParent(psiElement(PsiReferenceExpression.class).withParent(PsiThrowStatement.class)).accepts(position)) {
final PsiElementFactory factory = JavaPsiFacade.getInstance(position.getProject()).getElementFactory();
final PsiClassType classType = factory.createTypeByFQClassName(CommonClassNames.JAVA_LANG_RUNTIME_EXCEPTION, position.getResolveScope());
final List<ExpectedTypeInfo> result = new SmartList<>();
result.add(new ExpectedTypeInfoImpl(classType, ExpectedTypeInfo.TYPE_OR_SUBTYPE, classType, TailType.SEMICOLON, null, ExpectedTypeInfoImpl.NULL));
final PsiMethod method = PsiTreeUtil.getContextOfType(position, PsiMethod.class, true);
if (method != null) {
for (final PsiClassType type : method.getThrowsList().getReferencedTypes()) {
result.add(new ExpectedTypeInfoImpl(type, ExpectedTypeInfo.TYPE_OR_SUBTYPE, type, TailType.SEMICOLON, null, ExpectedTypeInfoImpl.NULL));
}
}
return result.toArray(new ExpectedTypeInfo[result.size()]);
}
PsiExpression expression = PsiTreeUtil.getContextOfType(position, PsiExpression.class, true);
if (expression == null)
return ExpectedTypeInfo.EMPTY_ARRAY;
return ExpectedTypesProvider.getExpectedTypes(expression, true, voidable, false);
}
use of com.intellij.codeInsight.ExpectedTypeInfoImpl in project intellij-community by JetBrains.
the class SmartCastProvider method getParenthesizedCastExpectationByOperandType.
@Nullable
static ExpectedTypeInfo getParenthesizedCastExpectationByOperandType(PsiElement position) {
PsiElement parenthesisOwner = getParenthesisOwner(position);
PsiExpression operand = getCastedExpression(parenthesisOwner);
if (operand == null || !(parenthesisOwner.getParent() instanceof PsiParenthesizedExpression))
return null;
PsiType dfaType = GuessManager.getInstance(operand.getProject()).getControlFlowExpressionType(operand);
if (dfaType != null) {
return new ExpectedTypeInfoImpl(dfaType, ExpectedTypeInfo.TYPE_OR_SUPERTYPE, dfaType, TailType.NONE, null, () -> null);
}
PsiType type = operand.getType();
return type == null || type.equalsToText(CommonClassNames.JAVA_LANG_OBJECT) ? null : new ExpectedTypeInfoImpl(type, ExpectedTypeInfo.TYPE_OR_SUBTYPE, type, TailType.NONE, null, () -> null);
}
use of com.intellij.codeInsight.ExpectedTypeInfoImpl in project intellij-community by JetBrains.
the class MethodReferenceCompletionProvider method addCompletions.
@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull final CompletionResultSet result) {
if (!PsiUtil.isLanguageLevel8OrHigher(parameters.getOriginalFile()))
return;
final PsiElement rulezzRef = parameters.getPosition().getParent();
if (rulezzRef == null || !LambdaUtil.isValidLambdaContext(rulezzRef.getParent()))
return;
final ExpectedTypeInfo[] expectedTypes = JavaSmartCompletionContributor.getExpectedTypes(parameters);
for (ExpectedTypeInfo expectedType : expectedTypes) {
final PsiType defaultType = expectedType.getDefaultType();
if (LambdaUtil.isFunctionalType(defaultType)) {
final PsiType functionalType = FunctionalInterfaceParameterizationUtil.getGroundTargetType(defaultType);
final PsiType returnType = LambdaUtil.getFunctionalInterfaceReturnType(functionalType);
if (returnType != null) {
final PsiElement position = parameters.getPosition();
final PsiElement refPlace = position.getParent();
final ExpectedTypeInfoImpl typeInfo = new ExpectedTypeInfoImpl(returnType, ExpectedTypeInfo.TYPE_OR_SUBTYPE, returnType, TailType.UNKNOWN, null, ExpectedTypeInfoImpl.NULL);
final Map<PsiElement, PsiType> map = LambdaUtil.getFunctionalTypeMap();
Consumer<LookupElement> noTypeCheck = new Consumer<LookupElement>() {
@Override
public void consume(final LookupElement lookupElement) {
final PsiElement element = lookupElement.getPsiElement();
if (element instanceof PsiMethod) {
final PsiMethodReferenceExpression referenceExpression = createMethodReferenceExpression((PsiMethod) element);
if (referenceExpression == null) {
return;
}
final PsiType added = map.put(referenceExpression, functionalType);
try {
final PsiElement resolve = referenceExpression.resolve();
if (resolve != null && PsiEquivalenceUtil.areElementsEquivalent(element, resolve) && PsiMethodReferenceUtil.checkMethodReferenceContext(referenceExpression, resolve, functionalType) == null) {
result.addElement(new JavaMethodReferenceElement((PsiMethod) element, refPlace));
}
} finally {
if (added == null) {
map.remove(referenceExpression);
}
}
}
}
private PsiMethodReferenceExpression createMethodReferenceExpression(PsiMethod method) {
PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(method.getProject());
if (refPlace instanceof PsiMethodReferenceExpression) {
final PsiMethodReferenceExpression referenceExpression = (PsiMethodReferenceExpression) refPlace.copy();
final PsiElement referenceNameElement = referenceExpression.getReferenceNameElement();
LOG.assertTrue(referenceNameElement != null, referenceExpression);
referenceNameElement.replace(method.isConstructor() ? elementFactory.createKeyword("new") : elementFactory.createIdentifier(method.getName()));
return referenceExpression;
} else if (method.hasModifierProperty(PsiModifier.STATIC)) {
final PsiClass aClass = method.getContainingClass();
LOG.assertTrue(aClass != null);
final String qualifiedName = aClass.getQualifiedName();
return (PsiMethodReferenceExpression) elementFactory.createExpressionFromText(qualifiedName + "::" + (method.isConstructor() ? "new" : method.getName()), refPlace);
} else {
return null;
}
}
};
final Runnable runnable = ReferenceExpressionCompletionContributor.fillCompletionVariants(new JavaSmartCompletionParameters(parameters, typeInfo), noTypeCheck);
if (runnable != null) {
runnable.run();
}
}
}
}
}
use of com.intellij.codeInsight.ExpectedTypeInfoImpl 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);
}
Aggregations