Skip to content

Commit

Permalink
Evaluate Package Attributes from source
Browse files Browse the repository at this point in the history
  • Loading branch information
laeubi committed Feb 12, 2025
1 parent c478f65 commit ad47032
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

}
5 changes: 5 additions & 0 deletions tycho-build/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,10 @@
<artifactId>org.osgi.util.promise</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-bndlib</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit ad47032

Please sign in to comment.