Skip to content

Commit

Permalink
ProviderType: inheritance + package level
Browse files Browse the repository at this point in the history
---
 Signed-off-by: Peter Kriens <[email protected]>

Signed-off-by: Peter Kriens <[email protected]>
  • Loading branch information
pkriens committed Mar 18, 2024
1 parent 104ad99 commit dd7fc3f
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 11 deletions.
37 changes: 26 additions & 11 deletions biz.aQute.bndlib/src/aQute/bnd/osgi/Analyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ public class Analyzer extends Processor {
private Set<PackageRef> nonClassReferences = new HashSet<>();
private Set<Check> checks;
private final Map<TypeRef, String> bcpTypes = map();
final TypeRef providerType = getTypeRef(
"org/osgi/annotation/versioning/ProviderType");

public enum Check {
ALL,
Expand Down Expand Up @@ -2170,18 +2172,31 @@ Set<PackageRef> findProvidedPackages() throws Exception {
return providers;
}

private boolean isProvider(TypeRef t) {
Clazz c;

boolean isProvider(TypeRef t) {
if (t == null || t.isJava())
return false;

try {
c = findClass(t);

if (isProvider(t.getPackageRef()))
return true;

Clazz c = findClass(t);

return c.annotations()
.contains(providerType) || isProvider(c.superClass);
} catch (Exception e) {
return false;
}
if (c == null)
return false;
}

TypeRef providerType = getTypeRef("org/osgi/annotation/versioning/ProviderType");
return c.annotations()
boolean isProvider(PackageRef packageRef) throws Exception {
if (packageRef == null)
return false;
TypeRef packageInfo = getTypeRef(packageRef.binaryName.concat("/package-info"));
Clazz c = findClass(packageInfo);
return c != null && c.annotations()
.contains(providerType);
}

Expand Down Expand Up @@ -3212,6 +3227,7 @@ public PackageRef getPackageRef(String binaryName) {
public TypeRef getTypeRefFrom(Class<?> clazz) {
return descriptors.getTypeRefFromFQN(clazz.getName());
}

public TypeRef getTypeRefFromFQN(String fqn) {
return descriptors.getTypeRefFromFQN(fqn);
}
Expand Down Expand Up @@ -3865,10 +3881,9 @@ public void addDelta(Jar delta) {
}

/**
* Useful to reuse the annotation processing. The Annotation
* can be created from other sources than Java code. This
* is mostly useful for the annotations that generate Manifest
* headers.
* Useful to reuse the annotation processing. The Annotation can be created
* from other sources than Java code. This is mostly useful for the
* annotations that generate Manifest headers.
*/
public void addAnnotation(Annotation ann, TypeRef c) throws Exception {
Clazz clazz = findClass(c);
Expand Down
43 changes: 43 additions & 0 deletions biz.aQute.bndlib/test/aQute/bnd/osgi/AnalyzerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package aQute.bnd.osgi;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

import aQute.bnd.osgi.providertype.TestProviderInheritance;
import aQute.bnd.osgi.providertype.packg.TestProviderInheritancePackage;
import aQute.lib.io.IO;

class AnalyzerTest {

/**
* Test provider type inheritance and package
*/

@Test
public void testProviderInheritance() throws Exception {
try (Builder outer = new Builder()) {
try (Builder builder = new Builder()) {
builder.addClasspath(IO.getFile("bin_test"));
builder.setProperty("-includepackage", "aQute.bnd.osgi.providertype.*");
builder.build();
assertThat(builder.check()).isTrue();

assertThat(isProvider(builder, TestProviderInheritance.Top.class)).isTrue();
assertThat(isProvider(builder, TestProviderInheritance.Middle.class)).isTrue();
assertThat(isProvider(builder, TestProviderInheritance.Bottom.class)).isTrue();
assertThat(isProvider(builder, TestProviderInheritance.None.class)).isFalse();

assertThat(isProvider(builder, TestProviderInheritancePackage.Top.class)).isTrue();
assertThat(isProvider(builder, TestProviderInheritancePackage.Middle.class)).isTrue();
assertThat(isProvider(builder, TestProviderInheritancePackage.Bottom.class)).isTrue();
assertThat(isProvider(builder, TestProviderInheritancePackage.None.class)).isTrue();
}
}
}

private boolean isProvider(Builder builder, Class<?> type) {
return builder.isProvider(builder.getTypeRefFromFQN(type.getName()));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package aQute.bnd.osgi.providertype;

import org.osgi.annotation.versioning.ProviderType;

public class TestProviderInheritance {
@ProviderType
public static class Top {}

public static class Middle extends Top {}

public static class Bottom extends Middle {}

public static class None {}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@Version("1.0.0")
@Export
package aQute.bnd.osgi.providertype;

import org.osgi.annotation.bundle.Export;
import org.osgi.annotation.versioning.Version;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package aQute.bnd.osgi.providertype.packg;

public class TestProviderInheritancePackage {
public static class Top {}

public static class Middle extends Top {}

public static class Bottom extends Middle {}

public static class None {}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@Version("1.0.0")
@Export
@ProviderType
package aQute.bnd.osgi.providertype.packg;

import org.osgi.annotation.bundle.Export;
import org.osgi.annotation.versioning.ProviderType;
import org.osgi.annotation.versioning.Version;

0 comments on commit dd7fc3f

Please sign in to comment.