com.intellij.psi.util.TypeConversionUtil Java Examples

The following examples show how to use com.intellij.psi.util.TypeConversionUtil. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HaxePullUpDialog.java    From intellij-haxe with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isMemberEnabled(MemberInfo member) {
  final PsiClass currentSuperClass = getSuperClass();
  if(currentSuperClass == null) return true;
  if (myMemberInfoStorage.getDuplicatedMemberInfos(currentSuperClass).contains(member)) return false;
  if (myMemberInfoStorage.getExtending(currentSuperClass).contains(member.getMember())) return false;
  final boolean isInterface = currentSuperClass.isInterface();
  if (!isInterface) return true;
  PsiElement element = member.getMember();
  if (element instanceof PsiClass && ((PsiClass) element).isInterface()) return true;
  if (element instanceof PsiField) {
    return ((PsiModifierListOwner) element).hasModifierProperty(PsiModifier.STATIC);
  }
  if (element instanceof PsiMethod) {
    final PsiSubstitutor superSubstitutor = TypeConversionUtil
      .getSuperClassSubstitutor(currentSuperClass, myClass, PsiSubstitutor.EMPTY);
    final MethodSignature signature = ((PsiMethod) element).getSignature(superSubstitutor);
    final PsiMethod superClassMethod = MethodSignatureUtil.findMethodBySignature(currentSuperClass, signature, false);
    if (superClassMethod != null && !PsiUtil.isLanguageLevel8OrHigher(currentSuperClass)) return false;
    return !((PsiModifierListOwner) element).hasModifierProperty(PsiModifier.STATIC) || PsiUtil.isLanguageLevel8OrHigher(currentSuperClass);
  }
  return true;
}
 
Example #2
Source File: DelegateHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void collectAllMethods(Collection<Pair<PsiMethod, PsiSubstitutor>> allMethods, @NotNull PsiClass psiStartClass, @NotNull PsiSubstitutor classSubstitutor) {
  PsiClass psiClass = psiStartClass;
  while (null != psiClass) {
    PsiMethod[] psiMethods = psiClass.getMethods();
    for (PsiMethod psiMethod : psiMethods) {
      if (!psiMethod.isConstructor() && psiMethod.hasModifierProperty(PsiModifier.PUBLIC) && !psiMethod.hasModifierProperty(PsiModifier.STATIC)) {

        Pair<PsiMethod, PsiSubstitutor> newMethodSubstitutorPair = new Pair<>(psiMethod, classSubstitutor);

        boolean acceptMethod = true;
        for (Pair<PsiMethod, PsiSubstitutor> uniquePair : allMethods) {
          if (PsiElementUtil.methodMatches(newMethodSubstitutorPair, uniquePair)) {
            acceptMethod = false;
            break;
          }
        }
        if (acceptMethod) {
          allMethods.add(newMethodSubstitutorPair);
        }
      }
    }

    for (PsiClass interfaceClass : psiClass.getInterfaces()) {
      classSubstitutor = TypeConversionUtil.getSuperClassSubstitutor(interfaceClass, psiClass, classSubstitutor);

      collectAllMethods(allMethods, interfaceClass, classSubstitutor);
    }

    psiClass = psiClass.getSuperClass();
  }
}
 
Example #3
Source File: HaxeCallReferenceProcessor.java    From intellij-haxe with Apache License 2.0 4 votes vote down vote up
@Override
public boolean process(@NotNull PsiReference reference, @NotNull JavaCallHierarchyData jchdata) {
  if (!(jchdata instanceof CallData)) {
    String msg = "Internal error, unexpected call data type passed in.";
    LOG.error(msg);
    throw new UnsupportedOperationException(msg);
  }
  CallData data = (CallData)jchdata;

  PsiClass originalClass = data.getOriginalClass();
  PsiMethod method = data.getMethod();
  Set<PsiMethod> methodsToFind = data.getMethodsToFind();
  PsiMethod methodToFind = data.getMethodToFind();
  PsiClassType originalType = data.getOriginalType();
  Map<PsiMember, NodeDescriptor> methodToDescriptorMap = data.getResultMap();
  Project myProject = data.getProject();
  HaxeHierarchyTimeoutHandler timeoutHandler = data.getTimeoutHandler();

  // All done, if we time out.
  if (timeoutHandler.checkAndCancelIfNecessary()) {
    return false;
  }

  if (reference instanceof HaxeReferenceExpression) {
    final PsiElement qualifierElement = ((HaxeReferenceExpression)reference).getQualifier();
    final HaxeReferenceExpression qualifier = (HaxeReferenceExpression) qualifierElement;
    if (qualifier instanceof HaxeSuperExpression) { // filter super.foo() call inside foo() and similar cases (bug 8411)
      final PsiClass superClass = PsiUtil.resolveClassInType(qualifier.getPsiType());
      if (superClass == null || originalClass.isInheritor(superClass, true)) {
        return true;
      }
    }
    if (qualifier != null && !methodToFind.hasModifierProperty(PsiModifier.STATIC)) {
      final PsiType qualifierType = qualifier.getPsiType();
      if (qualifierType instanceof PsiClassType &&
          !TypeConversionUtil.isAssignable(qualifierType, originalType) &&
          methodToFind != method) {
        final PsiClass psiClass = ((PsiClassType)qualifierType).resolve();
        if (psiClass != null) {
          final PsiMethod callee = psiClass.findMethodBySignature(methodToFind, true);
          if (callee != null && !methodsToFind.contains(callee)) {
            // skip sibling methods
            return true;
          }
        }
      }
    }
  }
  else {
    if (!(reference instanceof PsiElement)) {
      return true;
    }

    final PsiElement parent = ((PsiElement)reference).getParent();
    // If the parent is a 'new x' expression, but the reference isn't the primary
    // expression, then keep looking.
    if (parent instanceof HaxeNewExpression) {
      if (((HaxeNewExpression)parent).getType().getReferenceExpression() != reference) {
        return true;
      }
    }
    // If the reference isn't the primary expression of an anonymous class, then keep looking?
    else if (parent instanceof PsiAnonymousClass) {
      // XXX: This appears to be an optimization, knowing that an anonymous class can't be
      //      referenced from outside of itself, there's no need to load the class by
      //      calling for the base reference (the first child of the class).  The haxe
      //      PSI doesn't appear to be built in that fashion any way...
      // if (((PsiAnonymousClass)parent).getBaseClassReference() != reference) {
      //   return true;
      // }

      // let it be processed.
    }
    else {
      return true;
    }
  }

  final PsiElement element = reference.getElement();
  final PsiMember key = CallHierarchyNodeDescriptor.getEnclosingElement(element);

  synchronized (methodToDescriptorMap) {
    CallHierarchyNodeDescriptor d = (CallHierarchyNodeDescriptor)methodToDescriptorMap.get(key);
    if (d == null) {
      d = new CallHierarchyNodeDescriptor(myProject, (CallHierarchyNodeDescriptor)data.getNodeDescriptor(), element, false, true);
      methodToDescriptorMap.put(key, d);
    }
    else if (!d.hasReference(reference)) {
      d.incrementUsageCount();
    }
    d.addReference(reference);
  }
  return false;
}
 
Example #4
Source File: ValProcessor.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private PsiType processLocalVariableInitializer(final PsiExpression psiExpression) {
  PsiType result = null;
  if (null != psiExpression && !(psiExpression instanceof PsiArrayInitializerExpression)) {

    if (psiExpression instanceof PsiConditionalExpression) {
      result = RecursionManager.doPreventingRecursion(psiExpression, true, new Computable<PsiType>() {
        @Override
        public PsiType compute() {
          final PsiExpression thenExpression = ((PsiConditionalExpression) psiExpression).getThenExpression();
          final PsiExpression elseExpression = ((PsiConditionalExpression) psiExpression).getElseExpression();

          final PsiType thenType = null != thenExpression ? thenExpression.getType() : null;
          final PsiType elseType = null != elseExpression ? elseExpression.getType() : null;

          if (thenType == null) {
            return elseType;
          }
          if (elseType == null) {
            return thenType;
          }

          if (TypeConversionUtil.isAssignable(thenType, elseType, false)) {
            return thenType;
          }
          if (TypeConversionUtil.isAssignable(elseType, thenType, false)) {
            return elseType;
          }
          return thenType;
        }
      });
    } else {
      result = RecursionManager.doPreventingRecursion(psiExpression, true, new Computable<PsiType>() {
        @Override
        public PsiType compute() {
          return psiExpression.getType();
        }
      });
    }
  }

  return result;
}
 
Example #5
Source File: InnerBuilderCollector.java    From innerbuilder with Apache License 2.0 4 votes vote down vote up
private static PsiFieldMember buildFieldMember(final PsiField field, final PsiClass containingClass,
        final PsiClass clazz) {
    return new PsiFieldMember(field,
            TypeConversionUtil.getSuperClassSubstitutor(containingClass, clazz, PsiSubstitutor.EMPTY));
}