Search in sources :

Example 6 with RetentionPolicy

use of java.lang.annotation.RetentionPolicy in project groovy by apache.

the class Java5 method configureAnnotationFromDefinition.

public static void configureAnnotationFromDefinition(AnnotationNode definition, AnnotationNode root) {
    ClassNode type = definition.getClassNode();
    if ("java.lang.annotation.Retention".equals(type.getName())) {
        Expression exp = definition.getMember("value");
        if (!(exp instanceof PropertyExpression))
            return;
        PropertyExpression pe = (PropertyExpression) exp;
        String name = pe.getPropertyAsString();
        RetentionPolicy policy = RetentionPolicy.valueOf(name);
        setRetentionPolicy(policy, root);
    } else if ("java.lang.annotation.Target".equals(type.getName())) {
        Expression exp = definition.getMember("value");
        if (!(exp instanceof ListExpression))
            return;
        ListExpression le = (ListExpression) exp;
        int bitmap = 0;
        for (Expression e : le.getExpressions()) {
            if (!(e instanceof PropertyExpression))
                return;
            PropertyExpression element = (PropertyExpression) e;
            String name = element.getPropertyAsString();
            ElementType value = ElementType.valueOf(name);
            bitmap |= getElementCode(value);
        }
        root.setAllowedTargets(bitmap);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ElementType(java.lang.annotation.ElementType) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) RetentionPolicy(java.lang.annotation.RetentionPolicy)

Example 7 with RetentionPolicy

use of java.lang.annotation.RetentionPolicy in project intellij-plugins by JetBrains.

the class PackageAccessibilityInspection method checkAccessibility.

// OSGi Core Spec 3.5 "Class Loading Architecture"
private static Problem checkAccessibility(PsiClass targetClass, OsmorcFacet facet) {
    // ignores annotations invisible at runtime
    if (targetClass.isAnnotationType()) {
        RetentionPolicy retention = AnnotationsHighlightUtil.getRetentionPolicy(targetClass);
        if (retention == RetentionPolicy.SOURCE || retention == RetentionPolicy.CLASS) {
            return null;
        }
    }
    // ignores files of unsupported type
    PsiFile targetFile = targetClass.getContainingFile();
    if (!(targetFile instanceof PsiClassOwner)) {
        return null;
    }
    // accepts classes from the parent class loader (normally java.* packages from the boot class path)
    String packageName = ((PsiClassOwner) targetFile).getPackageName();
    if (packageName.isEmpty() || packageName.startsWith("java.")) {
        return null;
    }
    // accepts classes from the bundle's class path (private packages)
    Module requestorModule = facet.getModule();
    Module targetModule = ModuleUtilCore.findModuleForPsiElement(targetClass);
    if (targetModule == requestorModule) {
        return null;
    }
    BundleManifest importer = BundleManifestCache.getInstance(targetClass.getProject()).getManifest(requestorModule);
    if (importer != null && (importer.isPrivatePackage(packageName) || importer.getExportedPackage(packageName) != null)) {
        return null;
    }
    // rejects non-exported classes (manifest missing, or a package isn't listed as exported)
    BundleManifest exporter = BundleManifestCache.getInstance(targetClass.getProject()).getManifest(targetClass);
    if (exporter == null || exporter.getBundleSymbolicName() == null) {
        return Problem.weak(message("PackageAccessibilityInspection.non.osgi", packageName));
    }
    String exportedPackage = exporter.getExportedPackage(packageName);
    if (exportedPackage == null) {
        return Problem.error(message("PackageAccessibilityInspection.not.exported", packageName));
    }
    // ignores facets other than manually-edited manifests (most probably, they will have their import list correctly generated)
    if (!facet.getConfiguration().isManifestManuallyEdited()) {
        return null;
    }
    // accepts packages listed as imported or required
    if (importer != null) {
        if (importer.isPackageImported(packageName)) {
            return null;
        }
        if (importer.isBundleRequired(exporter.getBundleSymbolicName())) {
            return null;
        }
    // Attached fragments [AFAIK these should not be linked statically - r.sh]
    }
    return Problem.error(message("PackageAccessibilityInspection.not.imported", packageName), new ImportPackageFix(exportedPackage));
}
Also used : PsiClassOwner(com.intellij.psi.PsiClassOwner) BundleManifest(org.jetbrains.osgi.project.BundleManifest) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module) RetentionPolicy(java.lang.annotation.RetentionPolicy)

Aggregations

RetentionPolicy (java.lang.annotation.RetentionPolicy)7 ElementType (java.lang.annotation.ElementType)4 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)4 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)4 Expression (org.codehaus.groovy.ast.expr.Expression)4 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)4 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)4 Retention (java.lang.annotation.Retention)3 Target (java.lang.annotation.Target)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 ClassNode (org.codehaus.groovy.ast.ClassNode)2 Module (com.intellij.openapi.module.Module)1 PsiClassOwner (com.intellij.psi.PsiClassOwner)1 PsiFile (com.intellij.psi.PsiFile)1 PsiImmediateClassType (com.intellij.psi.impl.source.PsiImmediateClassType)1 Nullable (org.jetbrains.annotations.Nullable)1 BundleManifest (org.jetbrains.osgi.project.BundleManifest)1