1.Was ist Reflexion?
Eine Klasse verfügt über mehrere Komponenten, z. B. Mitgliedsvariablen, Methoden, Konstruktoren usw.
Reflexion besteht darin, die Klasse zu laden und die verschiedenen Komponenten der Klasse zu sezieren.
2. Laden der Klasse
In Java gibt es eine Klassenklasse, die den Bytecode einer bestimmten Klasse darstellt.
Da die Klassenklasse die Bytecode einer bestimmten Klasse darstellt, muss sie eine bestimmte Klasse laden.
Methode von Bytecode: Forname ().
in den Speicher und in den Klassenobjekten einkapseln.
Weitere zwei Möglichkeiten, Klassenobjekte zu erhalten:
Klassenname.Class
Object.getClass ()
Erstellen Sie zuerst eine einfache Personklasse
Die Codekopie lautet wie folgt:
öffentliche Klasse reflektiert Demo {
öffentliche statische Leere Main (String args []) löst Ausnahme aus
{// 1.
Class clazz = class.Forname ("dsa.person");
// 2.
Klasse clazz1 = new Person (). GetClass ();
// 3.
Classclazz2 = person.class;
}
}
3. Reflexionskonstruktionsmethode
Persönlicher Klasse:
Die Codekopie lautet wie folgt:
/**
*Baumethode
*/
publicPerson () {
System.out.println ("null");
}
publicPerson (StringName) {
System.out.println (Name);
}
publicPerson (StringName, intpwd) {
System.out.println (Name+"+"+PWD);
}
privateperson (listlist) {
System.out.println ("Liste");
}
In der Testklasse:
Die Codekopie lautet wie folgt:
// Reflection PublicPerson ()
@Prüfen
publicVoidTest1 () throwSexception {
Classclazz = class.forname ("rflectorDemo.person");
Constructorcr = clazz.getConstructor (null); // Holen Sie sich das Konstruktorobjekt
Personp = (Person) cr.Newinstance (null); // das Objekt durch den Konstruktor instanziieren
//System.out.println(p.name);
}
// Reflection PublicPerson (StringName)
@Prüfen
publicVoidTest2 () throwSexception {
Classclazz = class.forname ("rflectorDemo.person");
Constructorcr = clazz.getConstructor (string.class);
Personp = (Person) Cr.Newinstance ("haha");
System.out.println (P.Name);
}
// Reflection PublicPerson (StringName, intpwd)
@Prüfen
publicVoidTest3 () throwSexception {
Classclazz = class.forname ("rflectorDemo.person");
Constructorcr = clazz.getConstructor (string.class, int.Class);
Personp = (Person) Cr.Newinstance ("haha", 1);
//System.out.println(p.name);
}
// Reflection PublicPerson (Listliste)
@Prüfen
publicvoidTest4 () throwSexception {
Classclazz = class.forname ("rflectorDemo.person");
Constructorcr = clazz.getDeclaredConstructor (list.class);
cr.setAccessible (true); // Brute Force Cracking
PersonP = (Person) cr.Newinstance (newArrayList ());
System.out.println (P.Name);
}
// Eine andere Möglichkeit, Objekte zu erstellen, ist nur für die Konstruktion von Methoden ohne Parameter anwendbar
@Prüfen
publicVoidTest5 () throwSexception {
Classclazz = class.forname ("rflectorDemo.person");
Personp = (Person) clazz.Newinstance ();
System.out.println (P.Name);
}
Wenn die Konstruktionsmethode privat ist, werden wir Brute-Force-Crack! ! !
4. Konventionelle Reflexionsmethoden
Persönlicher Klasse:
Die Codekopie lautet wie folgt:
/**
*Verfahren
*/
publicvoidjf ()
{
}
publicVoidjf (StringName, intpwd)
{
}
publicClass [] JF (StringName, int [] PWD)
{
returnNewClass [] {string.class, int.class};
}
privateVoidjf (InputStreamin)
{
System.out.println (in);
}
publicstaticvoidjf (intnum)
{
System.out.println (num);
}
publicStaticvoidmain (StringArgs [])
{
System.out.println ("Main !!!");
}
In der Testklasse:
Die Codekopie lautet wie folgt:
/**
*Reflektierende Methode
*
*@authortanlvxu
*
*/
publicClassDemo2 {
// Reflexionsklassenmethode: Publicvoidjf ()
@Prüfen
publicVoidTest1 () throwSexception {
PersonP = newperson ();
Classclazz = class.forname ("rflectorDemo.person");
MethodMethod = clazz.getMethod ("jf", null);
method.invoke (p, null);
}
// Reflexionsklassenmethode: PublicVoidjf (StringName, Intpwd)
@Prüfen
publicVoidTest2 () throwSexception {
PersonP = newperson ();
Classclazz = class.forname ("rflectorDemo.person");
MethodMethod = clazz.getMethod ("jf", string.class, int.class);
method.invoke (p, "dsa", 30);
}
// Reflexionsklassenmethode: PublicClass [] JF (StringName, int [] PWD)
@Prüfen
publicVoidTest3 () throwSexception {
PersonP = newperson ();
Classclazz = class.forname ("rflectorDemo.person");
MethodMethod = clazz.getMethod ("jf", string.class, int []. Klasse);
Classcs [] = (class []) methode.invoke (p, "aads", newInt [] {1,2,3});
System.out.println (CS [0]);
}
// Reflection Class -Methode: privateVoidjf (InputStreamin)
@Prüfen
publicvoidTest4 () throwSexception {
PersonP = newperson ();
Classclazz = class.forname ("rflectorDemo.person");
MethodMethod = clazz.getDeclaredMethod ("JF", InputStream.class);
method.setAccessible (true);
method.invoke (p, newFileInputStream ("d: //qqclient.txt"));
}
// Reflexionsklassenmethode: PublicStaticVoidjf (Intnum)
@Prüfen
publicVoidTest5 () throwSexception {
Classclazz = class.forname ("rflectorDemo.person");
MethodMethod = clazz.getMethod ("jf", int.class);
method.invoke (null, 30);
}
// Reflexionsklassenmethode: PublicStaticVoidm (Intnum)
@Prüfen
publicVoidTest6 () throwSexception {
Classclazz = class.forname ("rflectorDemo.person");
MethodMethod = clazz.getMethod ("main", String []. Klasse);
//method.invoke(null,(Object)newstring -{"ds","das "});
method.invoke (null, newObject [] {newString [] {"ds", "Das"}});
}
5. Reflexionsfelder
Persönlicher Klasse:
Die Codekopie lautet wie folgt:
/**
*Feld
*/
PublicStringName = "SWR";
privateIntPassword = 45;
privatestaticintage = 35;
In der Testklasse:
Java -Code
/**
*Reflektierende Klassenfelder
*@authortanlvxu
*
*/
publicClassDemo3 {
/**
*Reflection Field PublicStringName = "SWR";
*@throwSexception
*/
@Prüfen
publicvoidTest1 () throwSexception
{
PersonP = newperson ();
Classclazz = class.forname ("rflectorDemo.person");
Fieldf = clazz.getfield ("name");
// Erhalten Sie den Wert des Feldes
ObjectValue = F.get (p);
// Erhalten Sie den Feldtyp
Classtype = f.gettype ();
if (type.equals (string.class)) {
StringName = (String) f.get (p);
System.out.println (Name);
}
// Setzen Sie den Wert des Feldes
f.set (p, "dfafa");
System.out.println (P.Name);
}
/**
*Reflection Field privateIntPassword;
*@throwSexception
*/
@Prüfen
publicvoidTest2 () throwSexception
{
PersonP = newperson ();
Classclazz = class.forname ("rflectorDemo.person");
Fieldf = clazz.getDeclaredfield ("Passwort");
f.setAccessible (wahr);
F.Set (p, 36);
System.out.println (f.get (p));
}
/**
*Reflexionsfeld privatestatische Intage = 35;
*@throwSexception
*/
@Prüfen
publicvoidTest3 () throwSexception
{
Classclazz = class.forname ("rflectorDemo.person");
Fieldf = clazz.getDeclaredfield ("Alter");
f.setAccessible (wahr);
F.Set (Null, 24);
System.out.println (f.get (null));
}
Tatsächlich können wir unabhängig von der Reflexionskonstruktionsmethode oder der Felder nacheinander daraus lernen!