在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:EsotericSoftware/reflectasm开源软件地址:https://github.com/EsotericSoftware/reflectasm开源编程语言:Java 100.0%开源软件介绍:Please use the ReflectASM discussion group for support. OverviewReflectASM is a very small Java library that provides high performance reflection by using code generation. An access class is generated to set/get fields, call methods, or create a new instance. The access class uses bytecode rather than Java's reflection, so it is much faster. It can also access primitive fields via bytecode to avoid boxing. PerformanceThe source code for these benchmarks is included in the project. The above charts were generated on Oracle's Java 7u3, server VM. InstallationTo use reflectasm with maven, please use the following snippet in your pom.xml <dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>reflectasm</artifactId>
<version>1.11.9</version>
</dependency> UsageMethod reflection with ReflectASM: SomeClass someObject = ...
MethodAccess access = MethodAccess.get(SomeClass.class);
access.invoke(someObject, "setName", "Awesome McLovin");
String name = (String)access.invoke(someObject, "getName"); Field reflection with ReflectASM: SomeClass someObject = ...
FieldAccess access = FieldAccess.get(SomeClass.class);
access.set(someObject, "name", "Awesome McLovin");
String name = (String)access.get(someObject, "name"); Constructor reflection with ReflectASM: ConstructorAccess<SomeClass> access = ConstructorAccess.get(SomeClass.class);
SomeClass someObject = access.newInstance(); Avoiding Name LookupFor maximum performance when methods or fields are accessed repeatedly, the method or field index should be used instead of the name: SomeClass someObject = ...
MethodAccess access = MethodAccess.get(SomeClass.class);
int addNameIndex = access.getIndex("addName");
for (String name : names)
access.invoke(someObject, addNameIndex, "Awesome McLovin"); Iterate all fields: FieldAccess access = FieldAccess.get(SomeClass.class);
for(int i = 0, n = access.getFieldCount(); i < n; i++) {
access.set(instanceObject, i, valueToPut);
} VisibilityReflectASM can always access public members. An attempt is made to define access classes in the same classloader (using setAccessible) and package as the accessed class. If the security manager allows setAccessible to succeed, then protected and default access (package private) members can be accessed. If setAccessible fails, no exception is thrown, but only public members can be accessed. Private members can never be accessed. ExceptionsStack traces when using ReflectASM are a bit cleaner. Here is Java's reflection calling a method that throws a RuntimeException:
Here is the same but when ReflectASM is used:
If ReflectASM is used to invoke code that throws a checked exception, the checked exception is thrown. Because it is a compilation error to use try/catch with a checked exception around code that doesn't declare that exception as being thrown, you must catch Exception if you care about catching a checked exception in code you invoke with ReflectASM. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论