Bug 434442 - ClassFormatError when using Enums in Java 8 Default
interface methods
Signed-off-by: Jayaprakash Arthanareeswaran <jarthana@in.ibm.com>
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/AssertionTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/AssertionTest.java
index 2b601e0..9bc978c 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/AssertionTest.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/AssertionTest.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2010 IBM Corporation and others.
+ * Copyright (c) 2000, 2014 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -10,6 +10,8 @@
*******************************************************************************/
package org.eclipse.jdt.core.tests.compiler.regression;
+import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
+
import junit.framework.Test;
public class AssertionTest extends AbstractRegressionTest {
@@ -617,4 +619,21 @@
"The local variable i may not have been initialized\n" +
"----------\n");
}
+ public void test023() {
+ if (this.complianceLevel < ClassFileConstants.JDK1_8)
+ return;
+ this.runConformTest(new String[] {"X.java",
+ "interface Foo {\n" +
+ " default Object test(Object a) {\n" +
+ " assert a != null; // triggers creation of bogus synthetic field\n" +
+ " return a;\n" +
+ " }\n" +
+ "}\n" +
+ "public class X implements Foo {\n" +
+ " public static void main(String[] args) {\n" +
+ " new X().test(\"\");\n" +
+ " System.out.println(\"Hello\");\n" +
+ " }\n" +
+ "}\n"}, "Hello");
+ }
}
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/EnumTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/EnumTest.java
index a5be8b2..cc6bedb 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/EnumTest.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/EnumTest.java
@@ -7081,4 +7081,39 @@
true,
options);
}
+public void test434442() {
+ if (this.complianceLevel < ClassFileConstants.JDK1_8)
+ return;
+ this.runConformTest(new String[] {
+ "X.java",
+ "interface I {\n" +
+ " public enum Letter {\n" +
+ " A, B;\n" +
+ " }\n" +
+ " public default void test(Letter letter) {\n" +
+ " switch (letter) {\n" +
+ " case A:\n" +
+ " System.out.println(\"A\");\n" +
+ " break;\n" +
+ " case B:\n" +
+ " System.out.println(\"B\");\n" +
+ " break;\n" +
+ " }\n" +
+ " }\n" +
+ "}\n" +
+ "\n" +
+ "public class X implements I {\n" +
+ " public static void main(String[] args) {\n" +
+ " try{\n" +
+ " X x = new X();\n" +
+ " x.test(Letter.A);\n" +
+ " }\n" +
+ " catch (Exception e) {\n" +
+ " e.printStackTrace();\n" +
+ " }\n" +
+ " }\n" +
+ "} \n" +
+ "\n"
+ });
+}
}
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/SourceTypeBinding.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/SourceTypeBinding.java
index 572f703..9154426 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/SourceTypeBinding.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/SourceTypeBinding.java
@@ -371,7 +371,7 @@
synthField = new SyntheticFieldBinding(
TypeConstants.SYNTHETIC_ASSERT_DISABLED,
TypeBinding.BOOLEAN,
- ClassFileConstants.AccDefault | ClassFileConstants.AccStatic | ClassFileConstants.AccSynthetic | ClassFileConstants.AccFinal,
+ (isInterface() ? ClassFileConstants.AccPublic : ClassFileConstants.AccDefault) | ClassFileConstants.AccStatic | ClassFileConstants.AccSynthetic | ClassFileConstants.AccFinal,
this,
Constant.NotAConstant,
this.synthetics[SourceTypeBinding.FIELD_EMUL].size());
@@ -511,7 +511,7 @@
synthField = new SyntheticFieldBinding(
fieldName,
this.scope.createArrayType(TypeBinding.INT,1),
- ClassFileConstants.AccPrivate | ClassFileConstants.AccStatic | ClassFileConstants.AccSynthetic,
+ (isInterface() ? (ClassFileConstants.AccPublic | ClassFileConstants.AccFinal) : ClassFileConstants.AccPrivate) | ClassFileConstants.AccStatic | ClassFileConstants.AccSynthetic,
this,
Constant.NotAConstant,
this.synthetics[SourceTypeBinding.FIELD_EMUL].size());
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/SyntheticMethodBinding.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/SyntheticMethodBinding.java
index 78c112e..bc49d10 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/SyntheticMethodBinding.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/SyntheticMethodBinding.java
@@ -162,7 +162,7 @@
}
public SyntheticMethodBinding(FieldBinding targetField, ReferenceBinding declaringClass, TypeBinding enumBinding, char[] selector) {
- this.modifiers = ClassFileConstants.AccDefault | ClassFileConstants.AccStatic | ClassFileConstants.AccSynthetic;
+ this.modifiers = (declaringClass.isInterface() ? ClassFileConstants.AccPublic : ClassFileConstants.AccDefault) | ClassFileConstants.AccStatic | ClassFileConstants.AccSynthetic;
this.tagBits |= (TagBits.AnnotationResolved | TagBits.DeprecatedAnnotationResolved);
SourceTypeBinding declaringSourceType = (SourceTypeBinding) declaringClass;
SyntheticMethodBinding[] knownAccessMethods = declaringSourceType.syntheticMethods();