@Nullable public static PsiReferenceExpression createMockReference( final PsiElement place, @NotNull PsiType qualifierType, LookupElement qualifierItem) { PsiElementFactory factory = JavaPsiFacade.getElementFactory(place.getProject()); if (qualifierItem.getObject() instanceof PsiClass) { final String qname = ((PsiClass) qualifierItem.getObject()).getQualifiedName(); if (qname == null) return null; final String text = qname + ".xxx"; try { final PsiExpression expr = factory.createExpressionFromText(text, place); if (expr instanceof PsiReferenceExpression) { return (PsiReferenceExpression) expr; } return null; // ignore ill-formed qualified names like "org.spark-project.jetty" that can't // be used from Java code anyway } catch (IncorrectOperationException e) { LOG.info(e); return null; } } return (PsiReferenceExpression) factory.createExpressionFromText( "xxx.xxx", JavaCompletionUtil.createContextWithXxxVariable(place, qualifierType)); }
private static boolean superConstructorHasParameters(PsiMethod method) { final PsiClass psiClass = method.getContainingClass(); if (psiClass == null) { return false; } final PsiClass superClass = psiClass.getSuperClass(); if (superClass != null) { for (final PsiMethod psiMethod : superClass.getConstructors()) { final PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(method.getProject()).getResolveHelper(); if (resolveHelper.isAccessible(psiMethod, method, null) && psiMethod.getParameterList().getParameters().length > 0) { return true; } } } return false; }
private static void addPrimitiveTypes(final Consumer<LookupElement> result, PsiElement position) { if (AFTER_DOT.accepts(position)) { return; } boolean afterNew = psiElement() .afterLeaf( psiElement() .withText(PsiKeyword.NEW) .andNot(psiElement().afterLeaf(PsiKeyword.THROW, "."))) .accepts(position); if (afterNew) { PsiElementFactory factory = JavaPsiFacade.getElementFactory(position.getProject()); for (String primitiveType : PRIMITIVE_TYPES) { result.consume( PsiTypeLookupItem.createLookupItem( factory.createTypeFromText(primitiveType + "[]", null), null)); } result.consume( PsiTypeLookupItem.createLookupItem(factory.createTypeFromText("void[]", null), null)); return; } boolean inCast = psiElement() .afterLeaf( psiElement() .withText("(") .withParent( psiElement(PsiParenthesizedExpression.class, PsiTypeCastExpression.class))) .accepts(position); boolean typeFragment = position.getContainingFile() instanceof PsiTypeCodeFragment && PsiTreeUtil.prevVisibleLeaf(position) == null; boolean declaration = DECLARATION_START.getValue().accepts(position); boolean expressionPosition = isExpressionPosition(position); boolean inGenerics = PsiTreeUtil.getParentOfType(position, PsiReferenceParameterList.class) != null; if (START_FOR.accepts(position) || isInsideParameterList(position) || inGenerics || VARIABLE_AFTER_FINAL.accepts(position) || inCast || declaration || typeFragment || expressionPosition || isStatementPosition(position)) { for (String primitiveType : PRIMITIVE_TYPES) { result.consume(createKeyword(position, primitiveType)); } } if (declaration) { result.consume( new OverrideableSpace( createKeyword(position, PsiKeyword.VOID), TailType.HUMBLE_SPACE_BEFORE_WORD)); } else if (typeFragment && ((PsiTypeCodeFragment) position.getContainingFile()).isVoidValid()) { result.consume(createKeyword(position, PsiKeyword.VOID)); } }
private static void completeAnnotationAttributeName( CompletionResultSet result, PsiElement insertedElement, CompletionParameters parameters) { PsiNameValuePair pair = PsiTreeUtil.getParentOfType(insertedElement, PsiNameValuePair.class); PsiAnnotationParameterList parameterList = (PsiAnnotationParameterList) pair.getParent(); PsiAnnotation anno = (PsiAnnotation) parameterList.getParent(); boolean showClasses = psiElement().afterLeaf("(").accepts(insertedElement); PsiClass annoClass = null; final PsiJavaCodeReferenceElement referenceElement = anno.getNameReferenceElement(); if (referenceElement != null) { final PsiElement element = referenceElement.resolve(); if (element instanceof PsiClass) { annoClass = (PsiClass) element; if (annoClass.findMethodsByName("value", false).length == 0) { showClasses = false; } } } if (showClasses && insertedElement.getParent() instanceof PsiReferenceExpression) { final Set<LookupElement> set = JavaCompletionUtil.processJavaReference( insertedElement, (PsiJavaReference) insertedElement.getParent(), new ElementExtractorFilter(createAnnotationFilter(insertedElement)), JavaCompletionProcessor.Options.DEFAULT_OPTIONS, result.getPrefixMatcher(), parameters); for (final LookupElement element : set) { result.addElement(element); } addAllClasses(parameters, result, new InheritorsHolder(insertedElement, result)); } if (annoClass != null) { final PsiNameValuePair[] existingPairs = parameterList.getAttributes(); methods: for (PsiMethod method : annoClass.getMethods()) { if (!(method instanceof PsiAnnotationMethod)) continue; final String attrName = method.getName(); for (PsiNameValuePair existingAttr : existingPairs) { if (PsiTreeUtil.isAncestor(existingAttr, insertedElement, false)) break; if (Comparing.equal(existingAttr.getName(), attrName) || PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME.equals(attrName) && existingAttr.getName() == null) continue methods; } LookupElementBuilder element = LookupElementBuilder.createWithIcon(method) .withInsertHandler( new InsertHandler<LookupElement>() { @Override public void handleInsert(InsertionContext context, LookupElement item) { final Editor editor = context.getEditor(); TailType.EQ.processTail(editor, editor.getCaretModel().getOffset()); context.setAddCompletionChar(false); context.commitDocument(); PsiAnnotationParameterList paramList = PsiTreeUtil.findElementOfClassAtOffset( context.getFile(), context.getStartOffset(), PsiAnnotationParameterList.class, false); if (paramList != null && paramList.getAttributes().length > 0 && paramList.getAttributes()[0].getName() == null) { int valueOffset = paramList.getAttributes()[0].getTextRange().getStartOffset(); context .getDocument() .insertString( valueOffset, PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME); TailType.EQ.processTail( editor, valueOffset + PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME.length()); } } }); PsiAnnotationMemberValue defaultValue = ((PsiAnnotationMethod) method).getDefaultValue(); if (defaultValue != null) { Object constant = JavaPsiFacade.getInstance(method.getProject()) .getConstantEvaluationHelper() .computeConstantExpression(defaultValue); if (constant != null) { element = element.withTailText( " default " + (constant instanceof String ? "\"" + constant + "\"" : constant), true); } } result.addElement(element); } } }
static PsiExpression createExpression(String text, PsiElement element) { return JavaPsiFacade.getInstance(element.getProject()) .getElementFactory() .createExpressionFromText(text, element); }