List of usage examples for java.lang.invoke MethodHandles lookup
@CallerSensitive @ForceInline public static Lookup lookup()
From source file:org.apache.commons.javaflow.examples.invokedynamic.SimpleDynamicLinkage.java
public static CallSite bootstrapDynamic(final MethodHandles.Lookup caller, final String name, final MethodType type) throws NoSuchMethodException, IllegalAccessException { final MethodHandles.Lookup lookup = MethodHandles.lookup(); final Class<?> thisClass = lookup.lookupClass(); // (who am I?) final MethodHandle sayHello = lookup.findStatic(thisClass, "sayHello", MethodType.methodType(void.class)); return new ConstantCallSite(sayHello.asType(type)); }
From source file:org.gerzog.jstataggr.core.manager.impl.StatisticsManagerImpl.java
protected MethodHandle findGetter(final Class<?> statisticsClass, final Field field) { return propogate(() -> { final Method readMethod = new PropertyDescriptor(field.getName(), statisticsClass).getReadMethod(); return MethodHandles.lookup().unreflect(readMethod); }, (e) -> new IllegalStateException("There is no public getter for field <" + field + ">", e)); }
From source file:org.granitemc.granite.utils.Mappings.java
public static MethodHandle getMethod(CtClass clazz, String methodName) { if (methods.containsKey(clazz)) { if (methods.get(clazz).containsKey(methodName)) { return methods.get(clazz).get(methodName); } else {/*w w w . ja v a2 s .c o m*/ CtMethod ctMethod = ctMethods.get(clazz).get(methodName); if (ctMethod != null) { try { Class<?>[] paramTypes = new Class<?>[ctMethod.getParameterTypes().length]; for (int i = 0; i < paramTypes.length; i++) { paramTypes[i] = ReflectionUtils .getClassByName(ctMethod.getParameterTypes()[i].getName()); } Method m = Class.forName(ctMethod.getDeclaringClass().getName()) .getDeclaredMethod(ctMethod.getName(), paramTypes); m.setAccessible(true); MethodHandle handle = MethodHandles.lookup().unreflect(m); methods.get(clazz).put(methodName, handle); return handle; } catch (NotFoundException | NoSuchMethodException | IllegalAccessException | ClassNotFoundException e) { e.printStackTrace(); } } } } try { CtClass superClass = clazz.getSuperclass(); if (superClass == null || superClass.getName().equals("java.lang.Object")) { for (CtClass interfac : clazz.getInterfaces()) { MethodHandle m = getMethod(interfac, methodName); return m; } } else { return getMethod(superClass, methodName); } } catch (NotFoundException ignored) { } return null; }
From source file:org.neo4j.io.pagecache.impl.SingleFilePageSwapper.java
private static MethodHandle getPositionLockGetter() { try {/*from ww w. j a v a2 s. com*/ MethodHandles.Lookup lookup = MethodHandles.lookup(); Field field = FileChannelImpl.class.getDeclaredField("positionLock"); field.setAccessible(true); return lookup.unreflectGetter(field); } catch (Exception e) { return null; } }
From source file:org.openo.nfvo.resmanagement.common.util.RestfulUtil.java
/** * encapsulate the java reflect exception.<br> * * @param methodName, Restful's method./* w w w .j av a 2 s.c om*/ * @param objects, method param array. * @return * @since NFVO 0.5 */ public static RestfulResponse getRestRes(String methodName, Object... objects) { try { if (objects == null || REST_CLIENT == null) { return null; } Class<?>[] classes = new Class[objects.length]; for (int i = 0; i < objects.length; i++) { classes[i] = objects[i].getClass(); } if (methodName.startsWith("async")) { classes[classes.length - 1] = RestfulAsyncCallback.class; } Class<?> rtType = methodName.startsWith("async") ? void.class : RestfulResponse.class; MethodType mt = MethodType.methodType(rtType, classes); Object result = MethodHandles.lookup().findVirtual(REST_CLIENT.getClass(), methodName, mt) .bindTo(REST_CLIENT).invokeWithArguments(objects); if (result != null) { return (RestfulResponse) result; } LOGGER.warn("function=getRestRes, msg: invoke Restful async {} method which return type is Void.", methodName); return null; } catch (ReflectiveOperationException e) { LOGGER.error("function=getRestRes, msg=error occurs, e={}.", e); } catch (Throwable e) {// NOSONAR LOGGER.error("function=getRestRes, msg=Throwable, e={}.", e); try { throw (ServiceException) new ServiceException().initCause(e.getCause()); } catch (ServiceException se) { LOGGER.error("function=getRestRes, msg=ServiceException occurs, e={}.", se); } } return null; }
From source file:org.openo.nfvo.vnfmadapter.common.ResultRequestUtil.java
/** * common method//from w ww .j ava 2s. com * <br/> * * @param vnfmObject * @param path * url defined * @param methodName * [get, put, delete, post] * @param paramsJson * raw data with json format, if <code>methodName</code> is get * or delete, fill it with null * @return * @since NFVO 0.5 */ public static JSONObject call(JSONObject vnfmObject, String path, String methodName, String paramsJson) { JSONObject resultJson = new JSONObject(); ConnectMgrVnfm mgrVcmm = new ConnectMgrVnfm(); if (Constant.HTTP_OK != mgrVcmm.connect(vnfmObject)) { resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR); resultJson.put("data", "connect fail."); return resultJson; } HttpMethod httpMethod = null; try { String result = null; String vnfPath = path.contains("%s") ? String.format(path, mgrVcmm.getRoaRand()) : path; LOG.info("function=call, msg=url is {}, session is {}", vnfmObject.getString("url") + vnfPath, mgrVcmm.getAccessSession()); HttpRequests.Builder builder = new HttpRequests.Builder(Constant.ANONYMOUS) .addHeader(Constant.ACCESSSESSION, mgrVcmm.getAccessSession()) .setUrl(vnfmObject.getString("url"), vnfPath).setParams(paramsJson); MethodType methodType = MethodType.methodType(HttpRequests.Builder.class, new Class[0]); MethodHandle mt = MethodHandles.lookup().findVirtual(builder.getClass(), methodName, methodType) .bindTo(builder); builder = (HttpRequests.Builder) mt.invoke(); httpMethod = builder.execute(); result = httpMethod.getResponseBodyAsString(); LOG.warn("function=call, msg=response status is {}. result is {}", httpMethod.getStatusCode(), result); resultJson.put(Constant.RETCODE, httpMethod.getStatusCode()); resultJson.put("data", result); } catch (IOException e) { LOG.info("function=call, msg=IOException, e is {}", e); } catch (ReflectiveOperationException e) { LOG.info("function=call, msg=ReflectiveOperationException, e is {}", e); } catch (Throwable e) { LOG.info("function=call, msg=Throwable, e is {}", e); } finally { if (httpMethod != null) { httpMethod.releaseConnection(); } } if (httpMethod == null) { resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR); resultJson.put("data", "get connection error"); } return resultJson; }
From source file:org.openo.nfvo.vnfmadapter.common.ResultRequestUtil.java
/** * common method/*from w w w . jav a 2s . c o m*/ * <br/> * * @param vnfmObject * @param path * url defined * @param methodName * [get, put, delete, post] * @param paramsJson * raw data with json format, if <code>methodName</code> is get * or delete, fill it with null * @return * @since NFVO 0.5 */ public static JSONObject call(JSONObject vnfmObject, String path, String methodName, String paramsJson, String authModel) { LOG.info("request-param=" + paramsJson + ",authModel=" + authModel + ",path=" + path + ",vnfmInfo=" + vnfmObject); JSONObject resultJson = new JSONObject(); ConnectMgrVnfm mgrVcmm = new ConnectMgrVnfm(); if (Constant.HTTP_OK != mgrVcmm.connect(vnfmObject, authModel)) { resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR); resultJson.put("data", "connect fail."); return resultJson; } HttpMethod httpMethod = null; try { String result = null; String vnfPath = path.contains("%s") ? String.format(path, mgrVcmm.getRoaRand()) : path; LOG.info("function=call, msg=url is {}, session is {}", vnfmObject.getString("url") + vnfPath, mgrVcmm.getAccessSession()); HttpRequests.Builder builder = new HttpRequests.Builder(authModel) .addHeader(Constant.ACCESSSESSION, mgrVcmm.getAccessSession()) .setUrl(vnfmObject.getString("url"), vnfPath).setParams(paramsJson); MethodType methodType = MethodType.methodType(HttpRequests.Builder.class, new Class[0]); MethodHandle mt = MethodHandles.lookup().findVirtual(builder.getClass(), methodName, methodType) .bindTo(builder); builder = (HttpRequests.Builder) mt.invoke(); httpMethod = builder.execute(); result = httpMethod.getResponseBodyAsString(); LOG.warn("function=call, msg=response status is {}. result is {}", httpMethod.getStatusCode(), result); resultJson.put(Constant.RETCODE, httpMethod.getStatusCode()); resultJson.put("data", result); // logout delete tokens String token = mgrVcmm.getAccessSession(); String roaRand = mgrVcmm.getRoaRand(); String vnfmUrl = vnfmObject.getString("url"); removeTokens(vnfmUrl, token, roaRand); } catch (IOException e) { LOG.info("function=call, msg=IOException, e is {}", e); } catch (ReflectiveOperationException e) { LOG.info("function=call, msg=ReflectiveOperationException, e is {}", e); } catch (Throwable e) { LOG.info("function=call, msg=Throwable, e is {}", e); } finally { if (httpMethod != null) { httpMethod.releaseConnection(); } } if (httpMethod == null) { resultJson.put(Constant.RETCODE, Constant.HTTP_INNERERROR); resultJson.put("data", "get connection error"); } return resultJson; }