]> source.dussan.org Git - javassist.git/commitdiff
allows adding a default method to an interface type.
authorchibash <chiba@javassist.org>
Sat, 17 Sep 2016 17:32:31 +0000 (02:32 +0900)
committerchibash <chiba@javassist.org>
Sat, 17 Sep 2016 17:32:31 +0000 (02:32 +0900)
src/main/javassist/CtClassType.java
src/test/javassist/JvstTest3.java
src/test/javassist/JvstTest5.java

index 627c15cfd873f14db5736222239ad73b15bf07ba..29aace81c95cdceb65a137d96afd83afe3862317 100644 (file)
@@ -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);
index d99fde1fceedd97fc8aafe28c6b76f77a0cc6927..427479d1fe923bb08a2ce02800bca99d04e732d1 100644 (file)
@@ -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);
index 792fed6e8abbd85a838837ac20cfd457c8acd2aa..779df525a40ade9f9e43b24dc1964018399f7a42 100644 (file)
@@ -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) {}
+    }
 }