List of usage examples for java.lang.invoke MethodHandles lookup
@CallerSensitive @ForceInline public static Lookup lookup()
From source file:com.haulmont.cuba.core.config.ConfigDefaultMethod.java
@Override public Object invoke(ConfigHandler handler, Object[] args, Object proxy) { try {// ww w.j a va 2 s. c om if (SystemUtils.IS_JAVA_1_8) { // hack to invoke default method of an interface reflectively Constructor<MethodHandles.Lookup> lookupConstructor = MethodHandles.Lookup.class .getDeclaredConstructor(Class.class, Integer.TYPE); if (!lookupConstructor.isAccessible()) { lookupConstructor.setAccessible(true); } return lookupConstructor.newInstance(configInterface, MethodHandles.Lookup.PRIVATE) .unreflectSpecial(configMethod, configInterface).bindTo(proxy).invokeWithArguments(args); } else { return MethodHandles.lookup() .findSpecial(configInterface, configMethod.getName(), MethodType.methodType(configMethod.getReturnType(), configMethod.getParameterTypes()), configInterface) .bindTo(proxy).invokeWithArguments(args); } } catch (Throwable throwable) { throw new RuntimeException("Error invoking default method of config interface", throwable); } }
From source file:de.ks.flatadocdb.defaults.ReflectionLuceneDocumentExtractor.java
protected DocField createDocField(Field f) { try {// w w w .ja va 2s .co m Class<?> type = f.getType(); f.setAccessible(true); MethodHandle getter = MethodHandles.lookup().unreflectGetter(f); if (TypeUtils.isArrayType(type)) { return createArrayDocField(f, getter); } else if (Collection.class.isAssignableFrom(type)) { return createCollectionDocField(f, getter); } else if (String.class.equals(type)) { return createStringDocField(f, getter); } else if (LocalDateTime.class.equals(type)) { return createLocalDateTimeDocField(f, getter); } else { return new DocField(f, getter, (id, value) -> new StringField(id, String.valueOf(value), org.apache.lucene.document.Field.Store.YES)); } } catch (Exception e) { log.error("Could not extract docfield from {}", f, e); throw new RuntimeException(e); } }
From source file:com.github.wolf480pl.mias4j.core.runtime.BMClassLoader.java
private void initHandle(Class<?> cls) { try {/*from ww w . ja v a2s.c om*/ policySetter = MethodHandles.lookup().findStatic(cls, METH_NAME, METH_TYPE); } catch (NoSuchMethodException | IllegalAccessException e) { throw new IllegalStateException("Couldn't find method handle for Bootstraps.setPolicy", e); } }
From source file:com.evanzeimet.queryinfo.jpa.result.AbstractTupleToPojoQueryInfoResultConverter.java
protected MethodHandle findFieldPutHandle(Class<?> hostClass, String memberName, Class<?> elementJavaType) { MethodHandle result = null;//from w w w . j ava 2s . c om try { Field field = hostClass.getDeclaredField(memberName); field.setAccessible(true); result = MethodHandles.lookup().unreflectSetter(field); } catch (NoSuchFieldException | SecurityException | IllegalAccessException e) { String message = String.format("Could not find field put handle for field [%s]", memberName); logger.debug(message); } if (result == null) { Class<?> hostSuperclass = hostClass.getSuperclass(); if (hostSuperclass != null) { result = findFieldPutHandle(hostSuperclass, memberName, elementJavaType); } } return result; }
From source file:de.unentscheidbar.validation.internal.Beans.java
public static @Nonnull MethodHandle getterMethod(Class<?> beanClass, String propertyName) { PropertyDescriptor pd = property(beanClass, propertyName); Method getter = pd == null ? null : pd.getReadMethod(); if (getter == null) { throw new IllegalArgumentException("Could not find a readable property named " + propertyName + " in class " + beanClass.getName()); }//from ww w .j av a 2s . co m try { return MethodHandles.lookup().unreflect(getter); } catch (IllegalAccessException e) { throw new IllegalArgumentException("I do not have access to property read method " + getter); } }
From source file:com.evanzeimet.queryinfo.jpa.result.AbstractTupleToPojoQueryInfoResultConverter.java
protected MethodHandle findFieldSetterHandle(String memberName, Class<?> elementJavaType) { MethodHandle result = null;// www. ja v a 2s . c o m String methodSuffix = StringUtils.capitalize(memberName); String setterName = String.format("set%s", methodSuffix); MethodType methodType = methodType(void.class, elementJavaType); Class<QueryInfoResultType> resultClass = getResultClass(); try { result = MethodHandles.lookup().findVirtual(resultClass, setterName, methodType); } catch (NoSuchMethodException | IllegalAccessException e) { String message = String.format("Could not find field setter handle [%s] for field [%s]", setterName, memberName); logger.debug(message); } return result; }
From source file:de.zib.sfs.WrappedFSDataInputStream.java
/** * Gets the datanode that was last read from as a string. Should be called * after the first read operation has been performed. * //from w w w . j a v a2 s. c o m * @return "->" + hostname of the datanode, or empty string if the * information is not available */ private String getDatanodeHostNameString() { if (this.datanodeHostnameSupplier == null) { if (this.in instanceof HdfsDataInputStream) { // call Hadoop's method directly final HdfsDataInputStream hdfsIn = (HdfsDataInputStream) this.in; if (hdfsIn.getCurrentDatanode() != null) { this.datanodeHostnameSupplier = () -> hdfsIn.getCurrentDatanode().getHostName(); if (LOG.isDebugEnabled()) { LOG.debug("Using datanodeHostNameSupplier from Hadoop."); } } else { if (LOG.isDebugEnabled()) { LOG.debug("datanodeHostNameSupplier from Hadoop has no DataNode information."); } this.datanodeHostnameSupplier = () -> ""; } } else { try { // Check if there's an appropriately named method available // that returns the hostname of the current node that is // being read from. Using the lambda factory provides almost // direct invocation performance. MethodHandles.Lookup methodHandlesLookup = MethodHandles.lookup(); // try this stream or the one it wraps Method getCurrentDatanodeHostNameMethod = null; InputStream bindToStream = null; try { getCurrentDatanodeHostNameMethod = this.in.getClass() .getDeclaredMethod("getCurrentDatanodeHostName"); bindToStream = this.in; } catch (NoSuchMethodException e) { getCurrentDatanodeHostNameMethod = this.in.getWrappedStream().getClass() .getDeclaredMethod("getCurrentDatanodeHostName"); bindToStream = this.in.getWrappedStream(); } MethodHandle datanodeHostNameSupplierTarget = LambdaMetafactory.metafactory(methodHandlesLookup, "get", MethodType.methodType(Supplier.class, bindToStream.getClass()), MethodType.methodType(Object.class), methodHandlesLookup.unreflect(getCurrentDatanodeHostNameMethod), MethodType.methodType(Object.class)).getTarget(); this.datanodeHostnameSupplier = (Supplier<String>) datanodeHostNameSupplierTarget .bindTo(bindToStream).invoke(); if (LOG.isDebugEnabled()) { LOG.debug("Using 'getCurrentDatanodeHostName' as datanodeHostNameSupplier."); } } catch (Throwable t) { this.datanodeHostnameSupplier = () -> ""; if (LOG.isDebugEnabled()) { LOG.debug("No datanodeHostNameSupplier available.", t); } } } } // handle cases where we have to perform a reverse lookup if // hostname is an IP String dnHostname = this.datanodeHostnameSupplier.get(); String cachedHostname = HOSTNAME_CACHE.get(dnHostname); if (cachedHostname == null) { try { // strip port if necessary int portIndex = dnHostname.indexOf(":"); cachedHostname = InetAddress .getByName(portIndex == -1 ? dnHostname : dnHostname.substring(0, portIndex)).getHostName(); } catch (UnknownHostException e) { if (LOG.isDebugEnabled()) { LOG.debug("Could not determine hostname for " + dnHostname, e); } cachedHostname = ""; } HOSTNAME_CACHE.put(dnHostname, cachedHostname); } return cachedHostname; }
From source file:ch.digitalfondue.npjt.QueryFactory.java
@SuppressWarnings("unchecked") public <T> T from(final Class<T> clazz) { return (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class[] { clazz }, new InvocationHandler() { @Override/*from w w w . jav a 2s.c om*/ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { boolean hasAnnotation = method.getAnnotation(Query.class) != null; if (hasAnnotation) { QueryTypeAndQuery qs = extractQueryAnnotation(clazz, method); return qs.type.apply(qs.query, qs.rowMapperClass, jdbc, method, args, columnMapperFactories.set, parameterConverters.set); } else if (method.getReturnType().equals(NamedParameterJdbcTemplate.class) && args == null) { return jdbc; } else if (IS_DEFAULT_METHOD != null && (boolean) IS_DEFAULT_METHOD.invoke(method)) { final Class<?> declaringClass = method.getDeclaringClass(); return LOOKUP_CONSTRUCTOR.newInstance(declaringClass, MethodHandles.Lookup.PRIVATE) .unreflectSpecial(method, declaringClass).bindTo(proxy).invokeWithArguments(args); } else { throw new IllegalArgumentException( String.format("missing @Query annotation for method %s in interface %s", method.getName(), clazz.getSimpleName())); } } } ); }
From source file:de.ks.flatadocdb.metamodel.Parser.java
protected MethodHandle getGetter(Field f) { try {//ww w. ja v a 2s.c o m f.setAccessible(true); return MethodHandles.lookup().unreflectGetter(f); } catch (IllegalAccessException e) { throw new ParseException( "Could not extract getter handle for " + f + " on " + f.getDeclaringClass().getName(), e); } }
From source file:de.ks.flatadocdb.metamodel.Parser.java
protected MethodHandle getSetter(Field f) { try {/*from w w w . j av a 2s . com*/ f.setAccessible(true); return MethodHandles.lookup().unreflectSetter(f); } catch (IllegalAccessException e) { throw new ParseException( "Could not extract setter handle for " + f + " on " + f.getDeclaringClass().getName(), e); } }