mirror of
https://github.com/GTNewHorizons/MouseTweaks.git
synced 2026-11-18 19:29:17 +00:00
Forge stuff, also 2.4.0.
This commit is contained in:
1 parent
e93c76b79f
commit
4a0fd23ba7
17 files changed
+228
-212
No files matched your search
@@ -0,0 +1,125 @@
|
||||
package yalter.mousetweaks;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class ReflectionCache
|
||||
{
|
||||
public boolean available = false;
|
||||
public boolean compatible = false;
|
||||
public HashMap<String, Class> classes = new HashMap<String, Class>();
|
||||
public HashMap<String, Method> methods = new HashMap<String, Method>();
|
||||
public HashMap<String, Field> fields = new HashMap<String, Field>();
|
||||
|
||||
public ReflectionCache()
|
||||
{
|
||||
}
|
||||
|
||||
public Object getFieldValue( String name, Object obj )
|
||||
{
|
||||
try
|
||||
{
|
||||
if ( fields.containsKey( name ) )
|
||||
{
|
||||
Field field = ( Field ) fields.get( name );
|
||||
return field.get( obj );
|
||||
}
|
||||
|
||||
Logger.Log( "No such field: " + name );
|
||||
}
|
||||
catch ( Exception e )
|
||||
{
|
||||
e.printStackTrace();
|
||||
Logger.Log( "Failed to get a value of field: " + name );
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean setFieldValue( Object obj, String name, Object value )
|
||||
{
|
||||
try
|
||||
{
|
||||
if ( fields.containsKey( name ) )
|
||||
{
|
||||
Field field = ( Field ) fields.get( name );
|
||||
field.set( obj, value );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Logger.Log( "No such field: " + name );
|
||||
}
|
||||
catch ( Exception e )
|
||||
{
|
||||
e.printStackTrace();
|
||||
Logger.Log( "Failed to set a value of field: " + name );
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object invokeMethod( Object obj, String name, Object... args )
|
||||
{
|
||||
try
|
||||
{
|
||||
if ( methods.containsKey( name ) )
|
||||
{
|
||||
Method method = ( Method ) methods.get( name );
|
||||
|
||||
if ( args != null )
|
||||
return method.invoke( obj, args );
|
||||
|
||||
return method.invoke( obj, new Object[0] );
|
||||
}
|
||||
|
||||
Logger.Log( "No such method: " + name );
|
||||
}
|
||||
catch ( Exception e )
|
||||
{
|
||||
e.printStackTrace();
|
||||
Logger.Log( "Failed to invoke method: " + name );
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public Object invokeStaticMethod( String className, String name, Object... args )
|
||||
{
|
||||
if ( classes.containsKey( className ) )
|
||||
{
|
||||
Class clazz = ( Class ) classes.get( className );
|
||||
|
||||
return invokeMethod( clazz, name, args );
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean isInstance( String className, Object obj )
|
||||
{
|
||||
if ( classes.containsKey( className ) )
|
||||
{
|
||||
Class clazz = ( Class ) classes.get( className );
|
||||
return clazz.isInstance( obj );
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void storeClass( String name, Class clazz )
|
||||
{
|
||||
classes.put( name, clazz );
|
||||
}
|
||||
|
||||
public void storeMethod( String name, Method method )
|
||||
{
|
||||
methods.put( name, method );
|
||||
}
|
||||
|
||||
public void storeField( String name, Field field )
|
||||
{
|
||||
fields.put( name, field );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user