Search in sources :

Example 1 with Clazzes

use of org.robovm.compiler.clazz.Clazzes in project robovm by robovm.

the class AppCompilerTest method testMultilineFile.

@Test
public void testMultilineFile() throws Exception {
    final Path impl1 = new MockPath("META-INF/services/java.lang.Number", "# first register Integer\n" + "java.lang.Integer\n" + "# then add Long\n" + "java.lang.Long\n" + "\n\n\n\n");
    Clazzes clazzes = createClazzes(impl1);
    Clazz interfaceClazz = clazzes.load("java/lang/Number");
    Set<Clazz> compiled = new HashSet<>();
    Set<Clazz> queue = new LinkedHashSet<>();
    AppCompiler.addMetaInfImplementations(clazzes, interfaceClazz, compiled, queue);
    assertEquals("Two items added to queue: " + queue, 2, queue.size());
    assertTrue("Integer in queue" + queue, queue.contains(clazzes.load("java/lang/Integer")));
    assertTrue("Long in queue" + queue, queue.contains(clazzes.load("java/lang/Long")));
}
Also used : Path(org.robovm.compiler.clazz.Path) LinkedHashSet(java.util.LinkedHashSet) Clazz(org.robovm.compiler.clazz.Clazz) Clazzes(org.robovm.compiler.clazz.Clazzes) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Test(org.junit.Test)

Example 2 with Clazzes

use of org.robovm.compiler.clazz.Clazzes in project robovm by robovm.

the class AppCompilerTest method allStreamsAreClosedInCaseOfFailure.

@Test
public void allStreamsAreClosedInCaseOfFailure() throws Exception {
    final MockPath impl1 = new MockPath("META-INF/services/java.lang.Number", "java.lang.Integer");
    impl1.toThrow = new IOException();
    final MockPath impl2 = new MockPath("META-INF/services/java.lang.Number", "nobody.knows.such.Class");
    Clazzes clazzes = createClazzes(impl1, impl2);
    Clazz interfaceClazz = clazzes.load("java/lang/Number");
    Set<Clazz> compiled = new HashSet<>();
    Set<Clazz> queue = new LinkedHashSet<>();
    try {
        AppCompiler.addMetaInfImplementations(clazzes, interfaceClazz, compiled, queue);
        fail("Should throw an exception");
    } catch (IOException ex) {
        assertSame("Our exception is thrown", impl1.toThrow, ex);
    }
    assertTrue("First stream is closed", impl1.closed);
    assertTrue("Second stream is closed", impl2.closed);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Clazz(org.robovm.compiler.clazz.Clazz) IOException(java.io.IOException) Clazzes(org.robovm.compiler.clazz.Clazzes) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Test(org.junit.Test)

Example 3 with Clazzes

use of org.robovm.compiler.clazz.Clazzes in project robovm by robovm.

the class AppCompilerTest method testMetainfServiceImplIsAdded.

@Test
public void testMetainfServiceImplIsAdded() throws Exception {
    final Path impl1 = new MockPath("META-INF/services/java.lang.Number", "java.lang.Integer");
    Clazzes clazzes = createClazzes(impl1);
    Clazz interfaceClazz = clazzes.load("java/lang/Number");
    Set<Clazz> compiled = new HashSet<>();
    Set<Clazz> queue = new LinkedHashSet<>();
    AppCompiler.addMetaInfImplementations(clazzes, interfaceClazz, compiled, queue);
    assertEquals("One item added to queue: " + queue, 1, queue.size());
    assertTrue("Integer in queue" + queue, queue.contains(clazzes.load("java/lang/Integer")));
}
Also used : Path(org.robovm.compiler.clazz.Path) LinkedHashSet(java.util.LinkedHashSet) Clazz(org.robovm.compiler.clazz.Clazz) Clazzes(org.robovm.compiler.clazz.Clazzes) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Test(org.junit.Test)

Example 4 with Clazzes

use of org.robovm.compiler.clazz.Clazzes in project robovm by robovm.

the class AppCompilerTest method createClazzes.

private static Clazzes createClazzes(final Path... paths) throws Exception {
    File home = new File(System.getProperty("java.home"));
    Config cfg = new Config() {
    };
    Clazzes clazzes = new Clazzes(cfg, Collections.nCopies(1, new File(new File(home, "lib"), "rt.jar")), Collections.<File>emptyList()) {

        @Override
        public List<Path> getPaths() {
            return Arrays.asList(paths);
        }
    };
    return clazzes;
}
Also used : Path(org.robovm.compiler.clazz.Path) Config(org.robovm.compiler.config.Config) Clazzes(org.robovm.compiler.clazz.Clazzes) File(java.io.File)

Example 5 with Clazzes

use of org.robovm.compiler.clazz.Clazzes in project robovm by robovm.

the class ObjCProtocolProxyPlugin method generateProxyMethods.

private void generateProxyMethods(Config config, List<String> interfazes, ClassWriter cw) throws IOException {
    Clazzes clazzes = config.getClazzes();
    final Set<String> addedMethods = new HashSet<>();
    for (String interfaze : interfazes) {
        Clazz clazz = clazzes.load(interfaze);
        if (clazz == null) {
            continue;
        }
        // Copy all abstract method (we skip default methods) to the proxy 
        // and make them native instead of abstract.
        ClassReader classReader = new ClassReader(clazz.getBytes());
        classReader.accept(new ClassVisitor(ASM4, cw) {

            @Override
            public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
                String key = name + desc;
                if ((access & ACC_ABSTRACT) > 0 && !addedMethods.contains(key)) {
                    access &= ~ACC_ABSTRACT;
                    access |= ACC_NATIVE;
                    addedMethods.add(key);
                    return super.visitMethod(access, name, desc, signature, exceptions);
                }
                return null;
            }

            @Override
            public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
            // Ignored
            }

            @Override
            public void visitEnd() {
            // Ignored
            }

            @Override
            public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
                // Ignored
                return null;
            }

            @Override
            public void visitAttribute(Attribute attr) {
            // Ignored
            }

            @Override
            public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
                // Ignored
                return null;
            }

            @Override
            public void visitInnerClass(String name, String outerName, String innerName, int access) {
            // Ignored
            }

            @Override
            public void visitOuterClass(String owner, String name, String desc) {
            // Ignored
            }

            @Override
            public void visitSource(String source, String debug) {
            // Ignored
            }
        }, 0);
    }
}
Also used : Attribute(org.objectweb.asm.Attribute) ClassVisitor(org.objectweb.asm.ClassVisitor) FieldVisitor(org.objectweb.asm.FieldVisitor) MethodVisitor(org.objectweb.asm.MethodVisitor) AnnotationVisitor(org.objectweb.asm.AnnotationVisitor) ClassReader(org.objectweb.asm.ClassReader) Clazz(org.robovm.compiler.clazz.Clazz) Clazzes(org.robovm.compiler.clazz.Clazzes) HashSet(java.util.HashSet)

Aggregations

Clazzes (org.robovm.compiler.clazz.Clazzes)8 HashSet (java.util.HashSet)6 Clazz (org.robovm.compiler.clazz.Clazz)6 LinkedHashSet (java.util.LinkedHashSet)5 Test (org.junit.Test)5 Path (org.robovm.compiler.clazz.Path)5 File (java.io.File)2 IOException (java.io.IOException)1 JarFile (java.util.jar.JarFile)1 ZipFile (java.util.zip.ZipFile)1 AnnotationVisitor (org.objectweb.asm.AnnotationVisitor)1 Attribute (org.objectweb.asm.Attribute)1 ClassReader (org.objectweb.asm.ClassReader)1 ClassVisitor (org.objectweb.asm.ClassVisitor)1 FieldVisitor (org.objectweb.asm.FieldVisitor)1 MethodVisitor (org.objectweb.asm.MethodVisitor)1 DependencyGraph (org.robovm.compiler.DependencyGraph)1 ITable (org.robovm.compiler.ITable)1 MarshalerLookup (org.robovm.compiler.MarshalerLookup)1 VTable (org.robovm.compiler.VTable)1