项目作者: zhanghai

项目描述 :
Java library for easy-to-use reflection
高级语言: Java
项目地址: git://github.com/zhanghai/Reflected.git
创建时间: 2019-06-04T04:54:00Z
项目社区:https://github.com/zhanghai/Reflected

开源协议:

下载


Reflected

A Java library for easy-to-use reflection.

Why Reflected?

  • Thread-safe one-time retrieval of reflected objects.
  • One line to reflect a method/field along with its class.
  • A single exception type to handle (or ignore) for reflective operations.

Integration

  1. dependencies {
  2. implementation 'me.zhanghai.java.reflected:library:1.0.1'
  3. }

Usage

Create an instance of ReflectedClass, ReflectedConstructor, ReflectedField or ReflectdMethod to access a class, constructor, field or method via reflection. .class constant, ReflectedClass instance and String for class name can all be used in place where a class is expected. If you want to handle a reflection failure, catch for ReflectedException.

For example:

  1. public class ReflectedExamples {
  2. // Using reflection to access a field.
  3. private static final ReflectedField<ErrnoException> sFunctionNameField = new ReflectedField<>(
  4. ErrnoException.class, "functionName");
  5. @NonNull
  6. public static String getFunctionName(@NonNull ErrnoException errnoException) {
  7. return sFunctionNameField.getObject(errnoException);
  8. }
  9. // Using reflection to access a method.
  10. @NonNull
  11. private static final ReflectedMethod<StorageManager> sGetVolumeListMethod =
  12. new ReflectedMethod<>(StorageManager.class, "getVolumeList");
  13. @NonNull
  14. public static StorageVolume[] getStorageVolumes(@NonNull StorageManager storageManager) {
  15. return sGetVolumeListMethod.invoke(storageManager);
  16. }
  17. // Using a string for the declaring class of a method.
  18. private static final ReflectedMethod<?> sNewFileChannelMethod = new ReflectedMethod<>(
  19. "java.nio.NioUtils", "newFileChannel", Closeable.class, FileDescriptor.class,
  20. int.class);
  21. @NonNull
  22. public static FileChannel newFileChannel(@NonNull Closeable ioObject,
  23. @NonNull FileDescriptor fd, int mode) {
  24. return sNewFileChannelMethod.invoke(null, ioObject, fd, mode);
  25. }
  26. // Sharing a ReflectedClass instance for the declaring class of methods.
  27. @NonNull
  28. private static final ReflectedClass<?> sSELinuxClass = new ReflectedClass<>(
  29. "android.os.SELinux");
  30. @NonNull
  31. private static final ReflectedMethod<?> sIsSELinuxEnabledMethod = new ReflectedMethod<>(
  32. sSELinuxClass, "isSELinuxEnabled");
  33. @NonNull
  34. private static final ReflectedMethod<?> sIsSELinuxEnforcedMethod = new ReflectedMethod<>(
  35. sSELinuxClass, "isSELinuxEnforced");
  36. public static boolean isSELinuxEnabled() {
  37. return sIsSELinuxEnabledMethod.invoke(null);
  38. }
  39. public static boolean isSELinuxEnforced() {
  40. return sIsSELinuxEnforcedMethod.invoke(null);
  41. }
  42. }

License

  1. Copyright 2018 Hai Zhang
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.