From ad470321f96f07486c12b9c318709a3ef1040512 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Wed, 12 Feb 2025 14:07:34 +0100 Subject: [PATCH] Evaluate Package Attributes from source --- .../tycho/bndlib/JdtProjectBuilder.java | 30 +++++++++++++++++++ .../bndlib/SourceCodeAnalyzerPlugin.java | 27 +++++++++++++++-- tycho-build/pom.xml | 5 ++++ .../tycho/build/bnd/BndProjectMapping.java | 3 +- 4 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 tycho-bndlib/src/main/java/org/eclipse/tycho/bndlib/JdtProjectBuilder.java diff --git a/tycho-bndlib/src/main/java/org/eclipse/tycho/bndlib/JdtProjectBuilder.java b/tycho-bndlib/src/main/java/org/eclipse/tycho/bndlib/JdtProjectBuilder.java new file mode 100644 index 0000000000..61f7222b73 --- /dev/null +++ b/tycho-bndlib/src/main/java/org/eclipse/tycho/bndlib/JdtProjectBuilder.java @@ -0,0 +1,30 @@ +package org.eclipse.tycho.bndlib; + +import aQute.bnd.build.Project; +import aQute.bnd.build.ProjectBuilder; + +public class JdtProjectBuilder extends ProjectBuilder { + + private boolean initpackages; + + public JdtProjectBuilder(Project project) { + super(project); + addBasicPlugin(new SourceCodeAnalyzerPlugin()); + } + + @Override + public void analyze() throws Exception { + // TODO Auto-generated method stub + super.analyze(); + } + + @Override + public String _packageattribute(String[] args) { + SourceCodeAnalyzerPlugin analyzerPlugin = getPlugin(SourceCodeAnalyzerPlugin.class); + try { + analyzerPlugin.analyzeJar(this); + } catch (Exception e) { + } + return super._packageattribute(args); + } +} diff --git a/tycho-bndlib/src/main/java/org/eclipse/tycho/bndlib/SourceCodeAnalyzerPlugin.java b/tycho-bndlib/src/main/java/org/eclipse/tycho/bndlib/SourceCodeAnalyzerPlugin.java index 14099c9673..b31538587e 100644 --- a/tycho-bndlib/src/main/java/org/eclipse/tycho/bndlib/SourceCodeAnalyzerPlugin.java +++ b/tycho-bndlib/src/main/java/org/eclipse/tycho/bndlib/SourceCodeAnalyzerPlugin.java @@ -30,7 +30,9 @@ import org.eclipse.jdt.core.dom.ASTParser; import org.eclipse.jdt.core.dom.Annotation; import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jdt.core.dom.ImportDeclaration; import org.eclipse.jdt.core.dom.MemberValuePair; +import org.eclipse.jdt.core.dom.Name; import org.eclipse.jdt.core.dom.NormalAnnotation; import org.eclipse.jdt.core.dom.PackageDeclaration; import org.eclipse.jdt.core.dom.SingleMemberAnnotation; @@ -95,6 +97,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO if (ast instanceof CompilationUnit cu) { PackageDeclaration packageDecl = cu.getPackage(); if (packageDecl != null) { + List imports = cu.imports(); String packageFqdn = packageDecl.getName().getFullyQualifiedName(); PackageRef packageRef = analyzer.getPackageRef(packageFqdn); if (seenPackages.add(packageFqdn)) { @@ -111,12 +114,14 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO String version = null; for (Object raw : packageDecl.annotations()) { if (raw instanceof Annotation annot) { - String annotationFqdn = annot.getTypeName().getFullyQualifiedName(); - if (ANNOTATION_EXPORT.equals(annotationFqdn)) { + Name typeName = annot.getTypeName(); + String annotationFqdn = typeName.getFullyQualifiedName(); + if (isType(annotationFqdn, ANNOTATION_EXPORT, imports)) { export = true; clazz.addAnnotation( analyzer.getTypeRef(ANNOTATION_EXPORT.replace('.', '/'))); - } else if (ANNOTATION_VERSION.equals(annotationFqdn)) { + } else if (isType(annotationFqdn, ANNOTATION_VERSION, imports)) { + export = true; if (annot instanceof NormalAnnotation normal) { for (Object vp : normal.values()) { MemberValuePair pair = (MemberValuePair) vp; @@ -178,4 +183,20 @@ public Clazz getPackageInfo(PackageRef packageRef) { return packageInfoMap.get(packageRef); } + private static boolean isType(String simpleOrFqdn, String type, List imports) { + if (type.equals(simpleOrFqdn)) { + return true; + } + if (type.endsWith("." + simpleOrFqdn)) { + for (Object object : imports) { + if (object instanceof ImportDeclaration importDecl) { + if (type.equals(importDecl.getName().getFullyQualifiedName())) { + return true; + } + } + } + } + return false; + } + } diff --git a/tycho-build/pom.xml b/tycho-build/pom.xml index 0cc048f95c..87a8e662ff 100644 --- a/tycho-build/pom.xml +++ b/tycho-build/pom.xml @@ -153,5 +153,10 @@ org.osgi.util.promise 1.3.0 + + org.eclipse.tycho + tycho-bndlib + ${project.version} + \ No newline at end of file diff --git a/tycho-build/src/main/java/org/eclipse/tycho/build/bnd/BndProjectMapping.java b/tycho-build/src/main/java/org/eclipse/tycho/build/bnd/BndProjectMapping.java index a3449fe1cb..55a902e8b0 100644 --- a/tycho-build/src/main/java/org/eclipse/tycho/build/bnd/BndProjectMapping.java +++ b/tycho-build/src/main/java/org/eclipse/tycho/build/bnd/BndProjectMapping.java @@ -28,6 +28,7 @@ import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.component.annotations.Requirement; import org.eclipse.tycho.TychoConstants; +import org.eclipse.tycho.bndlib.JdtProjectBuilder; import org.eclipse.tycho.pomless.AbstractTychoMapping; import org.eclipse.tycho.pomless.NoParentPomFound; import org.eclipse.tycho.pomless.ParentModel; @@ -168,7 +169,7 @@ protected void initModel(Model model, Reader artifactReader, Path artifactFile) } private static ProjectBuilder createBuilder(Project project) throws Exception { - ProjectBuilder builder = new ProjectBuilder(project); + ProjectBuilder builder = new JdtProjectBuilder(project); builder.setBase(project.getBase()); builder.use(project); builder.setFailOk(true);