PDF檔案解析&拆分在SAP憑證列印場景中的運用(二)
1.Class類
-
Class本身也是一個類
-
Class物件只能由系統建立物件
-
一個載入的類在JVM中只會有一個Class例項
-
一個Class物件對應的是一個載入到JVM中的一個class檔案
-
每個類的例項都會記得自己是由哪個Class例項所生成
-
通過Class可以完整的得到一個類中的所有被載入的結構
@Test
public void test1() throws ClassNotFoundException {
//通過反射獲取類的class物件
Class c1 = Class.forName("com.yl.pojo.User");
System.out.println(c1);
//一個類在記憶體中只有一個Class物件
//一個類被載入後,類的整個結構都會被封裝在Class物件中
Class c2 = Class.forName("com.yl.pojo.User");
System.out.println(c1==c2);
}
2.獲取Class類的例項
//測試Class類的建立方法
@Test
public void test2() throws ClassNotFoundException{
User user = new Student();
System.out.println("這個人是:"+user.name);
//方式一:通過物件獲得
Class c1 = user.getClass();
System.out.println(c1);
//方式二:forName獲得
Class c2 = Class.forName("com.yl.pojo.Student");
//方式三:通過類名.class獲得
Class<Student> c3 = Student.class;//c1 c2 c3是同一個物件
//方式四:基本內建型別的包裝類都有一個Type屬性
Class<Integer> c4 = Integer.TYPE;
System.out.println(c4);//int
//獲得父類型別
Class c5 = c1.getSuperclass();
System.out.println(c5);
}
3.哪些型別可以有Class物件
class、interface、陣列、enum、annotation、基本資料型別、void
只要元素型別和維度一樣,就是同一個Class物件
4.類的載入
什麼時候發生類的初始化:(new一個類和通過反射呼叫才發生)
5.獲取執行時類的完整結構
@Test
public void test4() throws NoSuchFieldException, NoSuchMethodException {
User student = new Student();
Class c1 = student.getClass();
//獲得類的名字
System.out.println(c1.getName());//獲得類名+包名
System.out.println(c1.getSimpleName());//獲得類名
//獲得類的屬性
Field[] fields = c1.getFields();//獲得public屬性,包括父類中的欄位
for (Field field : fields) {
System.out.println(field);
}
Field[] declaredFields = c1.getDeclaredFields();//獲得某個類的所有宣告的欄位,但不包括父類中的欄位
for (Field declaredField : declaredFields) {
System.out.println(declaredField);
}
System.out.println(c1.getField("name"));
//獲得類的方法
Method[] methods = c1.getMethods();
for (Method method : methods) {
System.out.println(method);
}
Method[] declaredMethods = c1.getDeclaredMethods();
for (Field declaredField : declaredFields) {
System.out.println(declaredField);
}
Method getName = c1.getMethod("getName", null);
Method setName = c1.getMethod("setName", String.class);
//獲得構造器,同樣可以指定
System.out.println(c1.getConstructors());
System.out.println(c1.getDeclaredConstructors());
}
6.動態建立物件執行方法
@Test
public void test5() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
Class c1 = Class.forName("com.yl.pojo.User");
User user = (User)c1.newInstance();//本質上是呼叫類的無參構造器
System.out.println(user);
//通過構造器建立物件
Constructor constructor = c1.getDeclaredConstructor(String.class, int.class, int.class,String.class);
User user2 = (User) constructor.newInstance("lulu", 1, 18,"女");
System.out.println(user2);
//通過反射呼叫普通方法
Method setGender = c1.getDeclaredMethod("setGender", String.class);
User user3 = (User) c1.newInstance();
setGender.invoke(user3,"男");//invoke:啟用
System.out.println(user3.getGender());
//通過反射操作屬性,不能直接操作私有屬性,需要關閉安全檢測
Field gender = c1.getDeclaredField("gender");//gender是私有欄位
gender.setAccessible(true);//關閉安全檢測
User user4 = (User) c1.newInstance();
gender.set(user4,"女");
System.out.println(user4.getGender());
}
7.通過反射獲取泛型資訊
public void test6(Map<String,User> map, List<User> list){
System.out.println("test6");
}
@Test
public void test7() throws NoSuchMethodException {
Method test6 = myTest.class.getMethod("test6", Map.class, List.class);
//獲得泛型引數
Type[] genericParameterTypes = test6.getGenericParameterTypes();
for (Type genericParameterType : genericParameterTypes) {
System.out.println("獲得型別"+genericParameterType);
//強轉
if(genericParameterType instanceof ParameterizedType) {
Type[] actualTypeArguments = ((ParameterizedType) genericParameterType).getActualTypeArguments();
for (Type actualTypeArgument : actualTypeArguments) {
System.out.println("真實型別:"+actualTypeArgument);
}
}
}
}
8.反射操作註解
類註解:
package com.yl.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//類名的註解
//target:作用域,retention:獲取級別
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyTable{
String value();
}
屬性註解:
package com.yl.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//屬性的註解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyField{
String columnName();
String type();
int length();
}
pojo:
@MyTable("db_User")
public class User {
@MyField(columnName = "id",type = "int",length = 10)
private int id;
@MyField(columnName = "name",type = "varchar",length = 15)
private String name;
@MyField(columnName = "age",type = "int",length = 3)
private int age;
}
測試:
@Test
public void test() throws ClassNotFoundException, NoSuchFieldException {
Class c1 = Class.forName("com.yl.pojo.User");
//通過反射獲得註解
Annotation[] annotations = c1.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
//獲得註解的value的值
MyTable myTable = (MyTable)c1.getAnnotation(MyTable.class);
System.out.println(myTable.value());
Field name = c1.getDeclaredField("name");
MyField annotation = name.getAnnotation(MyField.class);
System.out.println(annotation.columnName());
System.out.println(annotation.type());
System.out.println(annotation.length());
}
相關推薦
PDF檔案解析&拆分在SAP憑證列印場景中的運用(二)
小爬上篇文章分析了,SAP憑證批量列印場景中為啥要用到PDF檔案解析&拆分。這篇文章,緊接著上一篇,重點談談如何用python來做到高效的PDF檔案解析&拆分。
HCNA Routing&Switching之動態路由協議OSPF基礎(二)
對於ospf的包來講,其ospf包頭結構不會發生變化,頭部主要資訊有版本資訊,預設ipv4是使用ospf v2版本;型別是用來表示ospf包型別,ospf包的型別有五中型別;分別是hello包,dd包(database description),LSR(L
檔案下載:從伺服器上下載,自動給下載的PDF新增水印(二)
給下載的檔案新增水印程式碼,接上一篇《檔案下載:從伺服器上下載,自動給下載的PDF新增水印(二)》
解析Linux中的VFS檔案系統之檔案系統的註冊(二)
繼上一篇文章:https://cloud.tencent.com/developer/article/1053882 3. 檔案系統的註冊 這裡的檔案系統是指可能會被掛載到目錄樹中的各個實際檔案系統,所謂實際檔案系統,即是指VFS 中的實際操作最終要通過它們來
Dubbo原始碼解析(二)Dubbo擴充套件機制SPI
Dubbo擴充套件機制SPI 前一篇文章《dubbo原始碼解析(一)Hello,Dubbo》是對dubbo整個專案大體的介紹,而從這篇文章開始,我將會從原始碼來解讀dubbo再各個模組的實現原理以及特點,由於全部由截圖的方式去解讀原始碼
RocketMQ深度解析(二):NameServer
NamerServer NameServer是一個非常簡單的Topic路由註冊中心,其角色類似Dubbo中的zookeeper,支援Broker的動態註冊與發現。主要包括兩個功能:Broker管理,NameServer接受Broker叢集的註冊資訊並且儲存下來作為路由資
JavaScript ECMA-262-3 深入解析(二):變數物件例項詳解
本文例項講述了JavaScript ECMA-262-3變數物件。分享給大家供大家參考,具體如下:
RocketMQ入門到入土(二)事務訊息&順序訊息
接上一篇:RocketMQ入門到入土(一)新手也能看懂的原理和實戰! 一、事務訊息的由來
Hadoop基礎(二十九):資料清洗(ETL)(二)複雜解析版
資料清洗案例實操-複雜解析版 1.需求 對Web訪問日誌中的各欄位識別切分,去除日誌中不合法的記錄。根據清洗規則,輸出過濾後的資料。
Redis詳解(二)- redis的配置檔案介紹
目錄 1、開頭說明 2、INCLUDES 3、MODULES 4、NETWORK 5、GENERAL 6、SNAPSHOTTING 7、REPLICATION 8、SECURITY
linux檔案和目錄(二)
rm命令 rm 刪除檔案,刪除時會提示是否刪除。預設會附帶選項-i。使用/usr/bin/rm就不會有提示。
2020最新版SpringCloud(H版&alibaba)框架開發教程(二)
單機Eureka構建步驟 1.建module 2.改pom <dependencies> <!--eureka server--> <dependency>
.NetCore對接各大財務軟體憑證API——用友系列(3)
一. 前言 由於前段時間專案比較集中,所以停更了好久,終於來到我們用友的系列產品3---U8Cloud2.7了。
python04篇 檔案操作(二)、集合
一、檔案操作(二) 1.1 利用with來開啟檔案 #withopen,python 會自動關閉檔案 with open(\'a.txt\', encoding=\'utf-8\') as f:# f 檔案控制代碼
JavaScript Blob二進位制檔案物件(二)使用示例
JavaScript Blob二進位制檔案物件(二)使用示例 1.Blob 建構函式用法舉例 Blob() 建構函式允許通過其它物件建立 Blob 物件。比如,用字串構建一個 blob:
在js檔案中引入(呼叫)另一個js檔案的三種方法
比如我寫了一個JS檔案,這個檔案需要呼叫另外一個JS檔案,該如何實現呢?下面就總結下在js檔案中引入另一個js檔案的實現
mybatis 啟動流程原始碼分析(二)之 Configuration-Properties解析
一. 配置檔案 參考: https://www.cnblogs.com/wanthune/p/13674243.html 二. 原始碼解析 XMLConfigBuilder 就是解析Xml的主類。
檔案上傳漏洞:upload-labs(二)
Pass-11 原始碼 $is_upload = false; $msg = null; if(isset($_POST[\'submit\'])){ $ext_arr = array(\'jpg\',\'png\',\'gif\');
ssm搭建專案配置檔案整理(二)
pom.xml <?xml version=\"1.0\" encoding=\"UTF-8\"?> <project xmlns=\"http://maven.apache.org/POM/4.0.0\"