rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/tck/ProxyTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 24 Feb 2015 11:12:53 +0100
changeset 1787 ea12a3bb4b33
parent 1723 3a1f262311cf
permissions -rw-r--r--
Using year range 2012-2015 in copyright header
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.bck2brwsr.tck;
    19 
    20 import java.lang.reflect.InvocationHandler;
    21 import java.lang.reflect.Method;
    22 import java.lang.reflect.Proxy;
    23 import org.apidesign.bck2brwsr.vmtest.Compare;
    24 import org.apidesign.bck2brwsr.vmtest.VMTest;
    25 import org.testng.annotations.Factory;
    26 
    27 /**
    28  *
    29  * @author Jaroslav Tulach <jtulach@netbeans.org>
    30  */
    31 public class ProxyTest {
    32     @Compare public String generateAnnotation() throws Exception {
    33         class InvHandler implements InvocationHandler {
    34             @Override
    35             public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    36                 return "Joe Hacker";
    37             }
    38         }
    39         Anno anno = (Anno) Proxy.newProxyInstance(
    40             Anno.class.getClassLoader(), 
    41             new Class[] { Anno.class }, 
    42             new InvHandler()
    43         );
    44         return anno.name();
    45     }
    46 
    47     @Compare public int getPrimitiveType() throws Exception {
    48         class InvHandler implements InvocationHandler {
    49             @Override
    50             public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    51                 return 40;
    52             }
    53         }
    54         Anno anno = (Anno) Proxy.newProxyInstance(
    55             Anno.class.getClassLoader(), 
    56             new Class[] { Anno.class }, 
    57             new InvHandler()
    58         );
    59         return 2 + anno.age();
    60     }
    61     
    62     public static @interface Anno {
    63         public String name();
    64         public int age();
    65     }
    66     
    67     @Factory
    68     public static Object[] create() {
    69         return VMTest.create(ProxyTest.class);
    70     }
    71 }