Java Code Examples for de.espend.idea.php.annotation.util.AnnotationUtil#getAnnotationReference()

The following examples show how to use de.espend.idea.php.annotation.util.AnnotationUtil#getAnnotationReference() . 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: AnnotationCompletionContributor.java    From idea-php-annotation-plugin with MIT License 6 votes vote down vote up
@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
    PsiElement psiElement = parameters.getOriginalPosition();
    if(psiElement == null) {
        return;
    }

    PhpDocTag phpDocTag = PsiTreeUtil.getParentOfType(parameters.getOriginalPosition(), PhpDocTag.class);
    PhpClass phpClass = AnnotationUtil.getAnnotationReference(phpDocTag);
    if(phpClass == null) {
        return;
    }

    AnnotationPropertyParameter annotationPropertyParameter = new AnnotationPropertyParameter(parameters.getOriginalPosition(), phpClass, AnnotationPropertyParameter.Type.DEFAULT);
    providerWalker(parameters, context, result, annotationPropertyParameter);

}
 
Example 2
Source File: AnnotationGoToDeclarationHandler.java    From idea-php-annotation-plugin with MIT License 6 votes vote down vote up
/**
 * Add goto for DocTag itself which should be the PhpClass and provide Extension
 *
 * @param psiElement origin DOC_TAG_NAME psi element
 * @param targets Goto targets
 */
private void addDocTagNameGoto(PsiElement psiElement, List<PsiElement> targets) {

    PsiElement phpDocTagValue = psiElement.getContext();
    if(!(phpDocTagValue instanceof PhpDocTag)) {
        return;
    }

    PhpClass phpClass = AnnotationUtil.getAnnotationReference((PhpDocTag) phpDocTagValue);
    if(phpClass == null) {
        return;
    }

    targets.add(phpClass);

    AnnotationDocTagGotoHandlerParameter parameter = new AnnotationDocTagGotoHandlerParameter((PhpDocTag) phpDocTagValue, phpClass, targets);
    for(PhpAnnotationDocTagGotoHandler phpAnnotationExtension : AnnotationUtil.EP_DOC_TAG_GOTO.getExtensions()) {
        phpAnnotationExtension.getGotoDeclarationTargets(parameter);
    }

}
 
Example 3
Source File: RepositoryClassInspection.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
@Override
public void visitElement(PsiElement psiElement) {
    if(!(psiElement instanceof PhpDocTag)) {
        super.visitElement(psiElement);
        return;
    }

    String name = ((PhpDocTag) psiElement).getName();
    if(AnnotationUtil.isBlockedAnnotationTag(name)) {
        super.visitElement(psiElement);
        return;
    }

    if(!AnnotationUtil.isAnnotationPhpDocTag((PhpDocTag) psiElement)) {
        super.visitElement(psiElement);
        return;
    }

    PhpClass phpClass = AnnotationUtil.getAnnotationReference(((PhpDocTag) psiElement));
    if(phpClass == null) {
        super.visitElement(psiElement);
        return;
    }

    if(!this.className.equals(phpClass.getPresentableFQN())) {
        super.visitElement(psiElement);
        return;
    }

    visitAnnotationProperty((PhpDocTag) psiElement);

    super.visitElement(psiElement);
}
 
Example 4
Source File: DefaultPropertyRegistrarMatcher.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
@Override
public boolean matches(@NotNull LanguageMatcherParameter parameter) {

    List<JsonSignature> filter = ContainerUtil.filter(parameter.getSignatures(), jsonSignature -> "annotation".equals(jsonSignature.getType()) && StringUtils.isNotBlank(jsonSignature.getClassName()));

    if(filter.size() == 0) {
        return false;
    }

    PsiElement parent = parameter.getElement().getParent();
    if(!(parent instanceof StringLiteralExpression)) {
        return false;
    }

    boolean accepts = AnnotationPattern.getDefaultPropertyValueString().accepts(parent);
    if(!accepts) {
        return false;
    }

    PhpDocTag phpDocTag = PsiTreeUtil.getParentOfType(parent, PhpDocTag.class);
    PhpClass phpClass = AnnotationUtil.getAnnotationReference(phpDocTag);
    if(phpClass == null) {
        return false;
    }

    for (JsonSignature signature : filter) {
        if(StringUtils.stripStart(phpClass.getFQN(), "\\").equals(StringUtils.stripStart(signature.getClassName(), "\\"))) {
            return true;
        }
    }

    return false;
}
 
Example 5
Source File: AnnotationCompletionContributor.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
    PsiElement psiElement = parameters.getOriginalPosition();
    if(psiElement == null) {
        return;
    }

    PsiElement parent = psiElement.getParent();
    if(!(parent instanceof StringLiteralExpression)) {
        return;
    }

    PhpDocTag phpDocTag = PsiTreeUtil.getParentOfType(parameters.getOriginalPosition(), PhpDocTag.class);
    PhpClass phpClass = AnnotationUtil.getAnnotationReference(phpDocTag);
    if(phpClass == null) {
        return;
    }

    PsiElement propertyForEnum = AnnotationUtil.getPropertyForArray((StringLiteralExpression) parent);
    if(propertyForEnum == null) {
        return;
    }

    AnnotationPropertyParameter annotationPropertyParameter = new AnnotationPropertyParameter(
        parameters.getOriginalPosition(),
        phpClass,
        propertyForEnum.getText(),
        AnnotationPropertyParameter.Type.PROPERTY_ARRAY
    );

    providerWalker(parameters, context, result, annotationPropertyParameter);
}
 
Example 6
Source File: AnnotationCompletionContributor.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
    PsiElement psiElement = parameters.getOriginalPosition();
    if(psiElement == null) {
        return;
    }

    PsiElement phpDocString = psiElement.getContext();
    if(!(phpDocString instanceof StringLiteralExpression)) {
        return;
    }

    PsiElement propertyName = PhpElementsUtil.getPrevSiblingOfPatternMatch(phpDocString, PlatformPatterns.psiElement(PhpDocTokenTypes.DOC_IDENTIFIER));
    if(propertyName == null) {
        return;
    }

    PhpDocTag phpDocTag = PsiTreeUtil.getParentOfType(parameters.getOriginalPosition(), PhpDocTag.class);
    PhpClass phpClass = AnnotationUtil.getAnnotationReference(phpDocTag);
    if(phpClass == null) {
        return;
    }

    AnnotationPropertyParameter annotationPropertyParameter = new AnnotationPropertyParameter(parameters.getOriginalPosition(), phpClass, propertyName.getText(), AnnotationPropertyParameter.Type.PROPERTY_VALUE);
    providerWalker(parameters, context, result, annotationPropertyParameter);

}
 
Example 7
Source File: AnnotationGoToDeclarationHandler.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
/**
 * Add goto for property value which are Fields inside PhpClass
 *
 * @param psiElement origin DOC_IDENTIFIER psi element
 * @param targets Goto targets
 */
private void addPropertyGoto(PsiElement psiElement, List<PsiElement> targets) {
    PhpDocTag phpDocTag = PsiTreeUtil.getParentOfType(psiElement, PhpDocTag.class);
    if(phpDocTag == null) {
        return;
    }

    PhpClass phpClass = AnnotationUtil.getAnnotationReference(phpDocTag);
    if(phpClass == null) {
        return;
    }

    String property = psiElement.getText();
    if(StringUtils.isBlank(property)) {
        return;
    }

    AnnotationUtil.visitAttributes(phpClass, (attributeName, type, target) -> {
        if(attributeName.equals(property)) {
            targets.add(target);
        }

        return null;
    });

    // extension point to provide virtual properties / fields targets
    AnnotationVirtualPropertyTargetsParameter parameter = null;
    for (PhpAnnotationVirtualProperties ep : AnnotationUtil.EP_VIRTUAL_PROPERTIES.getExtensions()) {
        if(parameter == null) {
            parameter = new AnnotationVirtualPropertyTargetsParameter(phpClass, psiElement, property);
        }

        ep.getTargets(parameter);
    }

    if(parameter != null) {
        targets.addAll(parameter.getTargets());
    }
}
 
Example 8
Source File: AnnotationDeprecatedInspection.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
private void visitAnnotationDocTag(PhpDocTag phpDocTag, @NotNull ProblemsHolder holder) {
    PhpClass phpClass = AnnotationUtil.getAnnotationReference(phpDocTag);
    if (phpClass == null || !phpClass.isDeprecated()) {
        return;
    }

    PsiElement firstChild = phpDocTag.getFirstChild();
    if (firstChild == null || firstChild.getNode().getElementType() != PhpDocElementTypes.DOC_TAG_NAME) {
        return;
    }

    holder.registerProblem(firstChild, MESSAGE, ProblemHighlightType.LIKE_DEPRECATED);
}
 
Example 9
Source File: DocTagNameAnnotationReferenceContributor.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
@NotNull
@Override
public ResolveResult[] multiResolve(boolean b) {

    PhpClass phpClass = AnnotationUtil.getAnnotationReference(getElement());
    if(phpClass == null) {
        return new ResolveResult[0];
    }

    return new ResolveResult[] { new PsiElementResolveResult(phpClass) };
}
 
Example 10
Source File: AnnotationPropertyValueReferenceContributor.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
@Nullable
private PhpClass getValidAnnotationClass(PsiElement psiElement) {
    PhpDocTag phpDocTag = PsiTreeUtil.getParentOfType(psiElement, PhpDocTag.class);
    if(phpDocTag == null) {
        return null;
    }

    return AnnotationUtil.getAnnotationReference(phpDocTag);
}
 
Example 11
Source File: PropertyArrayRegistrarMatcher.java    From idea-php-annotation-plugin with MIT License 4 votes vote down vote up
@Override
public boolean matches(@NotNull LanguageMatcherParameter parameter) {
    PsiElement element = parameter.getElement();

    PsiElement docString = element.getParent();
    if(!(docString instanceof StringLiteralExpression)) {
        return false;
    }

    if(!AnnotationPattern.getPropertyArrayPattern().accepts(element)) {
        return false;
    }

    List<JsonSignature> filter = ContainerUtil.filter(parameter.getSignatures(), jsonSignature ->
        "annotation_array".equals(jsonSignature.getType()) &&
        StringUtils.isNotBlank(jsonSignature.getField()) &&
        StringUtils.isNotBlank(jsonSignature.getClassName())
    );

    if(filter.size() == 0) {
        return false;
    }

    PsiElement propertyForEnum = AnnotationUtil.getPropertyForArray((StringLiteralExpression) docString);
    if(propertyForEnum == null) {
        return false;
    }

    String propertyName = propertyForEnum.getText();
    if(StringUtils.isBlank(propertyName)) {
        return false;
    }

    for (JsonSignature signature : filter) {
        if(!propertyName.equals(signature.getField())) {
            continue;
        }

        PhpClass phpClass = AnnotationUtil.getAnnotationReference(PsiTreeUtil.getParentOfType(docString, PhpDocTag.class));
        if(phpClass == null) {
            continue;
        }

        if(StringUtils.stripStart(phpClass.getFQN(), "\\").equalsIgnoreCase(StringUtils.stripStart(signature.getClassName(), "\\"))) {
            return true;
        }
    }

    return false;
}
 
Example 12
Source File: PropertyRegistrarMatcher.java    From idea-php-annotation-plugin with MIT License 4 votes vote down vote up
@Override
public boolean matches(@NotNull LanguageMatcherParameter parameter) {

    List<JsonSignature> filter = ContainerUtil.filter(parameter.getSignatures(), jsonSignature -> "annotation".equals(jsonSignature.getType()) &&
    StringUtils.isNotBlank(jsonSignature.getField()) &&
    StringUtils.isNotBlank(jsonSignature.getClassName()));

    if(filter.size() == 0) {
        return false;
    }

    PsiElement phpDocString = parameter.getElement().getParent();
    if(!(phpDocString instanceof StringLiteralExpression)) {
        return false;
    }

    boolean accepts = AnnotationPattern.getTextIdentifier().accepts(parameter.getElement());
    if(!accepts) {
        return false;
    }

    PsiElement propertyName = PhpElementsUtil.getPrevSiblingOfPatternMatch(
        phpDocString,
        PlatformPatterns.psiElement(PhpDocTokenTypes.DOC_IDENTIFIER)
    );

    if(propertyName == null) {
        return false;
    }

    String fieldName = propertyName.getText();
    if(StringUtils.isBlank(fieldName)) {
        return false;
    }

    for (JsonSignature signature : filter) {
        if(!fieldName.equals(signature.getField())) {
            continue;
        }

        PhpClass phpClass = AnnotationUtil.getAnnotationReference(PsiTreeUtil.getParentOfType(phpDocString, PhpDocTag.class));
        if(phpClass == null) {
            continue;
        }

        if(StringUtils.stripStart(phpClass.getFQN(), "\\").equals(StringUtils.stripStart(signature.getClassName(), "\\"))) {
            return true;
        }
    }

    return false;
}
 
Example 13
Source File: AnnotationCompletionContributor.java    From idea-php-annotation-plugin with MIT License 4 votes vote down vote up
@Override
protected void addCompletions(@NotNull CompletionParameters completionParameters, @NotNull ProcessingContext processingContext, @NotNull CompletionResultSet completionResultSet) {
    PsiElement psiElement = completionParameters.getOriginalPosition();
    if (psiElement == null) {
        return;
    }

    PhpDocTag phpDocTag = PsiTreeUtil.getParentOfType(psiElement, PhpDocTag.class);
    if (phpDocTag == null) {
        return;
    }

    PhpClass phpClass = AnnotationUtil.getAnnotationReference(phpDocTag);
    if (phpClass == null) {
        return;
    }

    AnnotationUtil.visitAttributes(phpClass, (attributeName, type, target) -> {
        completionResultSet.addElement(new PhpAnnotationPropertyLookupElement(new AnnotationProperty(attributeName, AnnotationPropertyEnum.fromString(type))));
        return null;
    });

    // extension point for virtual properties
    AnnotationVirtualPropertyCompletionParameter virtualPropertyParameter = null;
    AnnotationCompletionProviderParameter parameter = null;

    for (PhpAnnotationVirtualProperties ep : AnnotationUtil.EP_VIRTUAL_PROPERTIES.getExtensions()) {
        if (virtualPropertyParameter == null) {
            virtualPropertyParameter = new AnnotationVirtualPropertyCompletionParameter(phpClass);
        }

        if (parameter == null) {
            parameter = new AnnotationCompletionProviderParameter(completionParameters, processingContext, completionResultSet);
        }

        ep.addCompletions(virtualPropertyParameter, parameter);
    }

    if (virtualPropertyParameter != null) {
        for (Map.Entry<String, AnnotationPropertyEnum> pair : virtualPropertyParameter.getLookupElements().entrySet()) {
            completionResultSet.addElement(new PhpAnnotationPropertyLookupElement(
                new AnnotationProperty(pair.getKey(), pair.getValue()))
            );
        }
    }
}
 
Example 14
Source File: AnnotationMissingUseInspection.java    From idea-php-annotation-plugin with MIT License 4 votes vote down vote up
private void visitAnnotationDocTag(@NotNull PhpDocTag phpDocTag, @NotNull ProblemsHolder holder, @NotNull AnnotationInspectionUtil.LazyNamespaceImportResolver lazyNamespaceImportResolver) {
    // Target for our inspection is DocTag name: @Foobar() => Foobar
    // This prevent highlighting the complete DocTag
    PsiElement firstChild = phpDocTag.getFirstChild();
    if (firstChild == null || firstChild.getNode().getElementType() != PhpDocElementTypes.DOC_TAG_NAME) {
        return;
    }

    String name = phpDocTag.getName();
    String tagName = StringUtils.stripStart(name, "@");

    // ignore "@\Foo" absolute FQN ones
    if (tagName.startsWith("\\")) {
        return;
    }

    String[] split = tagName.split("\\\\");

    Map<String, String> useImportMap = lazyNamespaceImportResolver.getImports();
    if (useImportMap.containsKey(split[0])) {
        return;
    }

    PhpClass annotationReference = AnnotationUtil.getAnnotationReference(phpDocTag);
    if (annotationReference != null) {
        return;
    }

    Map<String, String> phpClasses = AnnotationUtil.getPossibleImportClasses(phpDocTag);
    if (phpClasses.size() > 0) {
        Collection<Pair<String, String>> collect = phpClasses.entrySet().stream()
            .map(entry -> Pair.create(entry.getKey(), entry.getValue()))
            .collect(Collectors.toList());

        holder.registerProblem(
            firstChild,
            MESSAGE,
            ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
            new ImportUseForAnnotationQuickFix(phpDocTag, collect)
        );
    }
}