From f6992d66a0d786d8872f5e0754d3270a4728e10c Mon Sep 17 00:00:00 2001 From: chibash Date: Sun, 18 Sep 2016 02:32:31 +0900 Subject: [PATCH] allows adding a default method to an interface type. --- src/main/javassist/CtClassType.java | 7 ++++--- src/test/javassist/JvstTest3.java | 3 ++- src/test/javassist/JvstTest5.java | 16 ++++++++++++++++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/main/javassist/CtClassType.java b/src/main/javassist/CtClassType.java index 627c15cf..29aace81 100644 --- a/src/main/javassist/CtClassType.java +++ b/src/main/javassist/CtClassType.java @@ -1400,10 +1400,11 @@ class CtClassType extends CtClass { int mod = m.getModifiers(); if ((getModifiers() & Modifier.INTERFACE) != 0) { - m.setModifiers(mod | Modifier.PUBLIC); - if ((mod & Modifier.ABSTRACT) == 0) + if (Modifier.isProtected(mod) || Modifier.isPrivate(mod)) throw new CannotCompileException( - "an interface method must be abstract: " + m.toString()); + "an interface method must be public: " + m.toString()); + + m.setModifiers(mod | Modifier.PUBLIC); } getMembers().addMethod(m); diff --git a/src/test/javassist/JvstTest3.java b/src/test/javassist/JvstTest3.java index d99fde1f..427479d1 100644 --- a/src/test/javassist/JvstTest3.java +++ b/src/test/javassist/JvstTest3.java @@ -1086,7 +1086,8 @@ public class JvstTest3 extends JvstTestRoot { CtMethod m3 = CtMethod.make("public void foo3() {}", cc); try { cc.addMethod(m3); - fail(); + if (ClassFile.MAJOR_VERSION < ClassFile.JAVA_8) + fail(); } catch (CannotCompileException e) { // System.out.println(e); diff --git a/src/test/javassist/JvstTest5.java b/src/test/javassist/JvstTest5.java index 792fed6e..779df525 100644 --- a/src/test/javassist/JvstTest5.java +++ b/src/test/javassist/JvstTest5.java @@ -235,4 +235,20 @@ public class JvstTest5 extends JvstTestRoot { Object obj = make(ctClass.getName()); assertEquals(1, invoke(obj, "run")); } + + public void testAddDefaultMethod() throws Exception { + CtClass cc = sloader.makeInterface("test5.AddDefaultMethod"); + cc.addMethod(CtNewMethod.make("static int foo() { return 1; }", cc)); + cc.addMethod(CtNewMethod.make("public static int foo1() { return 1; }", cc)); + cc.addMethod(CtNewMethod.make("public int foo2() { return 1; }", cc)); + cc.addMethod(CtNewMethod.make("int foo3() { return 1; }", cc)); + try { + cc.addMethod(CtNewMethod.make("private int foo4() { return 1; }", cc)); + fail(); + } catch (CannotCompileException e) {} + try { + cc.addMethod(CtNewMethod.make("private static int foo5() { return 1; }", cc)); + fail(); + } catch (CannotCompileException e) {} + } } -- 2.39.5