Use the Enum hack fpr all used enums. Before it was for the enums in the libraries

This commit is contained in:
Volker Berlin 2022-03-19 22:12:30 +01:00
parent bf0b43fd1a
commit 8d17e7cfc5
2 changed files with 28 additions and 20 deletions

View File

@ -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 );

View File

@ -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<String, ReplacementForEnums> enumConstantDirectory;
public abstract class ReplacementForEnums {
private static transient Map<String, ReplacementForEnums> 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<String, ReplacementForEnums> 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 );
}
}