Using Java reflection to solve a situation where the Android system method cannot be invoked

Source: Internet
Author: User

Written before the reprint:

The "Android approach" that the authors refer to mostly refers to hidden classes in the Android code, but there are two ways to call them:

The first is to extract the Hidden class jar package from the Linux source, then import the project to call;

The second is to extract the corresponding Java class from the source code before compiling it and include it in the project;

From the performance point of view, I do not advocate the use of reflection, according to the online test results, the simple method of using reflection to invoke its performance loss is too large, but the complexity of the method or project use of performance loss ratio is small, but there will be loss, so I hope that everyone before using a lot of


Reproduced:

Before writing the 2.2 exit program, it was found that some APIs were hidden and apps could not be used directly, and that it was possible to use reflection during the discussion. A reflective test case was made today. As a primer.
In order to practical, conceptual things, trouble everyone yourself Google to understand, or refer to the next http://blog.csdn.net/lzh20044178/archive/2011/02/21/6197859.aspx, directly on the code:

Import Java.lang.reflect.Constructor;
Import java.lang.reflect.InvocationTargetException;
Import Java.lang.reflect.Method;
Import Java.util.Arrays;

: Showmethod.java
Using Java 1.1 reflection to show all
Methods of Class A, even if the methods are
defined in the base class

public class Showmethod {
Static final String Usage =
"Usage:/n" +
"Showmethods qualified.class.name/n" +
"To show all methods in class or:/n" +
"Showmethods qualified.class.name word/n" +
"To search for methods involving ' word '";

public static void Main (string[] args) {
System.out.println (arrays.deeptostring (args));
if (Args.length < 1) {
SYSTEM.OUT.PRINTLN (usage);
System.exit (0);
}
try {
Class C = class.forname (Args[0]);

class[] parametertypes = new Class[1];
Parametertypes[0] = Char[].class;
try {
Method tt = C.getmethod ("valueof", parametertypes);
char[] pp = {' A ', ' B ', ' C ', ' d '};
try {
String back = (string) Tt.invoke (C.newinstance (), pp);
SYSTEM.OUT.PRINTLN ("Reflection Invocation Method ValueOf (char[]) returns string:" +back);
catch (IllegalArgumentException e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (Illegalaccessexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (InvocationTargetException e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (Instantiationexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

catch (SecurityException e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (Nosuchmethodexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

method[] m = C.getmethods ();
Constructor[] ctor = c.getconstructors ();
if (args.length = = 1) {
for (int i = 0; i < m.length; i++)
System.out.println (M.tostring ());
for (int i = 0; i < ctor.length; i++)
System.out.println (Ctor.tostring ());
}
else {
for (int i = 0; i < m.length; i++)
if (m.tostring ()
. IndexOf (Args[1])!=-1)
System.out.println (M.tostring ());
for (int i = 0; i < ctor.length; i++)
if (ctor.tostring ()
. IndexOf (Args[1])!=-1)
System.out.println (Ctor.tostring ());
}
catch (ClassNotFoundException e) {
System.out.println ("No such class:" + E);
}

}

}


Examples of implementation need to be noted below:

1. If you are not in the eclipse environment but are executing on the command line, you need to pay attention to the relationship between the compiled Showmethod.class and the execution path.

Need to be executed in the bin directory of the project, and mine is:/work/workspace/th-java/bin

The location of my class file is:/work/workspace/th-java/bin/thinking/java/example_c11


2. If you are executing in eclipse. The parameter java.lang.String is configured in the Run Configuration->arguments.


Partial Run Results:

[Java.lang.String]
Reflection Invocation Method valueof (char[]) returns STRING:ABCD
public boolean java.lang.String.equals (Java.lang.Object)
Public java.lang.String java.lang.String.toString ()
public int Java.lang.String.hashCode ()
public int Java.lang.String.compareTo (java.lang.String)
public int Java.lang.String.compareTo (java.lang.Object)
public int java.lang.String.indexOf (int)
public int java.lang.String.indexOf (int,int)
public int java.lang.String.indexOf (java.lang.String)
。。。
Because it's a test, so
The Code default generation section is not consolidated, hehe. (Bad coding habits)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.