diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java index 9f06552..457e923 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java @@ -190,19 +190,6 @@ public class ModuleGenerator { } } - if( classFile.isEnum() ) { - //temporary Hack because the generated Enum.valueOf(String) use Reflection which currently is not supported - for( MethodInfo method : classFile.getMethods() ) { - if( "valueOf".equals( method.getName() ) && method.getType().startsWith( "(Ljava/lang/String;)" ) ) { - FunctionName valueOf = new FunctionName( method ); - String replaceForEnums = ReplacementForEnums.class.getName().replace( ".", "/" ); - ClassFile file = classFileLoader.get( replaceForEnums ); - MethodInfo method2 = file.getMethod( "valueOf_", "(Ljava/lang/String;)L" + replaceForEnums + ";" ); - functions.addReplacement( valueOf, method2 ); - break; - } - } - } iterateMethods( classFile, m -> prepareMethod( m ) ); } @@ -234,6 +221,17 @@ public class ModuleGenerator { MethodInfo method = null; ClassFile classFile = classFileLoader.get( next.className ); if( classFile != null ) { + + //temporary Hack because the generated Enum.valueOf(String) use Reflection which currently is not supported + if( classFile.isEnum() && "valueOf".equals( next.methodName ) && next.signature.startsWith( "(Ljava/lang/String;)" ) ) { + System.err.println( next.signatureName ); + String replaceForEnums = ReplacementForEnums.class.getName().replace( ".", "/" ); + ClassFile file = classFileLoader.get( replaceForEnums ); + classFileLoader.partial( next.className, file ); + MethodInfo method2 = classFile.getMethod( "valueOf_", next.signature ); + functions.addReplacement( next, method2 ); + } + sourceFile = classFile.getSourceFile(); className = classFile.getThisClass().getName(); method = classFile.getMethod( next.methodName, next.signature ); diff --git a/src/de/inetsoftware/jwebassembly/module/nativecode/ReplacementForEnums.java b/src/de/inetsoftware/jwebassembly/module/nativecode/ReplacementForEnums.java index b2db65d..51320bc 100644 --- a/src/de/inetsoftware/jwebassembly/module/nativecode/ReplacementForEnums.java +++ b/src/de/inetsoftware/jwebassembly/module/nativecode/ReplacementForEnums.java @@ -1,5 +1,5 @@ /* - Copyright 2021 Volker Berlin (i-net software) + Copyright 2021 - 2022 Volker Berlin (i-net software) Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -24,20 +24,30 @@ import java.util.Map; * * @author Volker Berlin */ -public enum ReplacementForEnums { - ; - private transient Map enumConstantDirectory; +public abstract class ReplacementForEnums { + + private static transient Map enumConstantDirectory; + + /** + * Placeholder for existing public method. + */ + public native String name(); + + /** + * Placeholder for existing public method. + */ + public native static ReplacementForEnums[] values(); /** * Replacement code for generated Enum.valueOf( String ) * @param name the enum name * @return The singleton instance for the name */ - ReplacementForEnums valueOf_( String name ) { + static ReplacementForEnums valueOf_( String name ) { Map map = enumConstantDirectory; if( map == null ) { ReplacementForEnums[] universe = values(); - map = new HashMap<>( 2 * universe.length ); + map = new HashMap<>( universe.length ); for( ReplacementForEnums constant : universe ) { map.put( constant.name(), constant ); } @@ -51,6 +61,6 @@ public enum ReplacementForEnums { if( name == null ) { throw new NullPointerException( "Name is null" ); } - throw new IllegalArgumentException( "No enum constant " + getClass().getCanonicalName() + "." + name ); + throw new IllegalArgumentException( "No enum constant " + ReplacementForEnums.class + "." + name ); } }