- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
获取Class的三种方式
package com.bjpowernode.javase.reflect;
import java.util.Date;
public class ReflectTest01 {
public static void main(String[] args) {
Class c1 = null;
Class c2 = null;
try {
c1 = Class.forName("java.lang.String");
c2 = Class.forName("java.util.Date");
Class c3 = Class.forName("java.lang.Integer");
Class c4 = Class.forName("java.lang.System");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String s = "abc";
Class x = s.getClass();
System.out.println(c1 == x);
Date time = new Date();
Class y = time.getClass();
System.out.println(c2 == y);
Class z = String.class;
Class k = Date.class;
Class f = int.class;
Class e = double.class;
System.out.println(x == z);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
package com.bjpowernode.javase.bean;
public class User {
public User(){
System.out.println("无参数构造方法");
}
public User(String s){
}
}
package com.bjpowernode.javase.reflect;
import com.bjpowernode.javase.bean.User;
public class ReflectTest02 {
public static void main(String[] args) {
User user = new User();
System.out.println(user);
try {
Class c = Class.forName("com.bjpowernode.javase.bean.User");
Object obj = c.newInstance();
System.out.println(obj);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
通过读属性文件实例化对象
package com.bjpowernode.javase.reflect;
import java.io.FileReader;
import java.util.Properties;
public class ReflectTest03 {
public static void main(String[] args) throws Exception {
FileReader reader = new FileReader("reflect/classinfo2.properties");
Properties pro = new Properties();
pro.load(reader);
reader.close();
String className = pro.getProperty("className");
System.out.println(className);
Class c = Class.forName(className);
Object obj = c.newInstance();
System.out.println(obj);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
forName()作用
package com.bjpowernode.javase.reflect;
class MyClass{
static{
System.out.println("MyClass类的静态代码块执行了!");
}
}
package com.bjpowernode.javase.reflect;
public class ReflectTest04 {
public static void main(String[] args) {
try {
Class.forName("com.bjpowernode.javase.reflect.MyClass");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
获取类路径下文件的绝对路径
package com.bjpowernode.javase.reflect;
public class AbsolutePath{
public static void main(String[] args) throws Exception{
String path = Thread.currentThread().getContextClassLoader().getResource("classinfo2.properties").getPath();
System.out.println(path);
String path2 = Thread.currentThread().getContextClassLoader().getResource("com/bjpowernode/javase/bean/db.properties").getPath();
System.out.println(path2);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
以流的形式直接返回
package com.bjpowernode.javase.reflect;
import sun.net.TelnetInputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.util.Properties;
public class IoPropertiesTest {
public static void main(String[] args) throws Exception{
InputStream reader = Thread.currentThread().getContextClassLoader().getResourceAsStream("classinfo2.properties");
Properties pro = new Properties();
pro.load(reader);
String className = pro.getProperty("className");
System.out.println(className);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
资源绑定器
package com.bjpowernode.javase.reflect;
import java.util.ResourceBundle;
public class ResourceBundleTest {
public static void main(String[] args) {
ResourceBundle bundle = ResourceBundle.getBundle("com/bjpowernode/javase/bean/db");
String className = bundle.getString("className");
System.out.println(className);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
反射Field(了解一下)
package com.bjpowernode.javase.reflect;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
public class ReflectTest05 {
public static void main(String[] args) throws Exception{
Class studentClass = Class.forName("com.bjpowernode.javase.bean.Student");
String className = studentClass.getName();
System.out.println("完整类名" + className);
String simpleName = studentClass.getSimpleName();
System.out.println("简易名" + simpleName);
Field[] fileds = studentClass.getFields();
System.out.println(fileds.length);
Field f = fileds[0];
String fieldName = f.getName();
System.out.println(fieldName);
System.out.println("---------------------------");
Field[] fs = studentClass.getDeclaredFields();
System.out.println(fs.length);
for(Field field : fs){
int i = field.getModifiers();
System.out.println(i);
String modifierString = Modifier.toString(i);
System.out.println(modifierString);
Class fieldType = field.getType();
String fName = fieldType.getSimpleName();
System.out.println(fName);
System.out.println(field.getName());
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
反编译Field(了解一下)
package com.bjpowernode.javase.reflect;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
public class ReflectTest06 {
public static void main(String[] args) throws Exception{
StringBuilder s = new StringBuilder();
Class studentClass = Class.forName("java.lang.String");
s.append(Modifier.toString(studentClass.getModifiers()) + " class " + studentClass.getSimpleName() + " {\n");
Field[] fields = studentClass.getDeclaredFields();
for(Field field : fields){
s.append("\t");
s.append(Modifier.toString(field.getModifiers()));
s.append(" ");
s.append(field.getType().getSimpleName());
s.append(" ");
s.append(field.getName());
s.append(";\n");
}
s.append("}");
System.out.println(s);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
通过反射机制访问对象属性(必须掌握)
package com.bjpowernode.javase.reflect;
import com.bjpowernode.javase.bean.Student;
import java.lang.reflect.Field;
public class ReflectTest07 {
public static void main(String[] args) throws Exception{
Student s = new Student();
s.no = 1111;
System.out.println(s.no);
Class studentClass = Class.forName("com.bjpowernode.javase.bean.Student");
Object obj = studentClass.newInstance();
Field noField = studentClass.getDeclaredField("no");
noField.set(obj, 2222);
System.out.println(noField.get(obj));
Field nameField = studentClass.getDeclaredField("name");
nameField.setAccessible(true);
nameField.set(obj, "jackson");
System.out.println(nameField.get(obj));
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
可变长度参数
package com.bjpowernode.javase.reflect;
public class ReflectTest08 {
public static void main(String[] args) {
m();
m(10);
m(10, 20);
m2(100);
m2(200, "abc");
m2(200, "abc", "def");
m2(200, "abc", "def", "xyz");
m3("ab", "de", "kk");
String[] strs = {"a", "b", "c"};
m3(strs);
m3(new String[]{"我", "是", "中", "国", "人"});
}
public static void m(int...args){
System.out.println("m方法执行了!");
}
public static void m2(int a, String...args){
}
public static void m3(String...args){
for(int i = 0; i < args.length; i ++){
System.out.println(args[i]);
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
反射Method
package com.bjpowernode.javase.reflect;
import com.sun.org.apache.xpath.internal.operations.Mod;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public class ReflectTest09 {
public static void main(String[] args) throws Exception{
Class userServiceClass = Class.forName("com.bjpowernode.javase.service.UserService");
Method[] methods = userServiceClass.getDeclaredMethods();
System.out.println(methods.length);
for(Method method : methods){
System.out.println(Modifier.toString(method.getModifiers()));
System.out.println(method.getReturnType().getSimpleName());
System.out.println(method.getName());
Class[] parameterTypes = method.getParameterTypes();
for(Class parameterType : parameterTypes){
System.out.println(parameterType.getSimpleName());
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
反编译Method
package com.bjpowernode.javase.reflect;
import com.sun.org.apache.xpath.internal.operations.Mod;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public class ReflectTest10 {
public static void main(String[] args) throws Exception{
StringBuilder s = new StringBuilder();
Class userServiceClass = Class.forName("java.lang.String");
s.append(Modifier.toString(userServiceClass.getModifiers()) + " class " + userServiceClass.getSimpleName() + " {\n");
Method[] methods = userServiceClass.getDeclaredMethods();
for(Method method : methods){
s.append("\t");
s.append(Modifier.toString(method.getModifiers()));
s.append(method.getReturnType().getSimpleName());
s.append(" ");
s.append(method.getName());
s.append("(");
Class[] parameterTypes = method.getParameterTypes();
for(Class parameterType : parameterTypes){
s.append(parameterType.getSimpleName());
s.append(",");
}
s.deleteCharAt(s.length() - 1);
s.append("){}\n");
}
s.append("}");
System.out.println(s);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
反射机制调用方法
package com.bjpowernode.javase.reflect;
import com.bjpowernode.javase.service.UserService;
import java.lang.reflect.Method;
public class ReflectTest11 {
public static void main(String[] args) throws Exception{
UserService userService = new UserService();
boolean loginSuccess = userService.login("admin", "123");
System.out.println(loginSuccess);
System.out.println(loginSuccess ? "登录成功" : "登录失败");
Class userServiceClass = Class.forName("com.bjpowernode.javase.service.UserService");
Object obj = userServiceClass.newInstance();
Method loginMehod = userServiceClass.getDeclaredMethod("login", String.class, String.class);
Method loginMehod2 = userServiceClass.getDeclaredMethod("login", int.class);
Object retValue = loginMehod.invoke(obj, "admin", "123");
System.out.println(retValue);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
反射Constructor
package com.bjpowernode.javase.reflect;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
public class ReflectTest12 {
public static void main(String[] args) throws Exception{
StringBuilder s = new StringBuilder();
Class vipClass = Class.forName("com.bjpowernode.javase.bean.Vip");
s.append(Modifier.toString(vipClass.getModifiers()));
s.append(" class ");
s.append(vipClass.getSimpleName());
s.append(" {\n");
Constructor[] constructors = vipClass.getDeclaredConstructors();
for(Constructor constructor : constructors){
s.append("\t");
s.append(Modifier.toString(constructor.getModifiers()));
s.append(" ");
s.append(vipClass.getSimpleName());
s.append("(");
Class[] parameterTypes = constructor.getParameterTypes();
for(Class parameterType : parameterTypes){
s.append(parameterType.getSimpleName());
s.append(",");
}
if(parameterTypes.length > 0){
s.deleteCharAt(s.length() - 1);
}
s.append("){}\n");
}
s.append("}");
System.out.println(s);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
反射机制调用构造方法
package com.bjpowernode.javase.reflect;
import com.bjpowernode.javase.bean.Vip;
import java.lang.reflect.Constructor;
public class ReflectTest13 {
public static void main(String[] args) throws Exception{
Vip vip1 = new Vip();
Vip vip2 = new Vip(111);
Vip vip3 = new Vip(122, "lisi");
Class c = Class.forName("com.bjpowernode.javase.bean.Vip");
Object obj = c.newInstance();
System.out.println(obj);
Constructor con = c.getDeclaredConstructor(int.class, String.class, String.class, boolean.class);
Object newObj = con.newInstance(110,"jackson","2000-03-21",true);
System.out.println(newObj);
Constructor con2 = c.getDeclaredConstructor();
Object newObj2 = con2.newInstance();
System.out.println(newObj2);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
获取父类和父接口
package com.bjpowernode.javase.reflect;
public class ReflectTest14 {
public static void main(String[] args) throws Exception{
Class stringClass = Class.forName("java.lang.String");
Class superClass = stringClass.getSuperclass();
System.out.println(superClass.getName());
Class[] interfaces = stringClass.getInterfaces();
for(Class in : interfaces){
System.out.println(in.getName());
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27