Interviewer: what is java reflection? I can't answer

TimeFriends 2022-06-24 07:45:26 阅读数:22

interviewerjavareflectionanswer

One . Concept

Reflection is to Java The various components of the map to the corresponding Java class .

Class Class is constructed by private, from JVM establish .

Reflection is java A characteristic of language , It allows programs to run ( Note that it's not compile time ) To conduct self-examination and operate on internal members . For example, it allows a java Class gets all its member variables and methods and displays them .Java Maybe it's not a lot of practical use of this ability , But there is no such feature in other programming languages . for example ,Pascal、C perhaps C++ There is no way to get information about function definition in the program .( come from Sun)

JavaBean yes reflection One of the practical applications of , It allows some tools to visualize the operation of software components . These tools go through reflection Dynamically load and get Java Components ( class ) Properties of .

Reflection is from 1.2 There is , The latter three frameworks will use reflection mechanism , It's about classes "Class", Not directly new CLass(), Its object is a byte code in memory .

Class An instance of a class represents a running Java Classes and interfaces in applications . Enumeration is a kind of , Annotation is an interface . Each array belongs to is mapped as Class Object , All arrays with the same element type and dimension share this Class object .

Basic Java type (boolean、byte、char、short、int、long、float and double) And keywords void It also means Class object .Class There is no public construction method .

Class Object is loaded by Java Virtual machine and by calling the defineClass Method is automatically constructed .

Person p1 = new Person();
// You can get bytecode in all three ways
CLass c1 = Date.class();
p1.getClass();
// If it exists, load , Otherwise, create a new one , The third kind is often used , Class names do not need to be known when writing source programs , Pass it on at runtime
Class.forName("java.lang.String");

Class.forName() Bytecode has been loaded into java In the virtual machine , To get bytecode ;java Bytecode has not been generated in the virtual machine Load with class loader , The loaded bytecode is buffered to the virtual machine .

Consider this simple example , Let's see reflection How it works .

import java.lang.reflect.*;
public class DumpMethods {
public static void main(String args[]) {
try {
Class c = Class.forName("java.util.Stack");
Method m[] = c.getDeclaredMethods();
for (int i = 0; i < m.length; i++)
System.out.println(m[i].toString());
}
catch (Throwable e){
System.err.println(e);
}
}
}
public synchronized java.lang.Object java.util.Stack.pop()
public java.lang.Object java.util.Stack.push(java.lang.Object)
public boolean java.util.Stack.empty()
public synchronized java.lang.Object java.util.Stack.peek()
public synchronized int java.util.Stack.search(java.lang.Object)

So that's a list of java.util.Stack Class and their qualifiers and return types . This program uses Class.forName Load the specified class , And then call getDeclaredMethods To get the list of methods defined in this class .java.lang.reflect.Methods Is a class used to describe a single method in a class .

The following example uses Class Object to display the class name of the object :

void printClassName(Object obj) {
System.out.println("The class of " + obj +
" is " + obj.getClass().getName());
}

You can also use a class literal (JLS Section 15.8.2) To get the specified type ( or void) Of Class object . for example :

System.out.println("The name of class Foo is: "+Foo.class.getName());

When there are no object instances , There are two main ways .

// Two ways to get a class type
Class cls1 = Role.class;
Class cls2 = Class.forName("yui.Role");

Notice in the second way ,forName The parameter in must be the complete class name ( Package name + Class name ), And this method needs to catch exceptions . Now get it cls1 I can just create one Role Instance of class , utilize Class Of newInstance Method is equivalent to calling the default constructor of the class .

Object o = cls1.newInstance();
// Create an instance
//Object o1 = new Role(); // It is equivalent to the above method

Two . Common methods

1.isPrimitive( Determine whether it is a basic type of bytecode )

public class TestReflect {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "abc";
Class cls1 = str.getClass();
Class cls2 = String.class;
Class cls3 = null;// You have to add null
try {
cls3 = Class.forName("java.lang.String");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(cls1==cls2);
System.out.println(cls1==cls3);
System.out.println(cls1.isPrimitive());
System.out.println(int.class.isPrimitive());// Determine specified Class Whether the object represents a basic type .
System.out.println(int.class == Integer.class);
System.out.println(int.class == Integer.TYPE);
System.out.println(int[].class.isPrimitive());
System.out.println(int[].class.isArray());
}
}

result :

true
true
false
true
false
true
false
true

2.getConstructor and getConstructors()

java There is no order of construction methods in , Distinguish by type and number of parameters .

public class TestReflect {
public static void main(String[] args) throws SecurityException, NoSuchMethodException {
// TODO Auto-generated method stub
String str = "abc";
System.out.println(String.class.getConstructor(StringBuffer.class));
}
}

3.Filed Class represents a member variable in a class .

import java.lang.reflect.Field;
public class TestReflect {
public static void main(String[] args) throws SecurityException, NoSuchMethodException, NoSuchFieldException, IllegalArgumentException, Exception {
ReflectPointer rp1 = new ReflectPointer(3,4);
Field fieldx = rp1.getClass().getField("x");// Must be x perhaps y
System.out.println(fieldx.get(rp1));
/*
* private Member variables of must use getDeclaredField, and setAccessible(true), Otherwise you can't see it
*/
Field fieldy = rp1.getClass().getDeclaredField("y");
fieldy.setAccessible(true);// Violent reflex
System.out.println(fieldy.get(rp1));
}
}
class ReflectPointer {
public int x = 0;
private int y = 0;
public ReflectPointer(int x,int y) {//alt + shift +s Right click source
super();
// TODO Auto-generated constructor stub
this.x = x;
this.y = y;
}
}

3、 ... and . Typical examples

1. Will all String In the member variable of type b Change to a.

import java.lang.reflect.Field;
public class TestReflect {
public static void main(String[] args) throws SecurityException, NoSuchMethodException, NoSuchFieldException, IllegalArgumentException, Exception {
ReflectPointer rp1 = new ReflectPointer(3,4);
changeBtoA(rp1);
System.out.println(rp1);
}
private static void changeBtoA(Object obj) throws RuntimeException, Exception {
Field[] fields = obj.getClass().getFields();
for(Field field : fields) {
//if(field.getType().equals(String.class))
// Because there is only one copy of bytecode , use equals Semantic inaccuracy
if(field.getType()==String.class) {
String oldValue = (String)field.get(obj);
String newValue = oldValue.replace('b', 'a');
field.set(obj,newValue);
}
}
}
}
class ReflectPointer {
private int x = 0;
public int y = 0;
public String str1 = "ball";
public String str2 = "basketball";
public String str3 = "itcat";
public ReflectPointer(int x,int y) {//alt + shift +s Right click source
super();
// TODO Auto-generated constructor stub
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "ReflectPointer [str1=" + str1 + ", str2=" + str2 + ", str3="
+ str3 + "]";
}
}

2. Write a program according to the class name provided by the user , Call the main Method .

Why use reflection ?

import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class TestReflect {
public static void main(String[] args) throws SecurityException, NoSuchMethodException, NoSuchFieldException, IllegalArgumentException, Exception {
String str = args[0];
/*
* This will cause the array corner to cross the bounds , Because there's no character array at all
* Right click on run as-configurations-arguments Internal input b.Inter( Full class name )
*
*/
Method m = Class.forName(str).getMethod("main",String[].class);
// Either of the following can be done ,main Method requires a parameter
m.invoke(null, new Object[]{new String[]{"111","222","333"}});
m.invoke(null, (Object)new String[]{"111","222","333"});// This shows that , An array is Object
/*
* m.invoke(null, new String[]{"111","222","333"})
* The one on the top can't , because java It will automatically unpack
*/
}
}
class Inter {
public static void main(String[] args) {
for(Object obj : args) {
System.out.println(obj);
}
}
}

3. simulation instanceof The operator

class S {
}
public class IsInstance {
public static void main(String args[]) {
try {
Class cls = Class.forName("S");
boolean b1 = cls.isInstance(new Integer(37));
System.out.println(b1);
boolean b2 = cls.isInstance(new S());
System.out.println(b2);
}
catch (Throwable e) {
System.err.println(e);
}
}
}

In this example, create a S Class Class object , Then check whether some objects are S Example .Integer(37) No , but new S() yes .

Four .Method class

Representative class ( Not object ) One of the methods in .

import java.lang.reflect.Field;
import java.lang.reflect.Method;
/*
* People draw circles on the blackboard , It involves three objects , Drawing a circle requires the center and radius of the circle , But it's private , The way to draw a circle
* It's not appropriate to assign to people .
*
* The driver stepped on the brake , The driver just gives instructions to the train , The brake action still needs the train to complete .
*
* Interviews often test object-oriented design , For example, people close the door , People just push the door .
*
* This is the expert model : Who owns the data , Who is the expert , The method is assigned to whom
*/
public class TestReflect {
public static void main(String[] args) throws SecurityException, NoSuchMethodException, NoSuchFieldException, IllegalArgumentException, Exception {
String str = "shfsfs";
// The bag begins with com Said is sun For internal use ,java The first one is the user's
Method mtCharAt = String.class.getMethod("charAt", int.class);
Object ch = mtCharAt.invoke(str,1);// If the first parameter is null, It must be a static method
System.out.println(ch);
System.out.println(mtCharAt.invoke(str, new Object[]{2}));//1.4 grammar
}
}

5、 ... and . Reflection of arrays

Array The utility class is used to complete the reflection operation of array .

The same type and latitude have the same bytecode .

int.class and Integer.class Not the same bytecode ,Integer.TYPE,TYPE The bytecode representing the base class corresponding to the wrapper class int.class==Integer.TYPE.

import java.util.Arrays;
/*
* From this example, even if the bytecode is the same, the objects are not necessarily the same , It's not the same thing at all
*
*/
public class TestReflect {
public static void main(String[] args) throws SecurityException, NoSuchMethodException, NoSuchFieldException, IllegalArgumentException, Exception {
int[] a = new int[3];
int[] b = new int[]{4,5,5};// The length cannot be specified after direct assignment , otherwise CE
int[][] c = new int[3][2];
String[] d = new String[]{"jjj","kkkk"};
System.out.println(a==b);//false
System.out.println(a.getClass()==b.getClass());//true
//System.out.println(a.getClass()==d.getClass()); // Compare bytecode a and cd There's no comparison
System.out.println(a.getClass());// Output class [I
System.out.println(a.getClass().getName());// Output [I, Brackets indicate arrays ,I Represents an integer
System.out.println(a.getClass().getSuperclass());// Output class java.lang.Object
System.out.println(d.getClass().getSuperclass());// Output class java.lang.Object
// Because the parents are Object, All of the following are OK
Object obj1 = a;// No, but Object[]
Object obj2 = b;
Object[] obj3 = c;// A one bit array of basic types can only be used as Object, It has to be treated as Object[]
Object obj4 = d;
// Be careful asList Handle int[] and String[] The difference between
System.out.println(Arrays.asList(b));//1.4 There is no variable parameter , Using arrays ,[[[email protected]]
System.out.println(Arrays.asList(d));//[jjj, kkkk]
}
}

6、 ... and . Conclusion

That's a simple use of reflection mechanism , Obviously, I've learned spring My friends must understand , Why can we get the specified methods and variables through the configuration file , When we create objects, we pass in string Realized , It's like you need something , We're going to make it for you , And we've been using Object, This means that java The dynamic nature of a language , Dependency has been greatly reduced .

About Java Reflection , Did you stop learning ?


We sincerely invite you to join our family , There is not only technical knowledge sharing here , And the bloggers help each other . I still get red packets from time to time , There is a lottery every month , Game consoles and physical books ( Free shipping ), Let's keep warm together , Curl up . Create beauty C standing . Looking forward to your joining .

copyright:author[TimeFriends],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/175/202206240310538115.html