mirror of
https://github.com/i-net-software/JWebAssembly.git
synced 2025-03-15 02:44:47 +01:00
hack for Enum.valueOf(String)
This commit is contained in:
parent
76b84e3eee
commit
0ef00ba2ab
@ -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 ) );
|
||||
}
|
||||
|
||||
|
@ -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 );
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user