当前位置: 首页>>代码示例>>Java>>正文


Java PsiUtil.resolveGenericsClassInType方法代码示例

本文整理汇总了Java中com.intellij.psi.util.PsiUtil.resolveGenericsClassInType方法的典型用法代码示例。如果您正苦于以下问题:Java PsiUtil.resolveGenericsClassInType方法的具体用法?Java PsiUtil.resolveGenericsClassInType怎么用?Java PsiUtil.resolveGenericsClassInType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.intellij.psi.util.PsiUtil的用法示例。


在下文中一共展示了PsiUtil.resolveGenericsClassInType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getLambdaParameterFromType

import com.intellij.psi.util.PsiUtil; //导入方法依赖的package包/类
private static PsiType getLambdaParameterFromType(int parameterIndex, PsiLambdaExpression lambdaExpression, PsiType conjunct) {
  final PsiClassType.ClassResolveResult resolveResult = PsiUtil.resolveGenericsClassInType(conjunct);
  if (resolveResult != null) {
    final PsiMethod method = LambdaUtil.getFunctionalInterfaceMethod(conjunct);
    if (method != null) {
      final PsiParameter[] parameters = method.getParameterList().getParameters();
      if (parameterIndex < parameters.length) {
        final PsiType psiType = LambdaUtil.getSubstitutor(method, resolveResult).substitute(parameters[parameterIndex].getType());
        if (!LambdaUtil.dependsOnTypeParams(psiType, conjunct, lambdaExpression)) {
          return psiType;
        }
      }
    }
  }
  return null;
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:17,代码来源:PsiParameterImpl.java

示例2: getExpectedArgumentsTypesForNewExpression

import com.intellij.psi.util.PsiUtil; //导入方法依赖的package包/类
private void getExpectedArgumentsTypesForNewExpression(@NotNull final PsiNewExpression newExpr,
                                                       @NotNull final PsiExpressionList list) {
  PsiType newType = newExpr.getType();
  if (newType instanceof PsiClassType) {
    JavaResolveResult resolveResult = PsiUtil.resolveGenericsClassInType(newType);
    PsiClass newClass = (PsiClass)resolveResult.getElement();
    final PsiSubstitutor substitutor;
    if (newClass instanceof PsiAnonymousClass) {
      final PsiAnonymousClass anonymous = (PsiAnonymousClass)newClass;
      newClass = anonymous.getBaseClassType().resolve();
      if (newClass == null) return;

      substitutor = TypeConversionUtil.getSuperClassSubstitutor(newClass, anonymous, PsiSubstitutor.EMPTY);
    } else if (newClass != null) {
      substitutor = resolveResult.getSubstitutor();
    }
    else {
      return;
    }
    getExpectedTypesForConstructorCall(newClass, list, substitutor);
  }
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:23,代码来源:ExpectedTypesProvider.java

示例3: wrapWithNewExpression

import com.intellij.psi.util.PsiUtil; //导入方法依赖的package包/类
public static TypeConversionDescriptor wrapWithNewExpression(PsiType to, PsiType from, @Nullable PsiExpression expression, PsiElement context) {
  final String typeText = PsiDiamondTypeUtil.getCollapsedType(to, context);
  final PsiClassType.ClassResolveResult resolveResult = PsiUtil.resolveGenericsClassInType(to);
  final PsiClass atomicClass = resolveResult.getElement();
  LOG.assertTrue(atomicClass != null);
  final PsiTypeParameter[] typeParameters = atomicClass.getTypeParameters();
  if (typeParameters.length == 1) {
    final PsiType initial = resolveResult.getSubstitutor().substitute(typeParameters[0]);
    final PsiPrimitiveType unboxedInitialType = PsiPrimitiveType.getUnboxedType(initial);
    if (unboxedInitialType != null) {
      LOG.assertTrue(initial != null);
      if (from instanceof PsiPrimitiveType) {
        final PsiClassType boxedFromType = ((PsiPrimitiveType)from).getBoxedType(atomicClass);
        LOG.assertTrue(boxedFromType != null);
        if (!TypeConversionUtil.isAssignable(initial, boxedFromType)) {
          return new TypeConversionDescriptor("$val$", "new " + typeText + "((" + unboxedInitialType.getCanonicalText() + ")$val$)", expression);
        }
      }
    }
  }
  return new TypeConversionDescriptor("$val$", "new " + typeText + "($val$)", expression);
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:23,代码来源:AtomicConversionRule.java

示例4: getBoxedWrapper

import com.intellij.psi.util.PsiUtil; //导入方法依赖的package包/类
private static String getBoxedWrapper(final PsiType from, final PsiType to, @NotNull String arg) {
  final PsiClassType.ClassResolveResult resolveResult = PsiUtil.resolveGenericsClassInType(to);
  final PsiClass atomicClass = resolveResult.getElement();
  LOG.assertTrue(atomicClass != null);
  final PsiTypeParameter[] typeParameters = atomicClass.getTypeParameters();
  if (typeParameters.length == 1) {
    final PsiSubstitutor substitutor = resolveResult.getSubstitutor();
    LOG.assertTrue(substitutor.isValid());
    final PsiType initial = substitutor.substitute(typeParameters[0]);
    final PsiPrimitiveType unboxedInitialType = PsiPrimitiveType.getUnboxedType(initial);
    if (unboxedInitialType != null) {
      LOG.assertTrue(initial != null);
      if (from instanceof PsiPrimitiveType) {
        final PsiClassType boxedFromType = ((PsiPrimitiveType)from).getBoxedType(atomicClass);
        LOG.assertTrue(boxedFromType != null);
        return "new " + initial.getPresentableText() + "((" + unboxedInitialType.getCanonicalText() + ")(" + arg + "))";
      }
    }
  }
  return arg;
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:22,代码来源:AtomicConversionRule.java

示例5: processTypeDeclarations

import com.intellij.psi.util.PsiUtil; //导入方法依赖的package包/类
public static void processTypeDeclarations(PsiType type, PsiElement place, PsiScopeProcessor processor) {
  if (type instanceof PsiArrayType) {
    LanguageLevel languageLevel = PsiUtil.getLanguageLevel(place);
    final PsiClass arrayClass = JavaPsiFacade.getInstance(place.getProject()).getElementFactory().getArrayClass(languageLevel);
    final PsiTypeParameter[] arrayTypeParameters = arrayClass.getTypeParameters();
    PsiSubstitutor substitutor = PsiSubstitutor.EMPTY;
    if (arrayTypeParameters.length > 0) {
      substitutor = substitutor.put(arrayTypeParameters[0], ((PsiArrayType)type).getComponentType());
    }
    arrayClass.processDeclarations(processor, ResolveState.initial().put(PsiSubstitutor.KEY, substitutor), arrayClass, place);
  }
  else if (type instanceof PsiIntersectionType) {
    for (PsiType psiType : ((PsiIntersectionType)type).getConjuncts()) {
      processTypeDeclarations(psiType, place, processor);
    }
  }
  else if (type instanceof PsiDisjunctionType) {
    final PsiType lub = ((PsiDisjunctionType)type).getLeastUpperBound();
    processTypeDeclarations(lub, place, processor);
  }
  else {
    final JavaResolveResult result = PsiUtil.resolveGenericsClassInType(type);
    final PsiClass clazz = (PsiClass)result.getElement();
    if (clazz != null) {
      clazz.processDeclarations(processor, ResolveState.initial().put(PsiSubstitutor.KEY, result.getSubstitutor()), clazz, place);
    }
  }
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:29,代码来源:PsiScopesUtil.java

示例6: resolveType

import com.intellij.psi.util.PsiUtil; //导入方法依赖的package包/类
public static PsiClassType.ClassResolveResult resolveType(PsiType type) {
  final PsiClassType.ClassResolveResult resolveResult = PsiUtil.resolveGenericsClassInType(type);
  final PsiClass aClass = resolveResult.getElement();
  if (aClass instanceof PsiAnonymousClass) {
    final PsiClassType baseClassType = ((PsiAnonymousClass)aClass).getBaseClassType();
    return resolveType(resolveResult.getSubstitutor().substitute(baseClassType));
  }
  return resolveResult;
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:10,代码来源:Util.java

示例7: isBottomArgument

import com.intellij.psi.util.PsiUtil; //导入方法依赖的package包/类
private static boolean isBottomArgument(final PsiType type) {
  final PsiClassType.ClassResolveResult resolveResult = PsiUtil.resolveGenericsClassInType(type);
  final PsiClass clazz = resolveResult.getElement();
  if (clazz != null) {
    for (PsiTypeParameter typeParameter : PsiUtil.typeParametersIterable(clazz)) {
      final PsiType t = resolveResult.getSubstitutor().substitute(typeParameter);
      if (t == Bottom.BOTTOM) return true;
    }
  }

  return false;
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:13,代码来源:Result.java

示例8: isThreadLocalTypeMigration

import com.intellij.psi.util.PsiUtil; //导入方法依赖的package包/类
private static boolean isThreadLocalTypeMigration(PsiType from, PsiClassType to, PsiExpression context) {
  final PsiClassType.ClassResolveResult resolveResult = PsiUtil.resolveGenericsClassInType(to);
  final PsiClass threadLocalClass = resolveResult.getElement();

  if (threadLocalClass != null) {
    final String typeQualifiedName = threadLocalClass.getQualifiedName();
    if (!Comparing.strEqual(typeQualifiedName, ThreadLocal.class.getName())) {
      return false;
    }
    final PsiTypeParameter[] typeParameters = threadLocalClass.getTypeParameters();
    if (typeParameters.length != 1) return !PsiUtil.isLanguageLevel5OrHigher(context);
    final PsiType toTypeParameterValue = resolveResult.getSubstitutor().substitute(typeParameters[0]);
    if (toTypeParameterValue != null) {
      if (from instanceof PsiPrimitiveType) {
        final PsiPrimitiveType unboxedInitialType = PsiPrimitiveType.getUnboxedType(toTypeParameterValue);
        if (unboxedInitialType != null) {
          return TypeConversionUtil.areTypesConvertible(from, unboxedInitialType);
        }
      }
      else {
        return TypeConversionUtil.isAssignable(from, PsiUtil.captureToplevelWildcards(toTypeParameterValue, context));
      }
    }
    return !PsiUtil.isLanguageLevel5OrHigher(context);
  }
  return false;
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:28,代码来源:ThreadLocalConversionRule.java

示例9: getBoxedWrapper

import com.intellij.psi.util.PsiUtil; //导入方法依赖的package包/类
private static String getBoxedWrapper(final PsiType from,
                                      final PsiType to,
                                      @NotNull String arg,
                                      TypeMigrationLabeler labeler,
                                      PsiElement context,
                                      @Nullable String tryType) {
  if (from instanceof PsiPrimitiveType) {
    final PsiClassType.ClassResolveResult resolveResult = PsiUtil.resolveGenericsClassInType(to);
    final PsiClass threadLocalClass = resolveResult.getElement();
    LOG.assertTrue(threadLocalClass != null);
    final PsiTypeParameter[] typeParameters = threadLocalClass.getTypeParameters();
    if (typeParameters.length == 1) {
      final PsiType initial = resolveResult.getSubstitutor().substitute(typeParameters[0]);
      final PsiPrimitiveType unboxedInitialType = PsiPrimitiveType.getUnboxedType(initial);
      if (unboxedInitialType != null) {
        LOG.assertTrue(initial != null);
        if (tryType != null) {
          final PsiType exprType = labeler.getTypeEvaluator().evaluateType(
            JavaPsiFacade.getElementFactory(threadLocalClass.getProject()).createExpressionFromText(tryType, context));
          if (exprType != null && unboxedInitialType.isAssignableFrom(exprType)) {
            return toBoxed(arg, from, context);
          }
        }
        return "new " + initial.getCanonicalText() + "((" + unboxedInitialType.getCanonicalText() + ")(" + arg + "))";
      }
    }
  }
  return toBoxed(arg, from, context);
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:30,代码来源:ThreadLocalConversionRule.java

示例10: getInferredTypes

import com.intellij.psi.util.PsiUtil; //导入方法依赖的package包/类
@Nullable
private static String getInferredTypes(PsiType functionalInterfaceType, final PsiLambdaExpression lambdaExpression, boolean useFQN) {
  final PsiClassType.ClassResolveResult resolveResult = PsiUtil.resolveGenericsClassInType(functionalInterfaceType);
  final StringBuilder buf = new StringBuilder();
  buf.append("(");
  final PsiMethod interfaceMethod = LambdaUtil.getFunctionalInterfaceMethod(functionalInterfaceType);
  LOG.assertTrue(interfaceMethod != null);
  final PsiParameter[] parameters = interfaceMethod.getParameterList().getParameters();
  final PsiParameter[] lambdaParameters = lambdaExpression.getParameterList().getParameters();
  if (parameters.length != lambdaParameters.length) return null;
  for (int i = 0; i < parameters.length; i++) {
    PsiParameter parameter = parameters[i];
    final PsiType psiType = LambdaUtil.getSubstitutor(interfaceMethod, resolveResult).substitute(parameter.getType());
    if (!PsiTypesUtil.isDenotableType(psiType)) return null;
    if (psiType != null) {
      buf.append(useFQN ? psiType.getCanonicalText() : psiType.getPresentableText()).append(" ").append(lambdaParameters[i].getName());
    }
    else {
      buf.append(lambdaParameters[i].getName());
    }
    if (i < parameters.length - 1) {
      buf.append(", ");
    }
  }
  buf.append(")");
  return buf.toString();
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:28,代码来源:InferLambdaParameterTypeIntention.java

示例11: reduce

import com.intellij.psi.util.PsiUtil; //导入方法依赖的package包/类
@Override
public boolean reduce(InferenceSession session, List<ConstraintFormula> constraints) {
  if (!LambdaUtil.isFunctionalType(myT)) {
    return false;
  }

  final PsiType groundTargetType = FunctionalInterfaceParameterizationUtil.getGroundTargetType(myT, myExpression, false);
  final PsiClassType.ClassResolveResult resolveResult = PsiUtil.resolveGenericsClassInType(groundTargetType);
  final PsiMethod interfaceMethod = LambdaUtil.getFunctionalInterfaceMethod(resolveResult);
  if (interfaceMethod == null) {
    return false;
  }
  final PsiSubstitutor substitutor = LambdaUtil.getSubstitutor(interfaceMethod, resolveResult);
  final PsiParameter[] parameters = interfaceMethod.getParameterList().getParameters();

  final PsiParameter[] lambdaParameters = myExpression.getParameterList().getParameters();
  if (lambdaParameters.length != parameters.length) {
    return false;
  }
  if (myExpression.hasFormalParameterTypes()) {
    for (int i = 0; i < lambdaParameters.length; i++) {
      constraints.add(new TypeEqualityConstraint(lambdaParameters[i].getType(), substitutor.substitute(parameters[i].getType())));
    }
    constraints.add(new StrictSubtypingConstraint(myT, groundTargetType));
  } else {
    for (PsiParameter parameter : parameters) {
      if (!session.isProperType(session.substituteWithInferenceVariables(substitutor.substitute(parameter.getType())))) {
        return false;
      }
    }
  }

  PsiType returnType = interfaceMethod.getReturnType();
  if (returnType != null) {
    final List<PsiExpression> returnExpressions = LambdaUtil.getReturnExpressions(myExpression);
    if (returnType.equals(PsiType.VOID)) {
      if (!myExpression.isVoidCompatible()) {
        return false;
      }
    } else {
      if (!myExpression.isValueCompatible()) {
        return false;
      }
      InferenceSession callsession = session.findNestedCallSession(myExpression);
      returnType = callsession.substituteWithInferenceVariables(substitutor.substitute(returnType));
      if (!callsession.isProperType(returnType)) {
        for (PsiExpression returnExpression : returnExpressions) {
          constraints.add(new ExpressionCompatibilityConstraint(returnExpression, returnType));
        }
      }
    }
  }
  return true;
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:55,代码来源:LambdaExpressionCompatibilityConstraint.java

示例12: getInputVariables

import com.intellij.psi.util.PsiUtil; //导入方法依赖的package包/类
public Set<InferenceVariable> getInputVariables(InferenceSession session) {
  final PsiExpression psiExpression = getExpression();
  final PsiType type = getT();
  if (psiExpression instanceof PsiFunctionalExpression) {
    final InferenceVariable inferenceVariable = session.getInferenceVariable(type);
    if (inferenceVariable != null) {
      return Collections.singleton(inferenceVariable);
    }
    if (LambdaUtil.isFunctionalType(type)) {
      final PsiType functionType =
        psiExpression instanceof PsiLambdaExpression
        ? FunctionalInterfaceParameterizationUtil.getGroundTargetType(type, (PsiLambdaExpression)psiExpression, false)
        : type;
      final PsiClassType.ClassResolveResult resolveResult = PsiUtil.resolveGenericsClassInType(functionType);
      final PsiMethod interfaceMethod = LambdaUtil.getFunctionalInterfaceMethod(resolveResult);
      if (interfaceMethod != null) {

        final Set<InferenceVariable> result = new HashSet<InferenceVariable>();
        final PsiSubstitutor substitutor = LambdaUtil.getSubstitutor(interfaceMethod, resolveResult);
        if (psiExpression instanceof PsiLambdaExpression && !((PsiLambdaExpression)psiExpression).hasFormalParameterTypes() || 
            psiExpression instanceof PsiMethodReferenceExpression && !((PsiMethodReferenceExpression)psiExpression).isExact()) {
          for (PsiParameter parameter : interfaceMethod.getParameterList().getParameters()) {
            session.collectDependencies(substitutor.substitute(parameter.getType()), result);
          }
        }

        final PsiType returnType = interfaceMethod.getReturnType();
        if (returnType != null) {
          collectReturnTypeVariables(session, psiExpression, substitutor.substitute(returnType), result);
        }

        return result;
      }
    }
  }

  if (psiExpression instanceof PsiParenthesizedExpression) {
    final PsiExpression expression = ((PsiParenthesizedExpression)psiExpression).getExpression();
    return expression != null ? createSelfConstraint(type, expression).getInputVariables(session) : null;
  }

  if (psiExpression instanceof PsiConditionalExpression) {
    final PsiExpression thenExpression = ((PsiConditionalExpression)psiExpression).getThenExpression();
    final PsiExpression elseExpression = ((PsiConditionalExpression)psiExpression).getElseExpression();
    final Set<InferenceVariable> thenResult = thenExpression != null ? createSelfConstraint(type, thenExpression).getInputVariables(session) : null;
    final Set<InferenceVariable> elseResult = elseExpression != null ? createSelfConstraint(type, elseExpression).getInputVariables(session) : null;
    if (thenResult == null) {
      return elseResult;
    } else if (elseResult == null) {
      return thenResult;
    } else {
      thenResult.addAll(elseResult);
      return thenResult;
    }
  }
  return null;
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:58,代码来源:InputOutputConstraintFormula.java

示例13: isAcceptable

import com.intellij.psi.util.PsiUtil; //导入方法依赖的package包/类
@Override
public boolean isAcceptable(PsiType leftType) {
  if (leftType instanceof PsiIntersectionType) {
    for (PsiType conjunctType : ((PsiIntersectionType)leftType).getConjuncts()) {
      if (isAcceptable(conjunctType)) return true;
    }
    return false;
  }
  final PsiExpressionList argsList = PsiTreeUtil.getParentOfType(this, PsiExpressionList.class);

  if (MethodCandidateInfo.ourOverloadGuard.currentStack().contains(argsList)) {
    final MethodCandidateInfo.CurrentCandidateProperties candidateProperties = MethodCandidateInfo.getCurrentMethod(argsList);
    if (candidateProperties != null) {
      final PsiMethod method = candidateProperties.getMethod();
      if (hasFormalParameterTypes() && !InferenceSession.isPertinentToApplicability(this, method)) {
        return true;
      }

      if (LambdaUtil.isPotentiallyCompatibleWithTypeParameter(this, argsList, method)) {
        return true;
      }
    }
  }

  leftType = FunctionalInterfaceParameterizationUtil.getGroundTargetType(leftType, this);
  if (!isPotentiallyCompatible(leftType)) {
    return false;
  }

  if (MethodCandidateInfo.ourOverloadGuard.currentStack().contains(argsList) && !hasFormalParameterTypes()) {
    return true;
  }

  final PsiClassType.ClassResolveResult resolveResult = PsiUtil.resolveGenericsClassInType(leftType);
  final PsiMethod interfaceMethod = LambdaUtil.getFunctionalInterfaceMethod(resolveResult);
  if (interfaceMethod == null) return false;

  if (interfaceMethod.hasTypeParameters()) return false;

  final PsiSubstitutor substitutor = LambdaUtil.getSubstitutor(interfaceMethod, resolveResult);

  if (hasFormalParameterTypes()) {
    final PsiParameter[] lambdaParameters = getParameterList().getParameters();
    final PsiType[] parameterTypes = interfaceMethod.getSignature(substitutor).getParameterTypes();
    for (int lambdaParamIdx = 0, length = lambdaParameters.length; lambdaParamIdx < length; lambdaParamIdx++) {
      PsiParameter parameter = lambdaParameters[lambdaParamIdx];
      final PsiTypeElement typeElement = parameter.getTypeElement();
      if (typeElement != null) {
        final PsiType lambdaFormalType = toArray(typeElement.getType());
        final PsiType methodParameterType = toArray(parameterTypes[lambdaParamIdx]);
        if (!lambdaFormalType.equals(methodParameterType)) {
          return false;
        }
      }
    }
  }

  PsiType methodReturnType = interfaceMethod.getReturnType();
  if (methodReturnType != null && methodReturnType != PsiType.VOID) {
    Map<PsiElement, PsiType> map = LambdaUtil.getFunctionalTypeMap();
    try {
      if (map.put(this, leftType) != null) {
        return false;
      }
      return LambdaUtil.checkReturnTypeCompatible(this, substitutor.substitute(methodReturnType)) == null;
    }
    finally {
      map.remove(this);
    }
  }
  return true;
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:73,代码来源:PsiLambdaExpressionImpl.java

示例14: invokeImpl

import com.intellij.psi.util.PsiUtil; //导入方法依赖的package包/类
@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());

  if (!expression.isConstructor() && shouldCreateStaticMember(expression, targetClass)) {
    PsiUtil.setModifierProperty(method, PsiModifier.STATIC, true);
  }

  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, false,
                                    ContainerUtil.map2List(interfaceMethod.getParameterList().getParameters(), new Function<PsiParameter, Pair<PsiExpression, PsiType>>() {
                                      @Override
                                      public Pair<PsiExpression, PsiType> fun(PsiParameter parameter) {
                                        return Pair.create(null, substitutor.substitute(parameter.getType()));
                                      }
                                    }),
                                    PsiSubstitutor.EMPTY,
                                    expectedTypes, context);
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:59,代码来源:CreateMethodFromMethodReferenceFix.java

示例15: introduceWrappedCodeBlockParameter

import com.intellij.psi.util.PsiUtil; //导入方法依赖的package包/类
private void introduceWrappedCodeBlockParameter(PsiMethod methodToIntroduceParameter,
                                                PsiMethod methodToSearchFor, Editor editor,
                                                final Project project,
                                                final PsiType selectedType,
                                                final MyExtractMethodProcessor processor, 
                                                final PsiElement[] elements) {
  final PsiElement commonParent = elements.length > 1 ? PsiTreeUtil.findCommonParent(elements) 
                                                      : PsiTreeUtil.getParentOfType(elements[0].getParent(), PsiCodeBlock.class, false);
  if (commonParent == null) {
    LOG.error("Should have common parent:" + Arrays.toString(elements));
    return;
  }
  final RangeMarker marker = editor.getDocument().createRangeMarker(commonParent.getTextRange());

  final PsiElement[] copyElements = processor.getElements();
  final PsiElement containerCopy = copyElements.length > 1 ? PsiTreeUtil.findCommonParent(copyElements)
                                                           : PsiTreeUtil.getParentOfType(copyElements[0].getParent(), PsiCodeBlock.class, false);
  if (containerCopy == null) {
    LOG.error("Should have common parent:" + Arrays.toString(copyElements));
    return;
  }

  final PsiClassType.ClassResolveResult resolveResult = PsiUtil.resolveGenericsClassInType(selectedType);
  final PsiClass wrapperClass = resolveResult.getElement();
  LOG.assertTrue(wrapperClass != null);

  final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
  final Ref<String> suffixText = new Ref<String>();
  final Ref<String> prefixText = new Ref<String>();
  final Ref<String> methodText = new Ref<String>();
  WriteCommandAction.runWriteCommandAction(project, new Runnable() {
    @Override
    public void run() {
      final PsiMethod method = LambdaUtil.getFunctionalInterfaceMethod(wrapperClass);
      LOG.assertTrue(method != null);
      final String interfaceMethodName = method.getName();
      processor.setMethodName(interfaceMethodName);

      if (copyElements.length == 1) {
        copyElements[0].putUserData(ElementToWorkOn.REPLACE_NON_PHYSICAL, true);
      }

      processor.doExtract();

      final PsiMethod extractedMethod = processor.getExtractedMethod();
      final PsiParameter[] parameters = extractedMethod.getParameterList().getParameters();
      final PsiParameter[] interfaceParameters = method.getParameterList().getParameters();
      final PsiSubstitutor substitutor = resolveResult.getSubstitutor();
      for (int i = 0; i < interfaceParameters.length; i++) {
        final PsiTypeElement typeAfterInterface = factory.createTypeElement(substitutor.substitute(interfaceParameters[i].getType()));
        final PsiTypeElement typeElement = parameters[i].getTypeElement();
        if (typeElement != null) {
          typeElement.replace(typeAfterInterface);
        }
      }
      methodText.set(extractedMethod.getText());

      final PsiMethodCallExpression methodCall = processor.getMethodCall();
      prefixText.set(containerCopy.getText().substring(0, methodCall.getTextRange().getStartOffset() - containerCopy.getTextRange().getStartOffset()));
      suffixText.set("." + methodCall.getText() + containerCopy.getText().substring(methodCall.getTextRange().getEndOffset() - containerCopy.getTextRange().getStartOffset()));
    }
  });


  PsiExpression expression = factory
    .createExpressionFromText("new " + selectedType.getCanonicalText() + "() {" + methodText.get() + "}",
                              elements[0]);
  expression = (PsiExpression)JavaCodeStyleManager.getInstance(project).shortenClassReferences(expression);

  expression.putUserData(ElementToWorkOn.PARENT, commonParent);
  expression.putUserData(ElementToWorkOn.PREFIX, prefixText.get());
  expression.putUserData(ElementToWorkOn.SUFFIX, suffixText.get());
  expression.putUserData(ElementToWorkOn.TEXT_RANGE, marker);

  new Introducer(project, expression, null, editor)
    .introduceParameter(methodToIntroduceParameter, methodToSearchFor);
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:78,代码来源:IntroduceParameterHandler.java


注:本文中的com.intellij.psi.util.PsiUtil.resolveGenericsClassInType方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。