Skip to content

Commit

Permalink
8263910: Java.extend throws java.lang.ClassFormatError (#13)
Browse files Browse the repository at this point in the history
* Handle duplicate types, handle too many interfaces. Don't cache error results for multiple types

* Simplify the adaptation result, it was overcomplicated due to caching that autoConvertibleFromFunction field.

* Lazily create AdapterInfo.instanceAdapters map.
  • Loading branch information
szegedi committed Mar 24, 2021
1 parent 9dad101 commit a7bbe60
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 172 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import static org.openjdk.nashorn.internal.codegen.CompilerConstants.interfaceCallNoLookup;
import static org.openjdk.nashorn.internal.codegen.CompilerConstants.staticCallNoLookup;
import static org.openjdk.nashorn.internal.lookup.Lookup.MH;
import static org.openjdk.nashorn.internal.runtime.linker.AdaptationResult.Outcome.ERROR_NO_ACCESSIBLE_CONSTRUCTOR;

import java.lang.annotation.Annotation;
import java.lang.invoke.CallSite;
Expand Down Expand Up @@ -73,7 +72,6 @@
import org.openjdk.nashorn.internal.codegen.CompilerConstants.Call;
import org.openjdk.nashorn.internal.runtime.ScriptFunction;
import org.openjdk.nashorn.internal.runtime.ScriptObject;
import org.openjdk.nashorn.internal.runtime.linker.AdaptationResult.Outcome;

/**
* Generates bytecode for a Java adapter class. Used by the {@link JavaAdapterFactory}.
Expand Down Expand Up @@ -249,10 +247,9 @@ final class JavaAdapterBytecodeGenerator {
* @param commonLoader the class loader that can see all of superClass, interfaces, and Nashorn classes.
* @param classOverride true to generate the bytecode for the adapter that has class-level overrides, false to
* generate the bytecode for the adapter that has instance-level overrides.
* @throws AdaptationException if the adapter can not be generated for some reason.
*/
JavaAdapterBytecodeGenerator(final Class<?> superClass, final List<Class<?>> interfaces,
final ClassLoader commonLoader, final boolean classOverride) throws AdaptationException {
final ClassLoader commonLoader, final boolean classOverride) {
assert superClass != null && !superClass.isInterface();
assert interfaces != null;

Expand Down Expand Up @@ -383,7 +380,7 @@ private void emitInitCallThis(final InstructionAdapter mv) {
}
}

private boolean generateConstructors() throws AdaptationException {
private boolean generateConstructors() {
boolean gotCtor = false;
boolean canBeAutoConverted = false;
for (final Constructor<?> ctor: superClass.getDeclaredConstructors()) {
Expand All @@ -394,7 +391,8 @@ private boolean generateConstructors() throws AdaptationException {
}
}
if(!gotCtor) {
throw new AdaptationException(ERROR_NO_ACCESSIBLE_CONSTRUCTOR, superClass.getCanonicalName());
throw JavaAdapterFactory.adaptationException(
JavaAdapterFactory.ErrorOutcome.NO_ACCESSIBLE_CONSTRUCTOR, superClass.getCanonicalName());
}
return canBeAutoConverted;
}
Expand All @@ -407,9 +405,9 @@ private boolean generateConstructors(final Constructor<?> ctor) {
return false;
}

// Generate a constructor that delegates to ctor, but takes an additional ScriptObject parameter at the
// beginning of its parameter list.
generateOverridingConstructor(ctor, false);
// Generate a constructor that delegates to ctor, but takes an additional ScriptObject parameter at the
// beginning of its parameter list.
generateOverridingConstructor(ctor, false);

if (samName == null) {
return false;
Expand Down Expand Up @@ -1123,7 +1121,7 @@ private static int getAccessModifiers(final Method method) {
* class.
* @param type the type defining the methods.
*/
private void gatherMethods(final Class<?> type) throws AdaptationException {
private void gatherMethods(final Class<?> type) {
if (Modifier.isPublic(type.getModifiers())) {
final Method[] typeMethods = type.isInterface() ? type.getMethods() : type.getDeclaredMethods();

Expand All @@ -1143,7 +1141,8 @@ private void gatherMethods(final Class<?> type) throws AdaptationException {
hasExplicitFinalizer = true;
if(Modifier.isFinal(m)) {
// Must be able to override an explicit finalizer
throw new AdaptationException(Outcome.ERROR_FINAL_FINALIZER, type.getCanonicalName());
throw JavaAdapterFactory.adaptationException(
JavaAdapterFactory.ErrorOutcome.FINAL_FINALIZER, type.getCanonicalName());
}
}
continue;
Expand Down Expand Up @@ -1174,7 +1173,7 @@ private void gatherMethods(final Class<?> type) throws AdaptationException {
}
}

private void gatherMethods(final List<Class<?>> classes) throws AdaptationException {
private void gatherMethods(final List<Class<?>> classes) {
for(final Class<?> c: classes) {
gatherMethods(c);
}
Expand Down
Loading

0 comments on commit a7bbe60

Please sign in to comment.