Skip to main content
added 10 characters in body; edited title
Source Link
Cat
  • 67.6k
  • 24
  • 137
  • 144

android Android changing private static final field using java reflectionJava Reflection?

I want to set "NO_CACHE" to be newValue. Here is my code: I want to set "NO_CACHE" to be newValue. Here is my code:

static void setFinalStatic(Object newValue) throws Exception {  Field field = CacheManager.class.getDeclaredField("NO_CACHE"); field.setAccessible(true); Field modifiersField=Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); modifiersField.setInt(field,field.getModifiers() & ~Modifier.FINAL); field.set(null, newValue); } 

But running this code on Android produces the following error: java.lang.NoSuchFieldException: modifiers

java.lang.NoSuchFieldException: modifiers 

I find "modifiers"modifiers is a field in the FieldField class on JAVAin Java, but it is not a field in the FieldField class on Android.

This is JAVA FieldJava Field class code:

http://www.docjar.com/html/api/java/lang/reflect/Field.java.html

This is Android FieldField class code:

http://www.oschina.net/code/explore/android-2.2-froyo/java/lang/reflect/Field.java

The above code in JAVAJava can work, but it does not work on the Android platform.

How do I fix this?

android changing private static final field using java reflection?

I want to set "NO_CACHE" to be newValue. Here is my code:

static void setFinalStatic(Object newValue) throws Exception {  Field field = CacheManager.class.getDeclaredField("NO_CACHE"); field.setAccessible(true); Field modifiersField=Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); modifiersField.setInt(field,field.getModifiers() & ~Modifier.FINAL); field.set(null, newValue); } 

But running this code on Android produces the following error: java.lang.NoSuchFieldException: modifiers

I find "modifiers" is a field in the Field class on JAVA, but it is not a field in the Field class on Android.

This is JAVA Field class code:

http://www.docjar.com/html/api/java/lang/reflect/Field.java.html

This is Android Field class code:

http://www.oschina.net/code/explore/android-2.2-froyo/java/lang/reflect/Field.java

The above code in JAVA can work, but it does not work on the Android platform.

How do I fix this?

Android changing private static final field using Java Reflection?

I want to set "NO_CACHE" to be newValue. Here is my code:

static void setFinalStatic(Object newValue) throws Exception { Field field = CacheManager.class.getDeclaredField("NO_CACHE"); field.setAccessible(true); Field modifiersField=Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); modifiersField.setInt(field,field.getModifiers() & ~Modifier.FINAL); field.set(null, newValue); } 

But running this code on Android produces the following error:

java.lang.NoSuchFieldException: modifiers 

I find modifiers is a field in the Field class in Java, but it is not a field in the Field class on Android.

This is Java Field class code:

http://www.docjar.com/html/api/java/lang/reflect/Field.java.html

This is Android Field class code:

http://www.oschina.net/code/explore/android-2.2-froyo/java/lang/reflect/Field.java

The above code in Java can work, but it does not work on the Android platform.

How do I fix this?

fixed the code block formatting
Source Link
Zachary Yates
  • 13.5k
  • 7
  • 60
  • 90

I want to set "NO_CACHE" to be newValue. Here is my code:

static void setFinalStatic(Object newValue) throws Exception

{

Field field = CacheManager.class.getDeclaredField("NO_CACHE");

static void setFinalStatic(Object newValue) throws Exception { Field field = CacheManager.class.getDeclaredField("NO_CACHE"); field.setAccessible(true);    Field modifiersField=Field.class.getDeclaredField("modifiers");    modifiersField.setAccessible(true);    modifiersField.setInt(field,field.getModifiers() & ~Modifier.FINAL); field.set(null, newValue); } 

field.set(null, newValue);

}

But running this code on Android produces the following error: java.lang.NoSuchFieldException: modifiers

I find "modifiers" is a field in the Field class on JAVA, but it is not a field in the Field class on Android.

This is JAVA Field class code:

http://www.docjar.com/html/api/java/lang/reflect/Field.java.html

This is Android Field class code:

http://www.oschina.net/code/explore/android-2.2-froyo/java/lang/reflect/Field.java

The above code in JAVA can work, but it does not work on the Android platform.

How do I fix this?

I want to set "NO_CACHE" to be newValue. Here is my code:

static void setFinalStatic(Object newValue) throws Exception

{

Field field = CacheManager.class.getDeclaredField("NO_CACHE");

field.setAccessible(true);   Field modifiersField=Field.class.getDeclaredField("modifiers");   modifiersField.setAccessible(true);   modifiersField.setInt(field,field.getModifiers() & ~Modifier.FINAL); 

field.set(null, newValue);

}

But running this code on Android produces the following error: java.lang.NoSuchFieldException: modifiers

I find "modifiers" is a field in the Field class on JAVA, but it is not a field in the Field class on Android.

This is JAVA Field class code:

http://www.docjar.com/html/api/java/lang/reflect/Field.java.html

This is Android Field class code:

http://www.oschina.net/code/explore/android-2.2-froyo/java/lang/reflect/Field.java

The above code in JAVA can work, but it does not work on the Android platform.

How do I fix this?

I want to set "NO_CACHE" to be newValue. Here is my code:

static void setFinalStatic(Object newValue) throws Exception { Field field = CacheManager.class.getDeclaredField("NO_CACHE"); field.setAccessible(true);  Field modifiersField=Field.class.getDeclaredField("modifiers");  modifiersField.setAccessible(true);  modifiersField.setInt(field,field.getModifiers() & ~Modifier.FINAL); field.set(null, newValue); } 

But running this code on Android produces the following error: java.lang.NoSuchFieldException: modifiers

I find "modifiers" is a field in the Field class on JAVA, but it is not a field in the Field class on Android.

This is JAVA Field class code:

http://www.docjar.com/html/api/java/lang/reflect/Field.java.html

This is Android Field class code:

http://www.oschina.net/code/explore/android-2.2-froyo/java/lang/reflect/Field.java

The above code in JAVA can work, but it does not work on the Android platform.

How do I fix this?

Source Link

android changing private static final field using java reflection?

I want to set "NO_CACHE" to be newValue. Here is my code:

static void setFinalStatic(Object newValue) throws Exception

{

Field field = CacheManager.class.getDeclaredField("NO_CACHE");

field.setAccessible(true); Field modifiersField=Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); modifiersField.setInt(field,field.getModifiers() & ~Modifier.FINAL); 

field.set(null, newValue);

}

But running this code on Android produces the following error: java.lang.NoSuchFieldException: modifiers

I find "modifiers" is a field in the Field class on JAVA, but it is not a field in the Field class on Android.

This is JAVA Field class code:

http://www.docjar.com/html/api/java/lang/reflect/Field.java.html

This is Android Field class code:

http://www.oschina.net/code/explore/android-2.2-froyo/java/lang/reflect/Field.java

The above code in JAVA can work, but it does not work on the Android platform.

How do I fix this?