Home | History | Annotate | Download | only in src-ex
      1 /*
      2  * Copyright (C) 2018 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 import java.lang.invoke.MethodHandles;
     18 import java.lang.invoke.MethodType;
     19 
     20 /** Class with helper methods for field and method lookups using java.lang.invoke. */
     21 public class JLI {
     22   public static boolean canDiscoverWithLookupFindGetter(
     23       MethodHandles.Lookup lookup, Class<?> klass, String fieldName, Class<?> fieldType) {
     24     try {
     25       return lookup.findGetter(klass, fieldName, fieldType) != null;
     26     } catch (NoSuchFieldException e) {
     27       return false;
     28     } catch (IllegalAccessException e) {
     29       return true;
     30     }
     31   }
     32 
     33   public static boolean canDiscoverWithLookupFindSetter(
     34       MethodHandles.Lookup lookup, Class<?> klass, String fieldName, Class<?> fieldType) {
     35     try {
     36       return lookup.findSetter(klass, fieldName, fieldType) != null;
     37     } catch (NoSuchFieldException e) {
     38       return false;
     39     } catch (IllegalAccessException e) {
     40       return true;
     41     }
     42   }
     43 
     44   public static boolean canDiscoverWithLookupFindStaticGetter(
     45       MethodHandles.Lookup lookup, Class<?> klass, String fieldName, Class<?> fieldType) {
     46     try {
     47       return lookup.findStaticGetter(klass, fieldName, fieldType) != null;
     48     } catch (NoSuchFieldException e) {
     49       return false;
     50     } catch (IllegalAccessException e) {
     51       return true;
     52     }
     53   }
     54 
     55   public static boolean canDiscoverWithLookupFindStaticSetter(
     56       MethodHandles.Lookup lookup, Class<?> klass, String fieldName, Class<?> fieldType) {
     57     try {
     58       return lookup.findStaticSetter(klass, fieldName, fieldType) != null;
     59     } catch (NoSuchFieldException e) {
     60       return false;
     61     } catch (IllegalAccessException e) {
     62       return true;
     63     }
     64   }
     65 
     66   public static boolean canDiscoverWithLookupFindConstructor(
     67       MethodHandles.Lookup lookup, Class<?> klass, MethodType methodType) {
     68     try {
     69       return lookup.findConstructor(klass, methodType) != null;
     70     } catch (NoSuchMethodException e) {
     71       return false;
     72     } catch (IllegalAccessException e) {
     73       return true;
     74     }
     75   }
     76 
     77   public static boolean canDiscoverWithLookupFindVirtual(
     78       MethodHandles.Lookup lookup, Class<?> klass, String methodName, MethodType methodType) {
     79     try {
     80       return lookup.findVirtual(klass, methodName, methodType) != null;
     81     } catch (NoSuchMethodException e) {
     82       return false;
     83     } catch (IllegalAccessException e) {
     84       return true;
     85     }
     86   }
     87 
     88   public static boolean canDiscoverWithLookupFindStatic(
     89       MethodHandles.Lookup lookup, Class<?> klass, String methodName, MethodType methodType) {
     90     try {
     91       return lookup.findStatic(klass, methodName, methodType) != null;
     92     } catch (NoSuchMethodException e) {
     93       return false;
     94     } catch (IllegalAccessException e) {
     95       return true;
     96     }
     97   }
     98 }
     99