diff options
author | chibash <chiba@javassist.org> | 2016-02-22 02:05:06 +0900 |
---|---|---|
committer | chibash <chiba@javassist.org> | 2016-02-22 02:05:06 +0900 |
commit | 7314726c370b605b1e667157cbbe21a664d3ccca (patch) | |
tree | 806876b47fc37cad1cfd017fb331115a32bd1481 /src | |
parent | abdeecb5810a8a153c54b4f8339077241f639ef7 (diff) | |
download | javassist-7314726c370b605b1e667157cbbe21a664d3ccca.tar.gz javassist-7314726c370b605b1e667157cbbe21a664d3ccca.zip |
updates javassist.util.proxy to support a default interface method in Java 8
Diffstat (limited to 'src')
-rw-r--r-- | src/main/javassist/bytecode/ClassFile.java | 14 | ||||
-rw-r--r-- | src/main/javassist/util/proxy/ProxyFactory.java | 3 | ||||
-rw-r--r-- | src/test/javassist/proxyfactory/ProxyFactoryTest.java | 39 |
3 files changed, 50 insertions, 6 deletions
diff --git a/src/main/javassist/bytecode/ClassFile.java b/src/main/javassist/bytecode/ClassFile.java index 9e12c08d..887ec7a3 100644 --- a/src/main/javassist/bytecode/ClassFile.java +++ b/src/main/javassist/bytecode/ClassFile.java @@ -126,19 +126,25 @@ public final class ClassFile { * if the JVM supports <code>java.util.zip.DeflaterInputStream</code>. * It is 51 (JDK 1.7) * if the JVM supports <code>java.lang.invoke.CallSite</code>. + * It is 52 (JDK 1.8) + * if the JVM supports <code>java.util.function.Function</code>. */ - public static int MAJOR_VERSION = JAVA_3; + public static final int MAJOR_VERSION; static { + int ver = JAVA_3; try { Class.forName("java.lang.StringBuilder"); - MAJOR_VERSION = JAVA_5; + ver = JAVA_5; Class.forName("java.util.zip.DeflaterInputStream"); - MAJOR_VERSION = JAVA_6; + ver = JAVA_6; Class.forName("java.lang.invoke.CallSite"); - MAJOR_VERSION = JAVA_7; + ver = JAVA_7; + Class.forName("java.util.function.Function"); + ver = JAVA_8; } catch (Throwable t) {} + MAJOR_VERSION = ver; } /** diff --git a/src/main/javassist/util/proxy/ProxyFactory.java b/src/main/javassist/util/proxy/ProxyFactory.java index 5251b4be..26c5c6c9 100644 --- a/src/main/javassist/util/proxy/ProxyFactory.java +++ b/src/main/javassist/util/proxy/ProxyFactory.java @@ -1243,7 +1243,8 @@ public class ProxyFactory { Bytecode code = new Bytecode(cp, 0, 0); code.addAload(0); int s = addLoadParameters(code, meth.getParameterTypes(), 1); - code.addInvokespecial(declClass.getName(), meth.getName(), desc); + code.addInvokespecial(declClass.isInterface(), cp.addClassInfo(declClass.getName()), + meth.getName(), desc); addReturn(code, meth.getReturnType()); code.setMaxLocals(++s); delegator.setCodeAttribute(code.toCodeAttribute()); diff --git a/src/test/javassist/proxyfactory/ProxyFactoryTest.java b/src/test/javassist/proxyfactory/ProxyFactoryTest.java index 232a0165..5b1bb2ef 100644 --- a/src/test/javassist/proxyfactory/ProxyFactoryTest.java +++ b/src/test/javassist/proxyfactory/ProxyFactoryTest.java @@ -87,5 +87,42 @@ public class ProxyFactoryTest extends TestCase { public interface JIRA127Sub extends JIRA127 { JIRA127Sub get(); } - + + public void testDefaultMethod() throws Exception { + ProxyFactory proxyFactory = new ProxyFactory(); + //proxyFactory.writeDirectory = "./dump"; + proxyFactory.setInterfaces(new Class[]{ TestDefaultI.class }); + Class intf = proxyFactory.createClass(); + TestDefaultI obj = (TestDefaultI)intf.newInstance(); + obj.foo(); + + ProxyFactory proxyFactory2 = new ProxyFactory(); + //proxyFactory2.writeDirectory = "./dump"; + proxyFactory2.setSuperclass(TestDefaultC.class); + Class clazz2 = proxyFactory2.createClass(); + TestDefaultC obj2 = (TestDefaultC)clazz2.newInstance(); + obj2.foo(); + obj2.bar(); + + ProxyFactory proxyFactory3 = new ProxyFactory(); + proxyFactory3.setSuperclass(TestDefaultC2.class); + Class clazz3 = proxyFactory3.createClass(); + TestDefaultC2 obj3 = (TestDefaultC2)clazz3.newInstance(); + obj3.foo(); + obj3.bar(); + obj3.baz(); + } + + public static interface TestDefaultI { + default int foo() { return 10; } + } + + public static class TestDefaultC implements TestDefaultI { + public int foo() { return 1; } + public int bar() { return TestDefaultI.super.foo(); } + } + + public static class TestDefaultC2 extends TestDefaultC { + public int baz() { return super.foo(); } + } } |