Search in sources :

Example 16 with PackageRef

use of aQute.bnd.osgi.Descriptors.PackageRef in project bnd by bndtools.

the class Verifier method verifyActivator.

private void verifyActivator() throws Exception {
    String bactivator = main.get(Constants.BUNDLE_ACTIVATOR);
    if (bactivator != null) {
        if (!PACKAGEPATTERN.matcher(bactivator).matches()) {
            boolean allElementsAreTypes = true;
            for (String element : split(bactivator)) {
                if (!PACKAGEPATTERN.matcher(element.trim()).matches()) {
                    allElementsAreTypes = false;
                    break;
                }
            }
            if (allElementsAreTypes) {
                registerActivatorErrorLocation(error("The Bundle-Activator header only supports a single type. The following types were found: %s. This usually happens when a macro resolves to multiple types", bactivator), bactivator, ActivatorErrorType.MULTIPLE_TYPES);
            } else {
                registerActivatorErrorLocation(error("A Bundle-Activator header is present and its value is not a valid type name %s", bactivator), bactivator, ActivatorErrorType.INVALID_TYPE_NAME);
            }
            return;
        }
        TypeRef ref = analyzer.getTypeRefFromFQN(bactivator);
        if (analyzer.getClassspace().containsKey(ref)) {
            Clazz activatorClazz = analyzer.getClassspace().get(ref);
            if (activatorClazz.isInterface()) {
                registerActivatorErrorLocation(error("The Bundle Activator %s is an interface and therefore cannot be instantiated.", bactivator), bactivator, ActivatorErrorType.IS_INTERFACE);
            } else {
                if (activatorClazz.isAbstract()) {
                    registerActivatorErrorLocation(error("The Bundle Activator %s is abstract and therefore cannot be instantiated.", bactivator), bactivator, ActivatorErrorType.IS_ABSTRACT);
                }
                if (!activatorClazz.isPublic()) {
                    registerActivatorErrorLocation(error("Bundle Activator classes must be public, and %s is not.", bactivator), bactivator, ActivatorErrorType.NOT_PUBLIC);
                }
                if (!activatorClazz.hasPublicNoArgsConstructor()) {
                    registerActivatorErrorLocation(error("Bundle Activator classes must have a public zero-argument constructor and %s does not.", bactivator), bactivator, ActivatorErrorType.NO_SUITABLE_CONSTRUCTOR);
                }
                if (!analyzer.assignable(activatorClazz.getFQN(), "org.osgi.framework.BundleActivator")) {
                    registerActivatorErrorLocation(error("The Bundle Activator %s does not implement BundleActivator.", bactivator), bactivator, ActivatorErrorType.NOT_AN_ACTIVATOR);
                }
            }
            return;
        }
        PackageRef packageRef = ref.getPackageRef();
        if (packageRef.isDefaultPackage())
            registerActivatorErrorLocation(error("The Bundle Activator is not in the bundle and it is in the default package "), bactivator, ActivatorErrorType.DEFAULT_PACKAGE);
        else if (!analyzer.isImported(packageRef)) {
            registerActivatorErrorLocation(error(Constants.BUNDLE_ACTIVATOR + " not found on the bundle class path nor in imports: %s", bactivator), bactivator, ActivatorErrorType.NOT_ACCESSIBLE);
        } else {
            registerActivatorErrorLocation(warning(Constants.BUNDLE_ACTIVATOR + " %s is being imported into the bundle rather than being contained inside it. This is usually a bundle packaging error", bactivator), bactivator, ActivatorErrorType.IS_IMPORTED);
        }
    } else if (parent != null) {
        // If we have access to the parent we can do deeper checking
        String raw = parent.getUnprocessedProperty(BUNDLE_ACTIVATOR, null);
        if (raw != null) {
            // The activator was specified, but nothing showed up.
            if (raw.isEmpty()) {
                registerActivatorErrorLocation(warning("A Bundle-Activator header was present but no activator class was defined"), "", ActivatorErrorType.NOT_SET);
            } else {
                registerActivatorErrorLocation(error("A Bundle-Activator header is present but no activator class was found using the macro %s", raw), raw, ActivatorErrorType.NO_RESULT_FROM_MACRO);
            }
            return;
        }
    }
}
Also used : TypeRef(aQute.bnd.osgi.Descriptors.TypeRef) PackageRef(aQute.bnd.osgi.Descriptors.PackageRef)

Example 17 with PackageRef

use of aQute.bnd.osgi.Descriptors.PackageRef in project bnd by bndtools.

the class ClazzTest method testDynamicInstr.

/**
	 * Complaint from Groovy that the dynamic instruction fails.
	 * 
	 * <pre>
	 *  [bndwrap]
	 * java.lang.ArrayIndexOutOfBoundsException: 15 [bndwrap] at
	 * aQute.bnd.osgi.Clazz.parseClassFile(Clazz.java:387) [bndwrap] at
	 * aQute.bnd.osgi.Clazz.parseClassFile(Clazz.java:308) [bndwrap] at
	 * aQute.bnd.osgi.Clazz.parseClassFileWithCollector(Clazz.java:297)
	 * [bndwrap] at aQute.bnd.osgi.Clazz.parseClassFile(Clazz.java:286)
	 * [bndwrap] at aQute.bnd.osgi.Analyzer.analyzeJar(Analyzer.java:1489)
	 * [bndwrap] at
	 * aQute.bnd.osgi.Analyzer.analyzeBundleClasspath(Analyzer.java:1387)
	 * [bndwrap] Invalid class file:
	 * groovy/inspect/swingui/AstNodeToScriptVisitor.class [bndwrap] Exception:
	 * 15
	 * </pre>
	 */
public void testDynamicInstr() throws Exception {
    try (Analyzer a = new Analyzer()) {
        Clazz c = new Clazz(a, "", null);
        c.parseClassFile(new FileInputStream("jar/AstNodeToScriptVisitor.jclass"), new ClassDataCollector() {
        });
        Set<PackageRef> referred = c.getReferred();
        Descriptors d = new Descriptors();
        assertFalse(referred.contains(d.getPackageRef("")));
        System.out.println(referred);
    }
}
Also used : Clazz(aQute.bnd.osgi.Clazz) Descriptors(aQute.bnd.osgi.Descriptors) Analyzer(aQute.bnd.osgi.Analyzer) ClassDataCollector(aQute.bnd.osgi.ClassDataCollector) PackageRef(aQute.bnd.osgi.Descriptors.PackageRef) FileInputStream(java.io.FileInputStream)

Example 18 with PackageRef

use of aQute.bnd.osgi.Descriptors.PackageRef in project bnd by bndtools.

the class ClazzTest method testClassForNameFalsePickup.

/**
	 * Check if the class is not picking up false references when the
	 * CLass.forName name is constructed. The DeploymentAdminPermission.1.jclass
	 * turned out to use Class.forName with a name that was prefixed with a
	 * package from a property. bnd discovered the suffix
	 * (.DeploymentAdminPermission) but this ended up in the default package. So
	 * now the clazz parser tests that the name guessed for Class.forName must
	 * actually resemble a class name.
	 */
public void testClassForNameFalsePickup() throws Exception {
    try (Analyzer a = new Analyzer()) {
        Clazz c = new Clazz(a, "", null);
        c.parseClassFile(new FileInputStream("jar/DeploymentAdminPermission.1.jclass"), new ClassDataCollector() {
        });
        Set<PackageRef> referred = c.getReferred();
        Descriptors d = new Descriptors();
        assertFalse(referred.contains(d.getPackageRef("")));
        System.out.println(referred);
    }
}
Also used : Clazz(aQute.bnd.osgi.Clazz) Descriptors(aQute.bnd.osgi.Descriptors) Analyzer(aQute.bnd.osgi.Analyzer) ClassDataCollector(aQute.bnd.osgi.ClassDataCollector) PackageRef(aQute.bnd.osgi.Descriptors.PackageRef) FileInputStream(java.io.FileInputStream)

Example 19 with PackageRef

use of aQute.bnd.osgi.Descriptors.PackageRef in project bnd by bndtools.

the class VerifierPluginTest method testVerifierPluginExecution.

public static void testVerifierPluginExecution() throws Exception {
    final AtomicBoolean executedCheck = new AtomicBoolean(false);
    VerifierPlugin verifier = new VerifierPlugin() {

        @Override
        public void verify(Analyzer analyzer) throws Exception {
            // the following is true only after the jar and manifest have
            // been analyzed
            Packages exports = analyzer.getExports();
            PackageRef packageRef = analyzer.getPackageRef("test/activator");
            if (exports.containsKey(packageRef)) {
                executedCheck.set(true);
            }
        }
    };
    try (Builder b = new Builder()) {
        b.setProperty("Bundle-SymbolicName", "p1");
        b.setProperty("Bundle-Version", "1.2.3");
        b.setExportPackage("test.activator");
        b.addClasspath(new File("bin"));
        b.getPlugins().add(verifier);
        Jar jar = b.build();
    }
    assertTrue(executedCheck.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) VerifierPlugin(aQute.bnd.service.verifier.VerifierPlugin) Packages(aQute.bnd.osgi.Packages) Builder(aQute.bnd.osgi.Builder) Jar(aQute.bnd.osgi.Jar) Analyzer(aQute.bnd.osgi.Analyzer) PackageRef(aQute.bnd.osgi.Descriptors.PackageRef) File(java.io.File)

Example 20 with PackageRef

use of aQute.bnd.osgi.Descriptors.PackageRef in project bnd by bndtools.

the class DiffPluginImpl method hasSource.

private boolean hasSource(Analyzer analyzer, String path) throws Exception {
    if (!path.endsWith(".class"))
        return false;
    TypeRef type = analyzer.getTypeRefFromPath(path);
    PackageRef packageRef = type.getPackageRef();
    Clazz clazz = analyzer.findClass(type);
    if (clazz == null)
        return false;
    String sourceFile = clazz.getSourceFile();
    if (sourceFile == null)
        return false;
    String source = "OSGI-OPT/src/" + packageRef.getBinary() + "/" + sourceFile;
    Resource sourceResource = analyzer.getJar().getResource(source);
    if (sourceResource == null)
        return false;
    return true;
}
Also used : TypeRef(aQute.bnd.osgi.Descriptors.TypeRef) Resource(aQute.bnd.osgi.Resource) Clazz(aQute.bnd.osgi.Clazz) PackageRef(aQute.bnd.osgi.Descriptors.PackageRef)

Aggregations

PackageRef (aQute.bnd.osgi.Descriptors.PackageRef)40 Attrs (aQute.bnd.header.Attrs)18 Analyzer (aQute.bnd.osgi.Analyzer)9 Clazz (aQute.bnd.osgi.Clazz)8 Map (java.util.Map)8 Parameters (aQute.bnd.header.Parameters)7 TypeRef (aQute.bnd.osgi.Descriptors.TypeRef)7 MultiMap (aQute.lib.collections.MultiMap)7 HashSet (java.util.HashSet)7 TreeSet (java.util.TreeSet)7 Manifest (java.util.jar.Manifest)7 Jar (aQute.bnd.osgi.Jar)6 Resource (aQute.bnd.osgi.Resource)6 Descriptors (aQute.bnd.osgi.Descriptors)5 ClassDataCollector (aQute.bnd.osgi.ClassDataCollector)4 File (java.io.File)4 FileInputStream (java.io.FileInputStream)4 InputStream (java.io.InputStream)4 HashMap (java.util.HashMap)4 Entry (java.util.Map.Entry)4