use of org.eclipse.ceylon.langtools.classfile.Annotation in project ceylon by eclipse.
the class BytecodeUtils method getDependencies.
private static Set<ModuleDependencyInfo> getDependencies(ClassFile moduleInfo, Object[] dependencies, String module, String version, String groupId, String artifactId, Overrides overrides) {
if (dependencies == null) {
return Collections.<ModuleDependencyInfo>emptySet();
}
int[] binver = getBinaryVersions(moduleInfo);
boolean supportsNamespaces = binver != null && ModuleUtil.supportsImportsWithNamespaces(binver[0], binver[1]);
Set<ModuleDependencyInfo> result = new HashSet<ModuleDependencyInfo>(dependencies.length);
for (Object depObject : dependencies) {
Annotation dep = (Annotation) depObject;
String namespace;
String modName = (String) ClassFileUtil.getAnnotationValue(moduleInfo, dep, "name");
if (supportsNamespaces) {
namespace = (String) ClassFileUtil.getAnnotationValue(moduleInfo, dep, "namespace");
if (namespace != null && namespace.isEmpty()) {
namespace = null;
}
} else {
if (ModuleUtil.isMavenModule(modName)) {
namespace = MavenRepository.NAMESPACE;
} else {
namespace = null;
}
}
String depVersion = (String) ClassFileUtil.getAnnotationValue(moduleInfo, dep, "version");
boolean export = asBoolean(moduleInfo, dep, "export");
boolean optional = asBoolean(moduleInfo, dep, "optional");
Backends backends = Backends.ANY;
Object[] backendNames = (Object[]) ClassFileUtil.getAnnotationValue(moduleInfo, dep, "dependencies");
if (backendNames != null) {
for (Object backend : backendNames) {
backends = backends.merged(Backend.fromAnnotation((String) backend));
}
}
result.add(new ModuleDependencyInfo(namespace, modName, depVersion, optional, export, backends));
}
if (overrides != null) {
result = overrides.applyOverrides(module, version, new ModuleInfo(null, module, version, groupId, artifactId, null, null, result)).getDependencies();
}
return result;
}
use of org.eclipse.ceylon.langtools.classfile.Annotation in project ceylon by eclipse.
the class BytecodeUtils method matchesModuleInfo.
public boolean matchesModuleInfo(String moduleName, String moduleVersion, File moduleArchive, String query, Overrides overrides) {
ClassFile moduleInfo = readModuleInfo(moduleName, moduleArchive);
if (moduleInfo == null)
return false;
Annotation moduleAnnotation = ClassFileUtil.findAnnotation(moduleInfo, MODULE_ANNOTATION);
if (moduleAnnotation == null)
return false;
String groupId, artifactId;
groupId = (String) ClassFileUtil.getAnnotationValue(moduleInfo, moduleAnnotation, "group");
if (groupId == null || groupId.isEmpty()) {
String[] coordinates = ModuleUtil.getMavenCoordinates(moduleName);
groupId = coordinates[0];
artifactId = coordinates[1];
} else {
artifactId = (String) ClassFileUtil.getAnnotationValue(moduleInfo, moduleAnnotation, "artifact");
if (artifactId == null || artifactId.isEmpty())
artifactId = moduleName;
}
String version = (String) ClassFileUtil.getAnnotationValue(moduleInfo, moduleAnnotation, "version");
if (version == null)
return false;
String doc = (String) ClassFileUtil.getAnnotationValue(moduleInfo, moduleAnnotation, "doc");
if (doc != null && matches(doc, query))
return true;
String label = (String) ClassFileUtil.getAnnotationValue(moduleInfo, moduleAnnotation, "label");
if (label != null && matches(label, query))
return true;
String license = (String) ClassFileUtil.getAnnotationValue(moduleInfo, moduleAnnotation, "license");
if (license != null && matches(license, query))
return true;
Object[] by = (Object[]) ClassFileUtil.getAnnotationValue(moduleInfo, moduleAnnotation, "by");
if (by != null) {
for (Object author : by) {
if (matches((String) author, query))
return true;
}
}
Object[] dependencies = (Object[]) ClassFileUtil.getAnnotationValue(moduleInfo, moduleAnnotation, "dependencies");
if (dependencies != null) {
for (ModuleDependencyInfo dep : getDependencies(moduleInfo, dependencies, moduleName, version, groupId, artifactId, overrides)) {
if (matches(dep.getModuleName(), query))
return true;
}
}
return false;
}
use of org.eclipse.ceylon.langtools.classfile.Annotation in project ceylon by eclipse.
the class ClassFileUtilTest method test.
@Test
public void test() throws IOException, ConstantPoolException {
String location = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
File f = new File(location);
String classPath = ClassFileUtilTest.class.getName().replace('.', '/') + ".class";
ClassFile classFile;
if (f.isDirectory()) {
// folder
File klassFile = new File(f, classPath);
classFile = ClassFile.read(klassFile);
} else {
// zip
try (ZipFile zf = new ZipFile(f)) {
ZipEntry entry = zf.getEntry(classPath);
try (InputStream is = zf.getInputStream(entry)) {
classFile = ClassFile.read(is);
}
}
}
RuntimeVisibleAnnotations_attribute attribute = (RuntimeVisibleAnnotations_attribute) classFile.getAttribute(Attribute.RuntimeVisibleAnnotations);
Annotation annotation = ClassFileUtil.findAnnotation(classFile, attribute, TestInterface.class);
Assert.assertNotNull(annotation);
Assert.assertEquals(true, ClassFileUtil.getAnnotationValue(classFile, annotation, "bool"));
Assert.assertEquals((byte) 1, ClassFileUtil.getAnnotationValue(classFile, annotation, "b"));
Assert.assertEquals((short) 2, ClassFileUtil.getAnnotationValue(classFile, annotation, "s"));
Assert.assertEquals((int) 3, ClassFileUtil.getAnnotationValue(classFile, annotation, "i"));
Assert.assertEquals((long) 4, ClassFileUtil.getAnnotationValue(classFile, annotation, "l"));
Assert.assertEquals('a', ClassFileUtil.getAnnotationValue(classFile, annotation, "c"));
Assert.assertEquals((float) 5.0, ClassFileUtil.getAnnotationValue(classFile, annotation, "f"));
Assert.assertEquals((double) 6.0, ClassFileUtil.getAnnotationValue(classFile, annotation, "d"));
Assert.assertEquals("str", ClassFileUtil.getAnnotationValue(classFile, annotation, "string"));
Assert.assertArrayEquals(new Object[] { "a", "b" }, (Object[]) ClassFileUtil.getAnnotationValue(classFile, annotation, "array"));
Annotation subAnnotation = (Annotation) ClassFileUtil.getAnnotationValue(classFile, annotation, "annot");
Assert.assertNotNull(subAnnotation);
Assert.assertEquals("annot", ClassFileUtil.getAnnotationValue(classFile, subAnnotation, "val"));
}
use of org.eclipse.ceylon.langtools.classfile.Annotation in project ceylon by eclipse.
the class BytecodeUtils method readModuleInfo.
@Override
public ModuleVersionDetails readModuleInfo(String moduleName, String moduleVersion, File moduleArchive, boolean includeMembers, Overrides overrides) {
ClassFile moduleInfo = readModuleInfo(moduleName, moduleArchive);
if (moduleInfo == null)
return null;
Annotation moduleAnnotation = ClassFileUtil.findAnnotation(moduleInfo, MODULE_ANNOTATION);
if (moduleAnnotation == null)
return null;
String doc = (String) ClassFileUtil.getAnnotationValue(moduleInfo, moduleAnnotation, "doc");
String license = (String) ClassFileUtil.getAnnotationValue(moduleInfo, moduleAnnotation, "license");
String label = (String) ClassFileUtil.getAnnotationValue(moduleInfo, moduleAnnotation, "label");
Object[] by = (Object[]) ClassFileUtil.getAnnotationValue(moduleInfo, moduleAnnotation, "by");
Object[] dependencies = (Object[]) ClassFileUtil.getAnnotationValue(moduleInfo, moduleAnnotation, "dependencies");
String type = ArtifactContext.getSuffixFromFilename(moduleArchive.getName());
int[] binver = getBinaryVersions(moduleInfo);
String groupId, artifactId;
groupId = (String) ClassFileUtil.getAnnotationValue(moduleInfo, moduleAnnotation, "group");
if (groupId == null || groupId.isEmpty()) {
String[] coordinates = ModuleUtil.getMavenCoordinates(moduleName);
groupId = coordinates[0];
artifactId = coordinates[1];
} else {
artifactId = (String) ClassFileUtil.getAnnotationValue(moduleInfo, moduleAnnotation, "artifact");
if (artifactId == null || artifactId.isEmpty())
artifactId = moduleName;
}
ModuleVersionDetails mvd = new ModuleVersionDetails(moduleName, getVersionFromFilename(moduleName, moduleArchive.getName()), groupId, artifactId);
mvd.setDoc(doc);
mvd.setLabel(label);
mvd.setLicense(license);
if (by != null) {
for (Object author : by) {
mvd.getAuthors().add((String) author);
}
}
mvd.getDependencies().addAll(getDependencies(moduleInfo, dependencies, moduleName, mvd.getVersion(), groupId, artifactId, overrides));
ModuleVersionArtifact mva = new ModuleVersionArtifact(type, binver[0], binver[1]);
mvd.getArtifactTypes().add(mva);
if (includeMembers) {
mvd.setMembers(getMembers(moduleArchive));
}
return mvd;
}
use of org.eclipse.ceylon.langtools.classfile.Annotation in project ceylon by eclipse.
the class BytecodeUtils method getBinaryVersions.
public static int[] getBinaryVersions(ClassFile moduleInfo) {
if (moduleInfo == null)
return null;
Annotation ceylonAnnotation = ClassFileUtil.findAnnotation(moduleInfo, CEYLON_ANNOTATION);
if (ceylonAnnotation == null)
return null;
int major = asInt(moduleInfo, ceylonAnnotation, "major");
int minor = asInt(moduleInfo, ceylonAnnotation, "minor");
return new int[] { major, minor };
}
Aggregations