Search in sources :

Example 1 with Class

use of separate.SourceModel.Class in project jdk8u_jdk by JetBrains.

the class Compiler method compileHierarchy.

/**
     * Compiles a hierarchy, starting at 'type' and return a mapping of the
     * name to the location where the classfile for that type resides.
     */
private Map<String, File> compileHierarchy(Type type) {
    HashMap<String, File> outputDirs = new HashMap<>();
    File outDir = compileOne(type);
    outputDirs.put(type.getName(), outDir);
    Class superClass = type.getSuperclass();
    if (superClass != null) {
        for (Map.Entry<String, File> each : compileHierarchy(superClass).entrySet()) {
            outputDirs.put(each.getKey(), each.getValue());
        }
    }
    for (Extends ext : type.getSupertypes()) {
        Type iface = ext.getType();
        for (Map.Entry<String, File> each : compileHierarchy(iface).entrySet()) {
            outputDirs.put(each.getKey(), each.getValue());
        }
    }
    return outputDirs;
}
Also used : Type(separate.SourceModel.Type) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Class(separate.SourceModel.Class) Extends(separate.SourceModel.Extends) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 2 with Class

use of separate.SourceModel.Class in project jdk8u_jdk by JetBrains.

the class TestHarness method assertInvokeInterfaceEquals.

/**
     * Creates a class which calls target::method(args) via invokeinterface
     * through 'iface', compiles and loads both it and 'target', and
     * then invokes the method.  If the returned value does not match
     * 'value' then a test failure is indicated.
     */
public void assertInvokeInterfaceEquals(Object value, Class target, Extends iface, AbstractMethod method, String... args) {
    Compiler compiler = compilerLocal.get();
    compiler.setFlags(compilerFlags());
    Class ii = invokeInterfaceHarness(target, iface, method, args);
    ClassLoader loader = compiler.compile(ii, target);
    assertStaticCallEquals(loader, ii, method.getName(), value);
    compiler.cleanup();
}
Also used : Class(separate.SourceModel.Class)

Example 3 with Class

use of separate.SourceModel.Class in project jdk8u_jdk by JetBrains.

the class TestHarness method invokeInterfaceHarness.

/**
     * Returns a class which has a static method with the same name as
     * 'method', whose body creates an new instance of 'specimen', casts it
     * to 'iface' (including the type parameters)  and invokes
     * 'method' upon it via an invokeinterface instruction with 'args' as
     * function call parameters.
     */
private Class invokeInterfaceHarness(Class specimen, Extends iface, AbstractMethod method, String... args) {
    Interface istub = new Interface(iface.getType().getName(), iface.getType().getAccessFlags(), iface.getType().getParameters(), null, Arrays.asList((Method) method));
    Class cstub = new Class(specimen.getName());
    String params = Arrays.asList(args).stream().collect(Collectors.joining(", ")).toString();
    ConcreteMethod sm = new ConcreteMethod("int", SourceModel.stdMethodName, String.format("return ((%s)(new %s())).%s(%s);", iface.toString(), specimen.getName(), method.getName(), params), new AccessFlag("public"), new AccessFlag("static"));
    sm.suppressWarnings();
    Class ii = new Class("II_" + specimen.getName() + "_" + iface.getType().getName(), sm);
    ii.addCompilationDependency(istub);
    ii.addCompilationDependency(cstub);
    ii.addCompilationDependency(method);
    return ii;
}
Also used : Class(separate.SourceModel.Class) AfterMethod(org.testng.annotations.AfterMethod)

Example 4 with Class

use of separate.SourceModel.Class in project jdk8u_jdk by JetBrains.

the class InterfaceAccessFlagsTest method testMethodCallWithFlag.

public void testMethodCallWithFlag(AccessFlag... flags) {
    Class I = new Class("I", new ConcreteMethod("int", "m", "return priv();", AccessFlag.PUBLIC), new ConcreteMethod("int", "priv", "return 99;", flags));
    Interface Istub = new Interface("I", new DefaultMethod("int", "m", "return 0;"));
    Class C = new Class("C", Istub, new ConcreteMethod("int", "foo", "return (new C()).m();", AccessFlag.PUBLIC, AccessFlag.STATIC));
    C.addCompilationDependency(Istub.findMethod("m"));
    Compiler compiler = new Compiler();
    compiler.addPostprocessor(new ClassToInterfaceConverter("I"));
    // Turns I from a class into an interface when loading
    ClassLoader cl = compiler.compile(C, I);
    try {
        java.lang.Class<?> C_class = java.lang.Class.forName("C", true, cl);
        assertNotNull(C_class);
        java.lang.reflect.Method m = C_class.getMethod("foo");
        assertNotNull(m);
        Integer res = (Integer) m.invoke(null);
        assertEquals(res.intValue(), 99);
    } catch (java.lang.reflect.InvocationTargetException e) {
        fail("Unexpected exception: " + e.getCause());
    } catch (Throwable e) {
        fail("Unexpected exception: " + e);
    } finally {
        compiler.cleanup();
    }
}
Also used : Compiler(separate.Compiler) Class(separate.SourceModel.Class)

Example 5 with Class

use of separate.SourceModel.Class in project jdk8u_jdk by JetBrains.

the class TestHarness method assertThrows.

/**
     * Creates a class which calls target::method(args) via invokevirtual,
     * compiles and loads both the new class and 'target', and then invokes
     * the method.  If an exception of type 'exceptionType' is not thrown,
     * then a test failure is indicated.
     */
public void assertThrows(java.lang.Class<?> exceptionType, Class target, ConcreteMethod method, String returns, String... args) {
    Compiler compiler = compilerLocal.get();
    compiler.setFlags(compilerFlags());
    Class iv = invokeVirtualHarness(target, method, returns, args);
    ClassLoader loader = compiler.compile(iv, target);
    java.lang.Class<?> cls = null;
    try {
        cls = java.lang.Class.forName(iv.getName(), true, loader);
    } catch (ClassNotFoundException e) {
    }
    assertNotNull(cls);
    java.lang.reflect.Method m = null;
    try {
        m = cls.getMethod(method.getName());
    } catch (NoSuchMethodException e) {
    }
    assertNotNull(m);
    try {
        m.invoke(null);
        fail("Exception should have been thrown");
    } catch (InvocationTargetException | IllegalAccessException e) {
        if (verboseLocal.get() == Boolean.TRUE) {
            System.out.println(e.getCause());
        }
        assertEquals(e.getCause().getClass(), exceptionType);
    }
    compiler.cleanup();
}
Also used : InvocationTargetException(java.lang.reflect.InvocationTargetException) Class(separate.SourceModel.Class)

Aggregations

Class (separate.SourceModel.Class)7 AfterMethod (org.testng.annotations.AfterMethod)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Compiler (separate.Compiler)1 Extends (separate.SourceModel.Extends)1 Type (separate.SourceModel.Type)1