コード例 #1
0
 @Nullable
 private static PsiClass findClass(PsiNewExpression newExpression) {
   final PsiJavaCodeReferenceElement classReference =
       newExpression.getClassOrAnonymousClassReference();
   if (classReference != null) {
     final String text = classReference.getReferenceName();
     if (text != null) {
       final Project project = newExpression.getProject();
       final JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
       final PsiResolveHelper resolveHelper = facade.getResolveHelper();
       final PsiExpression newExpressionQualifier = newExpression.getQualifier();
       final PsiElement qualifierElement = classReference.getQualifier();
       final String qualifier = qualifierElement != null ? qualifierElement.getText() : "";
       final String qualifiedName = StringUtil.getQualifiedName(qualifier, text);
       if (newExpressionQualifier != null) {
         final PsiClass aClass =
             PsiUtil.resolveClassInClassTypeOnly(newExpressionQualifier.getType());
         if (aClass != null) {
           return aClass.findInnerClassByName(qualifiedName, false);
         }
       }
       return resolveHelper.resolveReferencedClass(qualifiedName, newExpression);
     } else {
       return null;
     }
   }
   return null;
 }
コード例 #2
0
  private static Set<LookupElement> suggestQualifierItems(
      CompletionParameters parameters,
      PsiJavaCodeReferenceElement qualifier,
      ElementFilter filter) {
    String referenceName = qualifier.getReferenceName();
    if (referenceName == null) {
      return Collections.emptySet();
    }

    PrefixMatcher qMatcher = new CamelHumpMatcher(referenceName);
    Set<LookupElement> plainVariants =
        JavaSmartCompletionContributor.completeReference(
            qualifier, qualifier, filter, true, true, parameters, qMatcher);

    for (PsiClass aClass :
        PsiShortNamesCache.getInstance(qualifier.getProject())
            .getClassesByName(referenceName, qualifier.getResolveScope())) {
      plainVariants.add(JavaClassNameCompletionContributor.createClassLookupItem(aClass, true));
    }

    if (!plainVariants.isEmpty()) {
      return plainVariants;
    }

    final Set<LookupElement> allClasses = new LinkedHashSet<LookupElement>();
    PsiElement qualifierName = qualifier.getReferenceNameElement();
    if (qualifierName != null) {
      JavaClassNameCompletionContributor.addAllClasses(
          parameters.withPosition(qualifierName, qualifierName.getTextRange().getEndOffset()),
          true,
          qMatcher,
          new CollectConsumer<LookupElement>(allClasses));
    }
    return allClasses;
  }
コード例 #3
0
 @NotNull
 public static String getPresentableText(@NotNull PsiJavaCodeReferenceElement ref) {
   String name = ref.getReferenceName();
   PsiAnnotation[] annotations = PsiTreeUtil.getChildrenOfType(ref, PsiAnnotation.class);
   return getPresentableText(
       name, notNull(annotations, PsiAnnotation.EMPTY_ARRAY), ref.getTypeParameters());
 }
コード例 #4
0
 @Override
 public void visitNewExpression(PsiNewExpression expression) {
   super.visitNewExpression(expression);
   final PsiJavaCodeReferenceElement classReference = expression.getClassReference();
   if (classReference == null) {
     return;
   }
   final String name = classReference.getReferenceName();
   if (!"BigDecimal".equals(name)) {
     return;
   }
   final PsiMethod constructor = expression.resolveConstructor();
   if (constructor == null) {
     return;
   }
   final PsiParameterList parameterList = constructor.getParameterList();
   final int length = parameterList.getParametersCount();
   if (length != 1 && length != 2) {
     return;
   }
   final PsiParameter[] parameters = parameterList.getParameters();
   final PsiParameter firstParameter = parameters[0];
   final PsiType type = firstParameter.getType();
   if (type != PsiType.DOUBLE) {
     return;
   }
   registerNewExpressionError(expression);
 }
コード例 #5
0
  @Override
  protected void invokeImpl(final PsiClass targetClass) {
    PsiNewExpression newExpression = getNewExpression();
    PsiJavaCodeReferenceElement ref = newExpression.getClassOrAnonymousClassReference();
    assert ref != null;
    String refName = ref.getReferenceName();
    LOG.assertTrue(refName != null);
    PsiElementFactory elementFactory =
        JavaPsiFacade.getInstance(newExpression.getProject()).getElementFactory();
    PsiClass created = elementFactory.createClass(refName);
    final PsiModifierList modifierList = created.getModifierList();
    LOG.assertTrue(modifierList != null);
    if (PsiTreeUtil.isAncestor(targetClass, newExpression, true)) {
      if (targetClass.isInterface()) {
        modifierList.setModifierProperty(PsiModifier.PACKAGE_LOCAL, true);
      } else {
        modifierList.setModifierProperty(PsiModifier.PRIVATE, true);
      }
    }

    if (!PsiTreeUtil.isAncestor(targetClass, newExpression, true)
        || PsiUtil.getEnclosingStaticElement(newExpression, targetClass) != null
        || isInThisOrSuperCall(newExpression)) {
      modifierList.setModifierProperty(PsiModifier.STATIC, true);
    }
    created = (PsiClass) targetClass.add(created);

    setupClassFromNewExpression(created, newExpression);

    setupGenericParameters(created, ref);
  }
コード例 #6
0
  public static DiamondInferenceResult resolveInferredTypesNoCheck(
      final PsiNewExpression newExpression, final PsiElement context) {
    final PsiClass psiClass = findClass(newExpression);
    if (psiClass == null) return DiamondInferenceResult.NULL_RESULT;
    final PsiExpressionList argumentList = newExpression.getArgumentList();
    if (argumentList == null) return DiamondInferenceResult.NULL_RESULT;
    final Ref<PsiMethod> staticFactoryRef = new Ref<PsiMethod>();
    final PsiSubstitutor inferredSubstitutor =
        ourDiamondGuard.doPreventingRecursion(
            context,
            false,
            new Computable<PsiSubstitutor>() {
              @Override
              public PsiSubstitutor compute() {
                final PsiMethod constructor = findConstructor(psiClass, newExpression);
                PsiTypeParameter[] params = getAllTypeParams(constructor, psiClass);

                final PsiMethod staticFactory =
                    generateStaticFactory(
                        constructor, psiClass, params, newExpression.getClassReference());
                if (staticFactory == null) {
                  return null;
                }
                staticFactoryRef.set(staticFactory);

                return inferTypeParametersForStaticFactory(
                    staticFactory, newExpression, context, false);
              }
            });
    if (inferredSubstitutor == null) {
      return DiamondInferenceResult.NULL_RESULT;
    }
    final PsiMethod staticFactory = staticFactoryRef.get();
    if (staticFactory == null) {
      LOG.error(inferredSubstitutor);
      return DiamondInferenceResult.NULL_RESULT;
    }
    final PsiTypeParameter[] parameters = staticFactory.getTypeParameters();
    final PsiTypeParameter[] classParameters = psiClass.getTypeParameters();
    final PsiJavaCodeReferenceElement classOrAnonymousClassReference =
        newExpression.getClassOrAnonymousClassReference();
    LOG.assertTrue(classOrAnonymousClassReference != null);
    final DiamondInferenceResult result =
        new DiamondInferenceResult(classOrAnonymousClassReference.getReferenceName() + "<>");
    for (PsiTypeParameter parameter : parameters) {
      for (PsiTypeParameter classParameter : classParameters) {
        if (Comparing.strEqual(classParameter.getName(), parameter.getName())) {
          result.addInferredType(inferredSubstitutor.substitute(parameter));
          break;
        }
      }
    }
    return result;
  }
コード例 #7
0
  public static PsiAnnotation findAnnotation(
      @NotNull PsiAnnotationOwner modifierList, @NotNull String qualifiedName) {
    final String shortName = StringUtil.getShortName(qualifiedName);
    PsiAnnotation[] annotations = modifierList.getAnnotations();
    for (PsiAnnotation annotation : annotations) {
      final PsiJavaCodeReferenceElement referenceElement = annotation.getNameReferenceElement();
      if (referenceElement != null && shortName.equals(referenceElement.getReferenceName())) {
        if (qualifiedName.equals(annotation.getQualifiedName())) return annotation;
      }
    }

    return null;
  }
コード例 #8
0
  @Nullable
  public static PsiAnnotation findAnnotation(
      @Nullable PsiAnnotationOwner annotationOwner, @NotNull String qualifiedName) {
    if (annotationOwner == null) return null;

    PsiAnnotation[] annotations = annotationOwner.getAnnotations();
    if (annotations.length == 0) return null;

    String shortName = StringUtil.getShortName(qualifiedName);
    for (PsiAnnotation annotation : annotations) {
      PsiJavaCodeReferenceElement referenceElement = annotation.getNameReferenceElement();
      if (referenceElement != null && shortName.equals(referenceElement.getReferenceName())) {
        if (qualifiedName.equals(annotation.getQualifiedName())) {
          return annotation;
        }
      }
    }

    return null;
  }
コード例 #9
0
 @NotNull
 @Override
 protected List<PsiField> getMembersToImport(boolean applicableOnly) {
   final Project project = myRef.getProject();
   PsiShortNamesCache cache = PsiShortNamesCache.getInstance(project);
   final PsiJavaCodeReferenceElement element = myRef.getElement();
   String name = element != null ? element.getReferenceName() : null;
   if (name == null) return Collections.emptyList();
   final StaticMembersProcessor<PsiField> processor =
       new StaticMembersProcessor<PsiField>(element) {
         @Override
         protected boolean isApplicable(PsiField field, PsiElement place) {
           final PsiType expectedType = getExpectedType();
           return expectedType == null
               || TypeConversionUtil.isAssignable(expectedType, field.getType());
         }
       };
   cache.processFieldsWithName(name, processor, element.getResolveScope(), null);
   return processor.getMembersToImport(applicableOnly);
 }
コード例 #10
0
ファイル: ImportHelper.java プロジェクト: jexp/idea2
  private static void addNamesToImport(
      @NotNull Set<String> names,
      @NotNull PsiElement scope,
      @NotNull String thisPackageName,
      @NotNull Set<String> namesToImportStaticly,
      PsiFile context) {
    if (scope instanceof PsiImportList) return;

    final LinkedList<PsiElement> stack = new LinkedList<PsiElement>();
    stack.add(scope);
    while (!stack.isEmpty()) {
      final PsiElement child = stack.removeFirst();
      if (child instanceof PsiImportList) continue;
      stack.addAll(Arrays.asList(child.getChildren()));

      for (final PsiReference reference : child.getReferences()) {
        if (!(reference instanceof PsiJavaReference)) continue;
        final PsiJavaReference javaReference = (PsiJavaReference) reference;
        if (javaReference instanceof JavaClassReference) {
          if (((JavaClassReference) javaReference).getContextReference() != null) continue;
        }
        PsiJavaCodeReferenceElement referenceElement = null;
        if (reference instanceof PsiJavaCodeReferenceElement) {
          referenceElement = (PsiJavaCodeReferenceElement) child;
          if (referenceElement.getQualifier() != null) {
            continue;
          }
          if (reference instanceof PsiJavaCodeReferenceElementImpl
              && ((PsiJavaCodeReferenceElementImpl) reference).getKind()
                  == PsiJavaCodeReferenceElementImpl.CLASS_IN_QUALIFIED_NEW_KIND) {
            continue;
          }
        }

        final JavaResolveResult resolveResult = javaReference.advancedResolve(true);
        PsiElement refElement = resolveResult.getElement();
        if (refElement == null && referenceElement != null) {
          refElement = ResolveClassUtil.resolveClass(referenceElement); // might be uncomplete code
        }

        PsiElement currentFileResolveScope = resolveResult.getCurrentFileResolveScope();
        if (!(currentFileResolveScope instanceof PsiImportStatementBase)) continue;
        if (context != null
            && currentFileResolveScope instanceof JspxImportStatement
            && context != ((JspxImportStatement) currentFileResolveScope).getDeclarationFile()) {
          continue;
        }

        if (refElement != null) {
          // Add names imported statically
          if (referenceElement != null) {
            if (currentFileResolveScope instanceof PsiImportStaticStatement) {
              PsiImportStaticStatement importStaticStatement =
                  (PsiImportStaticStatement) currentFileResolveScope;
              String name = importStaticStatement.getImportReference().getCanonicalText();
              if (importStaticStatement.isOnDemand()) {
                String refName = referenceElement.getReferenceName();
                if (refName != null) name = name + "." + refName;
              }
              names.add(name);
              namesToImportStaticly.add(name);
              continue;
            }
          }

          if (refElement instanceof PsiClass) {
            String qName = ((PsiClass) refElement).getQualifiedName();
            if (hasPackage(qName, thisPackageName)) continue;
            names.add(qName);
          }
        }
      }
    }
  }