@NotNull public TypeInfo applyAnnotations(@NotNull StubBase<?> owner) { PsiModifierListStub modifierList = (PsiModifierListStub)owner.findChildStubByType(JavaStubElementTypes.MODIFIER_LIST); if (modifierList == null) return this; List<PsiAnnotationStub> annotationStubs = null; for (StubElement child : modifierList.getChildrenStubs()) { if (!(child instanceof PsiAnnotationStub)) continue; PsiAnnotationStub annotationStub = (PsiAnnotationStub)child; if (PsiImplUtil.isTypeAnnotation(annotationStub.getPsiElement())) { if (annotationStubs == null) annotationStubs = new SmartList<PsiAnnotationStub>(); annotationStubs.add(annotationStub); } } PsiAnnotationStub[] stubArray = PsiAnnotationStub.EMPTY_ARRAY; if (annotationStubs != null) stubArray = annotationStubs.toArray(new PsiAnnotationStub[annotationStubs.size()]); return new TypeInfo(text, arrayCount, isEllipsis, stubArray); }
@Override public void deleteChildInternal(@NotNull ASTNode child) { if (child.getElementType() == JAVA_CODE_REFERENCE){ ASTNode next = PsiImplUtil.skipWhitespaceAndComments(child.getTreeNext()); if (next != null && next.getElementType() == AND){ deleteChildInternal(next); } else{ ASTNode prev = PsiImplUtil.skipWhitespaceAndCommentsBack(child.getTreePrev()); if (prev != null){ if (prev.getElementType() == AND || prev.getElementType() == EXTENDS_KEYWORD){ deleteChildInternal(prev); } } } } super.deleteChildInternal(child); }
@Override public ASTNode findChildByRole(int role){ LOG.assertTrue(ChildRole.isUnique(role)); switch(role){ default: return null; case ChildRole.WHILE_KEYWORD: return findChildByType(WHILE_KEYWORD); case ChildRole.LPARENTH: return findChildByType(LPARENTH); case ChildRole.CONDITION: return findChildByType(EXPRESSION_BIT_SET); case ChildRole.RPARENTH: return findChildByType(RPARENTH); case ChildRole.LOOP_BODY: return PsiImplUtil.findStatementChild(this); } }
@Nullable public ASTNode findEnumConstantListDelimiterPlace() { final ASTNode first = findChildByRole(ChildRole.LBRACE); if (first == null) return null; for (ASTNode child = first.getTreeNext(); child != null; child = child.getTreeNext()) { final IElementType childType = child.getElementType(); if (PsiImplUtil.isWhitespaceOrComment(child) || childType == ERROR_ELEMENT || childType == ENUM_CONSTANT) { } else if (childType == COMMA) { } else if (childType == SEMICOLON) { return child; } else { return PsiImplUtil.skipWhitespaceAndCommentsBack(child.getTreePrev()); } } return null; }
@Nullable private static PsiElement getNext(@NotNull PsiElement current, PsiElement elementUnderLabel, PsiElement elementAfterLabel) { PsiElement e = current; do { if (e == elementUnderLabel) { e = elementAfterLabel; } else { e = e.getNextSibling(); } } while (PsiImplUtil.isLeafElementOfType(e, TokenSets.WHITE_SPACES_OR_COMMENTS)); if (e instanceof GrLabeledStatement) return null; return e; }
@Override public ASTNode findChildByRole(int role){ LOG.assertTrue(ChildRole.isUnique(role)); switch(role){ default: return null; case ChildRole.DOC_COMMENT: return PsiImplUtil.findDocComment(this); case ChildRole.NAME: return findChildByType(JavaTokenType.IDENTIFIER); case ChildRole.ARGUMENT_LIST: return findChildByType(EXPRESSION_LIST); case ChildRole.ANONYMOUS_CLASS: return findChildByType(ENUM_CONSTANT_INITIALIZER); case ChildRole.MODIFIER_LIST: return findChildByType(JavaElementType.MODIFIER_LIST); } }
@Override public void deleteChildInternal(@NotNull ASTNode child) { if (child.getElementType() == JavaElementType.JAVA_CODE_REFERENCE){ ASTNode next = PsiImplUtil.skipWhitespaceAndComments(child.getTreeNext()); if (next != null && next.getElementType() == JavaTokenType.COMMA){ deleteChildInternal(next); } else{ ASTNode prev = PsiImplUtil.skipWhitespaceAndCommentsBack(child.getTreePrev()); if (prev != null){ if (prev.getElementType() == JavaTokenType.COMMA || prev.getElementType() == getKeywordType() ){ deleteChildInternal(prev); } } } } super.deleteChildInternal(child); }
@Override public ASTNode findChildByRole(int role) { LOG.assertTrue(ChildRole.isUnique(role)); switch(role){ default: return null; case ChildRole.STATEMENT: return PsiImplUtil.findStatementChild(this); case ChildRole.COLON: return findChildByType(COLON); case ChildRole.LABEL_NAME: return getFirstChildNode(); } }
private void addTypeUseAnnotations(List<PsiAnnotation> list) { PsiElement parent = this; while (parent instanceof PsiTypeElement) { PsiElement left = PsiTreeUtil.skipSiblingsBackward(parent, PsiComment.class, PsiWhiteSpace.class, PsiAnnotation.class); if (left instanceof PsiModifierList) { List<PsiAnnotation> annotations = PsiImplUtil.getTypeUseAnnotations((PsiModifierList)left); if (annotations != null && !annotations.isEmpty()) { list.addAll(annotations); } break; } if (left != null) break; parent = parent.getParent(); } }
@Override public boolean execute(@NotNull PsiElement element, @NotNull ResolveState state) { PsiMember member = (PsiMember)element; if (!isInteresting(element)) return true; if (myPsiClass.isInterface() && isObjectMember(element)) return true; if (!myAllMembers.contains(member) && isVisible(member, myPsiClass)) { if (member instanceof PsiMethod) { PsiMethod psiMethod = (PsiMethod)member; if (shouldAdd(psiMethod)) { mapMethodBySignature(psiMethod); myAllMembers.add(PsiImplUtil.handleMirror(psiMethod)); } } else { myAllMembers.add(PsiImplUtil.handleMirror(member)); } } return true; }
@Nullable public static RetentionPolicy getRetentionPolicy(@NotNull PsiClass annotation) { PsiModifierList modifierList = annotation.getModifierList(); if (modifierList != null) { PsiAnnotation retentionAnno = modifierList.findAnnotation(CommonClassNames.JAVA_LANG_ANNOTATION_RETENTION); if (retentionAnno == null) return RetentionPolicy.CLASS; PsiAnnotationMemberValue policyRef = PsiImplUtil.findAttributeValue(retentionAnno, null); if (policyRef instanceof PsiReference) { PsiElement field = ((PsiReference)policyRef).resolve(); if (field instanceof PsiEnumConstant) { String name = ((PsiEnumConstant)field).getName(); try { return Enum.valueOf(RetentionPolicy.class, name); } catch (Exception e) { LOG.warn("Unknown policy: " + name); } } } } return null; }
private static boolean isAnnotated(ASTNode element) { PsiJavaCodeReferenceElement ref = (PsiJavaCodeReferenceElement)element.getPsi(); PsiElement qualifier = ref.getQualifier(); if (qualifier instanceof PsiJavaCodeReferenceElement) { if (((PsiJavaCodeReferenceElement)qualifier).resolve() instanceof PsiPackage) { return false; } if (PsiTreeUtil.getChildOfType(qualifier, PsiAnnotation.class) != null) { return true; } } PsiModifierList modifierList = PsiImplUtil.findNeighbourModifierList(ref); if (modifierList != null) { for (PsiAnnotation annotation : modifierList.getAnnotations()) { if (PsiImplUtil.findApplicableTarget(annotation, PsiAnnotation.TargetType.TYPE_USE) != null) { return true; } } } return false; }
@Override @Nullable public PsiAnnotationMemberValue findDeclaredAttributeValue(@NonNls final String attributeName) { final GrAnnotationStub stub = getStub(); if (stub != null) { final GrAnnotation stubbedPsi = stub.getPsiElement(); final PsiAnnotationMemberValue value = PsiImplUtil.findDeclaredAttributeValue(stubbedPsi, attributeName); if (value == null) { return null; } } return PsiImplUtil.findDeclaredAttributeValue(this, attributeName); }
@Override public boolean isDeprecated() { final GrFieldStub stub = getStub(); if (stub != null) { return stub.isDeprecatedByDocTag() || PsiImplUtil.isDeprecatedByAnnotation(this); } return PsiImplUtil.isDeprecatedByDocTag(this) || PsiImplUtil.isDeprecatedByAnnotation(this); }
public void registerReturnTypeConstraints(PsiType returnType, PsiType targetType) { returnType = substituteWithInferenceVariables(returnType); final InferenceVariable inferenceVariable = shouldResolveAndInstantiate(returnType, targetType); if (inferenceVariable != null) { final PsiSubstitutor substitutor = resolveSubset(Collections.singletonList(inferenceVariable), mySiteSubstitutor); final PsiType substitutedReturnType = substitutor.substitute(inferenceVariable.getParameter()); if (substitutedReturnType != null) { addConstraint(new TypeCompatibilityConstraint(targetType, PsiImplUtil.normalizeWildcardTypeByPosition(substitutedReturnType, (PsiExpression)myContext))); } } else { if (FunctionalInterfaceParameterizationUtil.isWildcardParameterized(returnType)) { final PsiClassType.ClassResolveResult resolveResult = PsiUtil.resolveGenericsClassInType(returnType); final PsiClass psiClass = resolveResult.getElement(); if (psiClass != null) { LOG.assertTrue(returnType instanceof PsiClassType); final PsiTypeParameter[] typeParameters = psiClass.getTypeParameters(); InferenceVariable[] copy = initBounds(null, typeParameters); final PsiType substitutedCapture = PsiImplUtil.normalizeWildcardTypeByPosition(returnType, (PsiExpression)myContext); myIncorporationPhase.addCapture(copy, (PsiClassType)substituteWithInferenceVariables(returnType)); addConstraint(new TypeCompatibilityConstraint(targetType, substitutedCapture)); } } else { addConstraint(new TypeCompatibilityConstraint(targetType, myErased ? TypeConversionUtil.erasure(returnType) : returnType)); } } }
@Override public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException{ if(element instanceof PsiLabeledStatement){ myIdentifier = (PsiIdentifier) PsiImplUtil.setName(myIdentifier, ((PsiLabeledStatement)element).getName()); return myIdentifier; } throw new IncorrectOperationException("Can't bind not to labeled statement"); }
private List<PsiAnnotation> getAnnotations() { List<PsiAnnotation> annotations = PsiTreeUtil.getChildrenOfTypeAsList(this, PsiAnnotation.class); if (!isQualified()) { PsiModifierList modifierList = PsiImplUtil.findNeighbourModifierList(this); if (modifierList != null) { List<PsiAnnotation> typeAnnotations = PsiImplUtil.getTypeUseAnnotations(modifierList); if (typeAnnotations != null && !typeAnnotations.isEmpty()) { annotations.addAll(typeAnnotations); } } } return annotations; }
@Override @NotNull public Object[] getVariants() { final ElementFilter filter; switch (getKind(getContainingFile())) { case CLASS_OR_PACKAGE_NAME_KIND: filter = new OrFilter(); ((OrFilter)filter).addFilter(ElementClassFilter.CLASS); ((OrFilter)filter).addFilter(ElementClassFilter.PACKAGE_FILTER); break; case CLASS_NAME_KIND: filter = ElementClassFilter.CLASS; break; case PACKAGE_NAME_KIND: filter = ElementClassFilter.PACKAGE_FILTER; break; case CLASS_FQ_NAME_KIND: case CLASS_FQ_OR_PACKAGE_NAME_KIND: filter = new OrFilter(); ((OrFilter)filter).addFilter(ElementClassFilter.PACKAGE_FILTER); if (isQualified()) { ((OrFilter)filter).addFilter(ElementClassFilter.CLASS); } break; case CLASS_IN_QUALIFIED_NEW_KIND: filter = ElementClassFilter.CLASS; break; default: throw new RuntimeException("Unknown reference type"); } return PsiImplUtil.getReferenceVariantsByFilter(this, filter); }
@Override public List<PsiElement> unwrap(Editor editor, PsiElement element) throws IncorrectOperationException { List<PsiElement> res = super.unwrap(editor, element); for (PsiElement e : res) { if (PsiImplUtil.isLeafElementOfType(e, GroovyTokenTypes.mNLS)) { CodeEditUtil.setNodeGenerated(e.getNode(), true); } } return res; }
@Override public ASTNode findChildByRole(int role){ LOG.assertTrue(ChildRole.isUnique(role)); switch(role){ default: return null; case ChildRole.DO_KEYWORD: return findChildByType(DO_KEYWORD); case ChildRole.LOOP_BODY: return PsiImplUtil.findStatementChild(this); case ChildRole.WHILE_KEYWORD: return findChildByType(WHILE_KEYWORD); case ChildRole.LPARENTH: return findChildByType(LPARENTH); case ChildRole.CONDITION: return findChildByType(EXPRESSION_BIT_SET); case ChildRole.RPARENTH: return findChildByType(RPARENTH); case ChildRole.CLOSING_SEMICOLON: return TreeUtil.findChildBackward(this, SEMICOLON); } }
@Override public ASTNode findChildByRole(int role) { LOG.assertTrue(ChildRole.isUnique(role)); switch (role) { default: return null; case ChildRole.DOC_COMMENT: return PsiImplUtil.findDocComment(this); case ChildRole.MODIFIER_LIST: return findChildByType(MODIFIER_LIST); case ChildRole.TYPE_PARAMETER_LIST: return findChildByType(TYPE_PARAMETER_LIST); case ChildRole.NAME: return findChildByType(IDENTIFIER); case ChildRole.TYPE: return findChildByType(TYPE); case ChildRole.METHOD_BODY: return findChildByType(CODE_BLOCK); case ChildRole.PARAMETER_LIST: return findChildByType(PARAMETER_LIST); case ChildRole.THROWS_LIST: return findChildByType(THROWS_LIST); case ChildRole.CLOSING_SEMICOLON: return TreeUtil.findChildBackward(this, SEMICOLON); case ChildRole.DEFAULT_KEYWORD: return findChildByType(DEFAULT_KEYWORD); } }
@Override public ASTNode findChildByRole(int role){ assert (ChildRole.isUnique(role)); switch(role){ default: return null; case ChildRole.DOC_COMMENT: return PsiImplUtil.findDocComment(this); case ChildRole.MODIFIER_LIST: return findChildByType(JavaElementType.MODIFIER_LIST); case ChildRole.TYPE: return findChildByType(JavaElementType.TYPE); case ChildRole.NAME: return findChildByType(JavaTokenType.IDENTIFIER); case ChildRole.INITIALIZER_EQ: return findChildByType(JavaTokenType.EQ); case ChildRole.INITIALIZER: return findChildByType(ElementType.EXPRESSION_BIT_SET); case ChildRole.CLOSING_SEMICOLON: return TreeUtil.findChildBackward(this, JavaTokenType.SEMICOLON); } }
@Override public boolean processDeclarations(@NotNull PsiScopeProcessor processor, @NotNull ResolveState state, PsiElement lastParent, @NotNull PsiElement place) { return PsiImplUtil.processDeclarationsInResourceList(this, processor, state, lastParent); }
@Override @NotNull public SearchScope getUseScope() { if (isProperty()) { return ResolveScopeManager.getElementUseScope(this); //maximal scope } return PsiImplUtil.getMemberUseScope(this); }
@Override public ASTNode findChildByRole(int role) { LOG.assertTrue(ChildRole.isUnique(role)); switch(role) { case ChildRole.LOOP_BODY: return PsiImplUtil.findStatementChild(this); case ChildRole.FOR_ITERATED_VALUE: return findChildByType(EXPRESSION_BIT_SET); case ChildRole.FOR_KEYWORD: return getFirstChildNode(); case ChildRole.LPARENTH: return findChildByType(LPARENTH); case ChildRole.RPARENTH: return findChildByType(RPARENTH); case ChildRole.FOR_ITERATION_PARAMETER: return findChildByType(PARAMETER); case ChildRole.COLON: return findChildByType(COLON); default: return null; } }
public static byte buildFlags(GrMethod method) { byte f = 0; if (PsiImplUtil.isDeprecatedByDocTag(method)) { f|= IS_DEPRECATED_BY_DOC_TAG; } return f; }
@Override public boolean processDeclarations(@NotNull final PsiScopeProcessor processor, @NotNull final ResolveState state, final PsiElement lastParent, @NotNull final PsiElement place) { return PsiImplUtil.processDeclarationsInLambda(this, processor, state, lastParent, place); }
@Override public PsiElement replace(@NotNull PsiElement newElement) throws IncorrectOperationException { // neighbouring type annotations are logical part of this type element and should be dropped PsiImplUtil.markTypeAnnotations(this); PsiElement result = super.replace(newElement); PsiImplUtil.deleteTypeAnnotations((PsiTypeElement)result); return result; }
@Override @NotNull public PsiAnnotation[] getApplicableAnnotations() { final PsiAnnotation.TargetType[] targets = PsiImplUtil.getTargetsForLocation(this); List<PsiAnnotation> filtered = ContainerUtil.findAll(getAnnotations(), new Condition<PsiAnnotation>() { @Override public boolean value(PsiAnnotation annotation) { PsiAnnotation.TargetType target = PsiImplUtil.findApplicableTarget(annotation, targets); return target != null && target != PsiAnnotation.TargetType.UNKNOWN; } }); return filtered.toArray(new PsiAnnotation[filtered.size()]); }
@Override @Nullable public PsiAnnotationMemberValue findAttributeValue(@Nullable String attributeName) { final GrAnnotationStub stub = getStub(); if (stub != null) { final GrAnnotation stubbedPsi = stub.getPsiElement(); final PsiAnnotationMemberValue value = PsiImplUtil.findAttributeValue(stubbedPsi, attributeName); if (value == null || !PsiTreeUtil.isAncestor(stubbedPsi, value, true)) { // if value is a default value we can use it return value; } } return PsiImplUtil.findAttributeValue(this, attributeName); }
@Override public boolean isDeprecated() { final PsiMethodStub stub = getStub(); if (stub != null) { return stub.isDeprecated() || stub.hasDeprecatedAnnotation() && PsiImplUtil.isDeprecatedByAnnotation(this); } return PsiImplUtil.isDeprecatedByDocTag(this) || PsiImplUtil.isDeprecatedByAnnotation(this); }
@Override public boolean isVarArgs() { final PsiMethodStub stub = getStub(); if (stub != null) { return stub.isVarArgs(); } return PsiImplUtil.isVarArgs(this); }
@NotNull public PsiMethod[] getConstructors() { return CachedValuesManager.getCachedValue(myClass, new CachedValueProvider<PsiMethod[]>() { @Nullable @Override public Result<PsiMethod[]> compute() { return Result.create(PsiImplUtil.getConstructors(myClass), OUT_OF_CODE_BLOCK_MODIFICATION_COUNT, myTracker); } }); }
@Nullable public static String getNameByReference(@Nullable PsiElement expression) { if (!(expression instanceof GrReferenceExpression)) return null; PsiElement firstChild = expression.getFirstChild(); if (firstChild != expression.getLastChild() || !PsiImplUtil.isLeafElementOfType(firstChild, GroovyTokenTypes.mIDENT)) return null; GrReferenceExpression ref = (GrReferenceExpression)expression; if (ref.isQualified()) return null; return ref.getReferenceName(); }
private static void addPhysicalElements(PsiElement[] elements, LinkedHashSet<PsiElement> to) { for (PsiElement element : elements) { PsiElement mirror = PsiImplUtil.handleMirror(element); if (!(mirror instanceof LightElement)) { to.add(mirror); } } }
@Nullable static HighlightInfo checkRepeatableAnnotation(PsiAnnotation annotation) { String qualifiedName = annotation.getQualifiedName(); if (!CommonClassNames.JAVA_LANG_ANNOTATION_REPEATABLE.equals(qualifiedName)) return null; String description = doCheckRepeatableAnnotation(annotation); if (description != null) { PsiAnnotationMemberValue containerRef = PsiImplUtil.findAttributeValue(annotation, null); if (containerRef != null) { return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(containerRef).descriptionAndTooltip(description).create(); } } return null; }
@Nullable private static String doCheckRepeatableAnnotation(@NotNull PsiAnnotation annotation) { PsiAnnotationOwner owner = annotation.getOwner(); if (!(owner instanceof PsiModifierList)) return null; PsiElement target = ((PsiModifierList)owner).getParent(); if (!(target instanceof PsiClass) || !((PsiClass)target).isAnnotationType()) return null; PsiClass container = getRepeatableContainer(annotation); if (container == null) return null; PsiMethod[] methods = container.findMethodsByName("value", false); if (methods.length == 0) { return JavaErrorMessages.message("annotation.container.no.value", container.getQualifiedName()); } if (methods.length == 1) { PsiType expected = new PsiImmediateClassType((PsiClass)target, PsiSubstitutor.EMPTY).createArrayType(); if (!expected.equals(methods[0].getReturnType())) { return JavaErrorMessages.message("annotation.container.bad.type", container.getQualifiedName(), JavaHighlightUtil.formatType(expected)); } } RetentionPolicy targetPolicy = getRetentionPolicy((PsiClass)target); if (targetPolicy != null) { RetentionPolicy containerPolicy = getRetentionPolicy(container); if (containerPolicy != null && targetPolicy.compareTo(containerPolicy) > 0) { return JavaErrorMessages.message("annotation.container.low.retention", container.getQualifiedName(), containerPolicy); } } Set<PsiAnnotation.TargetType> repeatableTargets = PsiImplUtil.getAnnotationTargets((PsiClass)target); if (repeatableTargets != null) { Set<PsiAnnotation.TargetType> containerTargets = PsiImplUtil.getAnnotationTargets(container); if (containerTargets != null && !repeatableTargets.containsAll(containerTargets)) { return JavaErrorMessages.message("annotation.container.wide.target", container.getQualifiedName()); } } return null; }
@Nullable private static PsiClass getRepeatableContainer(@NotNull PsiAnnotation annotation) { PsiAnnotationMemberValue containerRef = PsiImplUtil.findAttributeValue(annotation, null); if (!(containerRef instanceof PsiClassObjectAccessExpression)) return null; PsiType containerType = ((PsiClassObjectAccessExpression)containerRef).getOperand().getType(); if (!(containerType instanceof PsiClassType)) return null; PsiClass container = ((PsiClassType)containerType).resolve(); if (container == null || !container.isAnnotationType()) return null; return container; }
public static byte buildFlags(GrField field) { byte f = 0; if (field instanceof GrEnumConstant) { f |= IS_ENUM_CONSTANT; } if (field.isProperty()) { f |= IS_PROPERTY; } if (PsiImplUtil.isDeprecatedByDocTag(field)) { f|= IS_DEPRECATED_BY_DOC_TAG; } return f; }
PsiReference[] markRootUsages(final PsiElement element, final PsiType migrationType, final PsiReference[] refs) { final List<PsiReference> validReferences = new ArrayList<PsiReference>(); for (PsiReference ref1 : refs) { final PsiElement ref = ref1.getElement(); if (ref != null) { if (element instanceof PsiMethod) { final PsiElement parent = Util.getEssentialParent(ref); if (!(parent instanceof PsiMethodCallExpression)) { continue; } getTypeEvaluator().setType(new TypeMigrationUsageInfo(parent), migrationType); } else if (element instanceof PsiVariable) { if (ref instanceof PsiReferenceExpression) { getTypeEvaluator().setType(new TypeMigrationUsageInfo(ref), PsiImplUtil.normalizeWildcardTypeByPosition(migrationType, (PsiReferenceExpression)ref)); } } else { LOG.error("Method call expression or reference expression expected but found " + element.getClass().getName()); continue; } validReferences.add(ref1); } } Collections.sort(validReferences, new Comparator<PsiReference>() { public int compare(final PsiReference o1, final PsiReference o2) { return o1.getElement().getTextOffset() - o2.getElement().getTextOffset(); } }); return validReferences.toArray(new PsiReference[validReferences.size()]); }