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;
  }
 @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);
 }
  @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);
  }
Ejemplo n.º 4
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;
  }
Ejemplo n.º 5
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;
  }
 @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);
 }
Ejemplo n.º 7
0
  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);
          }
        }
      }
    }
  }