Java High Performance Reflection Toolkit Reflectasm

Source: Internet
Author: User

Reflectasm uses bytecode generation to achieve a more efficient reflection mechanism. Execution generates an Access class to set/get the field, access the method, or create an instance. As soon as you see ASM, you can realize that REFLECTASM will be generated in bytecode, rather than relying on the reflection mechanism of Java itself, so it is faster and avoids the problem of accessing the original type due to automatic boxing.

Pom.xml

<Projectxmlns= "http://maven.apache.org/POM/4.0.0"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelversion>4.0.0</modelversion>    <groupId>Com.jts</groupId>    <Artifactid>T1</Artifactid>    <version>0.0.1-snapshot</version>    <Packaging>Jar</Packaging>    <Properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    </Properties>    <Dependencies>        <Dependency>            <groupId>Junit</groupId>            <Artifactid>Junit</Artifactid>            <version>4.10</version>            <Scope>Test</Scope>        </Dependency>        <Dependency>            <groupId>Com.esotericsoftware</groupId>            <Artifactid>Reflectasm</Artifactid>            <version>1.11.0</version>        </Dependency>    </Dependencies></Project>

class to be reflected:

 Package Com.jts.service;  Public class UserService {        publicint state ;          Public void Update (int  ID, String username) {            }}

Test class:

 Packagecom.jts.test;ImportJava.lang.reflect.Method;Importorg.junit.Test;Importcom.esotericsoftware.reflectasm.ConstructorAccess;Importcom.esotericsoftware.reflectasm.FieldAccess;Importcom.esotericsoftware.reflectasm.MethodAccess;ImportCom.jts.service.UserService; Public classApptest {/*** JDK Reflection call Method *@throwsException*/@Test Public voidTestjdkreflect ()throwsException {userservice userservice=NewUserService (); LongStart =System.currenttimemillis (); Method Method= Userservice.getclass (). GetMethod ("Update",int.class, String.class);  for(inti = 0; i < 100000000; i++) {Method.invoke (UserService,1, "Zhangsan"); }        LongEnd =System.currenttimemillis (); System.out.println ("timeout=" + (End-start));//809 753 880 875 816    }    /*** Reflectasm Reflection Call Method * Locate the Reflection method by name*/@Test Public voidTestreflectasm4name () {UserService UserService=NewUserService (); Methodaccess Access= Methodaccess.get (userservice.class);//generate bytecode by creating userservicemethodaccess        LongStart =System.currenttimemillis ();  for(inti = 0; i < 100000000; i++) {Access.invoke (UserService,"Update", 1, "Zhangsan"); }        LongEnd =System.currenttimemillis (); System.out.println ("timeout=" + (End-start));//523 382 415 489 482    }    /*** Reflectasm Reflection Call Method * High performance with method and field index positioning reflection method*/@Test Public voidTestreflectasm4index () {UserService UserService=NewUserService (); Methodaccess Access= Methodaccess.get (userservice.class); intindex = access.getindex ("Update",int.class, String.class); LongStart =System.currenttimemillis ();  for(inti = 0; i < 100000000; i++) {Access.invoke (UserService, index,1, "Zhangsan"); }        LongEnd =System.currenttimemillis (); System.out.println ("timeout=" + (End-start));// the    }    /*** Reflectasm reflection to Set/get field values*/@Test Public voidtestfieldaccess () {userservice target=NewUserService (); Fieldaccess fieldaccess=Fieldaccess.get (Target.getclass ()); Fieldaccess.set (Target,"State", 1); intstate = (Integer) fieldaccess.get (target, ' state ');            System.out.println (state); }    /*** Reflectasm reflection to invoke construction method*/@Test Public voidtestconstructoraccess () {constructoraccess<UserService> constructoraccess = Constructoraccess.get (userservice.class); UserService UserService=constructoraccess.newinstance ();    System.out.println (UserService); }    /*** Index of the lookup method*/@Test Public voidTestindex () {userservice target=NewUserService (); Methodaccess methodaccess=Methodaccess.get (Target.getclass ()); intindex = methodaccess.getindex ("Update",int.class, String.class);    SYSTEM.OUT.PRINTLN (index); }}

Java High Performance Reflection Toolkit Reflectasm

Related Article

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.