hack for Enum.valueOf(String)

This commit is contained in:
Volker Berlin 2021-08-29 13:44:29 +02:00
parent 76b84e3eee
commit 0ef00ba2ab
2 changed files with 71 additions and 0 deletions

View File

@ -42,6 +42,7 @@ import de.inetsoftware.jwebassembly.JWebAssembly;
import de.inetsoftware.jwebassembly.WasmException;
import de.inetsoftware.jwebassembly.javascript.JavaScriptWriter;
import de.inetsoftware.jwebassembly.module.TypeManager.StructType;
import de.inetsoftware.jwebassembly.module.nativecode.ReplacementForEnums;
import de.inetsoftware.jwebassembly.wasm.AnyType;
import de.inetsoftware.jwebassembly.wasm.FunctionType;
import de.inetsoftware.jwebassembly.wasm.ValueType;
@ -185,6 +186,20 @@ 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 ) );
}

View File

@ -0,0 +1,56 @@
/*
Copyright 2021 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.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package de.inetsoftware.jwebassembly.module.nativecode;
import java.util.HashMap;
import java.util.Map;
/**
* Replacement for java.lang.Enum
*
* @author Volker Berlin
*/
public enum ReplacementForEnums {
;
private transient Map<String, ReplacementForEnums> enumConstantDirectory;
/**
* Replacement code for generated Enum.valueOf( String )
* @param name the enum name
* @return The singleton instance for the name
*/
ReplacementForEnums valueOf_( String name ) {
Map<String, ReplacementForEnums> map = enumConstantDirectory;
if( map == null ) {
ReplacementForEnums[] universe = values();
map = new HashMap<>( 2 * universe.length );
for( ReplacementForEnums constant : universe ) {
map.put( constant.name(), constant );
}
enumConstantDirectory = map;
}
ReplacementForEnums result = map.get( name );
if( result != null ) {
return result;
}
if( name == null ) {
throw new NullPointerException( "Name is null" );
}
throw new IllegalArgumentException( "No enum constant " + getClass().getCanonicalName() + "." + name );
}
}