Search in sources :

Example 1 with BundleManifest

use of org.jetbrains.osgi.project.BundleManifest in project intellij-plugins by JetBrains.

the class UnregisteredActivatorInspection method buildVisitor.

@NotNull
@Override
protected PsiElementVisitor buildVisitor(final OsmorcFacet facet, final ProblemsHolder holder, boolean isOnTheFly) {
    if (!(holder.getFile() instanceof PsiClassOwner))
        return PsiElementVisitor.EMPTY_VISITOR;
    return new JavaElementVisitor() {

        @Override
        public void visitFile(PsiFile file) {
            if (file instanceof PsiClassOwner) {
                for (PsiClass psiClass : ((PsiClassOwner) file).getClasses()) {
                    String className = psiClass.getQualifiedName();
                    if (OsgiPsiUtil.isActivator(psiClass) && className != null) {
                        BundleManifest manifest = BundleManifestCache.getInstance(psiClass.getProject()).getManifest(facet.getModule());
                        if (manifest != null && !className.equals(manifest.getBundleActivator())) {
                            LocalQuickFix[] fixes = LocalQuickFix.EMPTY_ARRAY;
                            OsmorcFacetConfiguration configuration = facet.getConfiguration();
                            if (configuration.isManifestManuallyEdited()) {
                                fixes = new LocalQuickFix[] { new RegisterInManifestQuickfix(className) };
                            } else if (configuration.isOsmorcControlsManifest()) {
                                fixes = new LocalQuickFix[] { new RegisterInConfigurationQuickfix(className, configuration) };
                            }
                            PsiElement identifier = unwrap(psiClass.getNameIdentifier());
                            if (identifier != null) {
                                holder.registerProblem(identifier, OsmorcBundle.message("UnregisteredActivatorInspection.message"), fixes);
                            }
                        }
                    }
                }
            }
        }
    };
}
Also used : OsmorcFacetConfiguration(org.osmorc.facet.OsmorcFacetConfiguration) BundleManifest(org.jetbrains.osgi.project.BundleManifest) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with BundleManifest

use of org.jetbrains.osgi.project.BundleManifest 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

BundleManifest (org.jetbrains.osgi.project.BundleManifest)2 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 Module (com.intellij.openapi.module.Module)1 PsiClassOwner (com.intellij.psi.PsiClassOwner)1 PsiFile (com.intellij.psi.PsiFile)1 RetentionPolicy (java.lang.annotation.RetentionPolicy)1 NotNull (org.jetbrains.annotations.NotNull)1 OsmorcFacetConfiguration (org.osmorc.facet.OsmorcFacetConfiguration)1