private static MethodHandle getSharedStringConstructorMethodHandle( MethodHandles.Lookup lookup ) { try { Constructor<String> constructor = String.class.getDeclaredConstructor( char[].class, Boolean.TYPE ); constructor.setAccessible( true ); return lookup.unreflectConstructor( constructor ); } catch ( Exception e ) { return null; } }
static Optional<MethodHandle> findConstructor(Class<?> type, Class<?>... parameterTypes) { try { Constructor<?> constructor = type.getConstructor(parameterTypes); if (checkConstructor(constructor, parameterTypes.length > 0)) { return Optional.of(MethodHandles.publicLookup().unreflectConstructor(constructor)); } else { LOGGER.log(Level.FINEST, () -> "Class " + type.getName() + " constructor with parameters " + Arrays.toString(parameterTypes) + " cannot be used."); } } catch (NoSuchMethodException ex) { LOGGER.log(Level.FINEST, ex, () -> "Class " + type.getName() + " does not have a constructor with parameters " + Arrays.toString(parameterTypes) + "."); } catch (IllegalAccessException ex) { LOGGER.log(Level.FINER, ex, () -> "Access checking fails on " + type.getName() + " class, constructor with parameters " + Arrays.toString(parameterTypes) + "."); } return Optional.empty(); }
private MethodHandle constructor( Class<?> procDefinition ) throws ProcedureException { try { return lookup.unreflectConstructor( procDefinition.getConstructor() ); } catch ( IllegalAccessException | NoSuchMethodException e ) { throw new ProcedureException( Status.Procedure.ProcedureRegistrationFailed, e, "Unable to find a usable public no-argument constructor in the class `%s`. " + "Please add a valid, public constructor, recompile the class and try again.", procDefinition.getSimpleName() ); } }
/** Lazy initialize MethodHandle lookup which is persisted for reuse. * {@inheritDoc} */ @Override protected MethodHandle lookup(MethodHandle m) { try { return super.lookup( MethodHandles.lookup().unreflectConstructor(constructor)); } catch (Exception e) { throw new RuntimeException(e); } finally { // release object reference constructor = null; } }
public static CallSite constructorBootstrap(Lookup lookup, String selector, MethodType type, String referenceString) throws Throwable { Reference reference = Reference.factory.value_(referenceString); Constructor constructor = ImageBootstrapper.systemMapping.classMappingAtReference_(reference).identityClass().getConstructor(); MethodHandle constructorHandle = lookup.unreflectConstructor(constructor); return new ConstantCallSite(constructorHandle.asType(type)); }
public static <T> T makeEnumInstance(Class<T> enumClass, Object... constructorArgs) { try { Constructor<?> constructor = enumClass.getDeclaredConstructors()[0]; constructor.setAccessible(true); //noinspection unchecked return (T) MethodHandles.lookup().unreflectConstructor(constructor).invokeWithArguments(constructorArgs); } catch (Throwable t) { throw t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t); } }
@Override protected MethodHandle makeMethodHandle() { CompilerAsserts.neverPartOfCompilation(); try { final MethodHandle methodHandle = MethodHandles.publicLookup().unreflectConstructor(reflectionConstructor); return adaptSignature(methodHandle, true, getParameterCount()); } catch (IllegalAccessException e) { throw new IllegalStateException(e); } } }
private MethodInvocationHandler createConstructorInvocator(Constructor<?> constructor, Type result, Type[] params) throws NoSuchMethodException { try { return new ConstructorInvoker(lookup.unreflectConstructor(constructor), result.convertedType(), convertedTypes(params)); } catch (IllegalAccessException e) { throw new ReflectionFailedException(e); } }
public MethodHandle declaredConstructor(Class<?>... parameterTypes) { try { Constructor ctor = targetClass.getDeclaredConstructor(parameterTypes); return lookup.unreflectConstructor(ctor); } catch (NoSuchMethodException e) { throw new RuntimeException("Could not get constructor for " + targetClass.getName(), e); } catch (IllegalAccessException e) { throw new RuntimeException("Could not get constructor MethodHandle for " + targetClass.getName(), e); } }
public static MethodHandle constructorMethodHandle(StandardErrorCode errorCode, Class<?> clazz, Class<?>... parameterTypes) { try { return MethodHandles.lookup().unreflectConstructor(clazz.getConstructor(parameterTypes)); } catch (IllegalAccessException | NoSuchMethodException e) { throw new PrestoException(errorCode, e); } } }
@Override protected MethodHandle makeMethodHandle() { CompilerAsserts.neverPartOfCompilation(); try { final MethodHandle methodHandle = MethodHandles.publicLookup().unreflectConstructor(reflectionConstructor); return adaptSignature(methodHandle, true, getParameterCount()); } catch (IllegalAccessException e) { throw new IllegalStateException(e); } } }
mh = lookup.unreflectConstructor(constructor);
public LambdaFunctionBootstrap getLmabdaBootstrap() { Expression expression = this.getResult(); this.endVisitClass(); byte[] bytes = this.classWriter.toByteArray(); try { Class<?> defineClass = null; if (ClassDefiner.isJDK7()) { defineClass = ClassDefiner.defineClassByClassLoader(className, bytes, classLoader); } else { defineClass = ClassDefiner.defineClass(this.className, LambdaFunction.class, bytes, this.classLoader); } Constructor<?> constructor = defineClass.getConstructor(List.class, Expression.class, Env.class); MethodHandle methodHandle = MethodHandles.lookup().unreflectConstructor(constructor); return new LambdaFunctionBootstrap(this.className, expression, methodHandle, arguments); } catch (Exception e) { throw new CompileExpressionErrorException("define lambda class error", e); } }
/** * Gets a constructor that has the specified types of arguments. * Throw an IllegalStateException if the class does not have such a constructor. */ public static MethodHandle getConstructor(Class<?> clazz, Class<?>... args) { try { // We use unreflect so that we can make the constructor accessible Constructor<?> ctor = clazz.getDeclaredConstructor(args); ctor.setAccessible(true); return MethodHandles.lookup().unreflectConstructor(ctor); } catch (NoSuchMethodException e) { throw new IllegalStateException(clazz.getName() + " has no constructor with args " + Arrays.toString(args), e); } catch (IllegalAccessException e) { throw new IllegalStateException("Problem getting constructor for " + clazz.getName() + " with args " + Arrays.toString(args), e); } }
Constructor con = mc.getCachedConstrcutor().cachedConstructor; try { handle = LOOKUP.unreflectConstructor(con); if (LOG_ENABLED) LOG.info("successfully unreflected constructor"); } catch (IllegalAccessException e) {
private <T> Optional<Function<T, Supplier<PollingStrategy>>> custom(Class<?> clazz, Config properties, Class<T> targetType) { Function<T, Supplier<PollingStrategy>> pollingStrategyFunction; if (PollingStrategy.class.isAssignableFrom(clazz)) { // set class is PollingStrategy implementation try { // use public constructor with target parameter Constructor<?> constructor = clazz.getConstructor(targetType); MethodHandle constructorHandle = MethodHandles.publicLookup().unreflectConstructor(constructor); pollingStrategyFunction = customSupplier(constructorHandle); } catch (NoSuchMethodException | IllegalAccessException ex) { LOGGER.log(Level.FINE, ex, () -> clazz.getName() + " does not have public constructor with single parameter (" + targetType.getName() + "). Generic instance from Config will be used."); // use generic mapping as a fallback pollingStrategyFunction = target -> (Supplier<PollingStrategy>) properties.as(clazz).get(); } } else { // use builder pattern as a fallback throw new ConfigException("Configured polling strategy class " + clazz.getName() + " does not implement PollingStrategy"); } return Optional.ofNullable(pollingStrategyFunction); }
@Override public MethodHandle getShadowCreator(Class<?> theClass) { ShadowInfo shadowInfo = getShadowInfo(theClass); if (shadowInfo == null) return dropArguments(NO_SHADOW_HANDLE, 0, theClass); String shadowClassName = shadowInfo.shadowClassName; try { Class<?> shadowClass = Class.forName(shadowClassName, false, theClass.getClassLoader()); ShadowMetadata shadowMetadata = getShadowMetadata(shadowClass); MethodHandle mh = identity(shadowClass); // (instance) mh = dropArguments(mh, 1, theClass); // (instance) for (Field field : shadowMetadata.realObjectFields) { MethodHandle setter = LOOKUP.unreflectSetter(field); MethodType setterType = mh.type().changeReturnType(void.class); mh = foldArguments(mh, setter.asType(setterType)); } mh = foldArguments(mh, LOOKUP.unreflectConstructor(shadowMetadata.constructor)); // (shadow, instance) return mh; // (instance) } catch (IllegalAccessException | ClassNotFoundException e) { throw new RuntimeException("Could not instantiate shadow " + shadowClassName + " for " + theClass, e); } }
/** * Returns a MethodHandle corresponding to the specified constructor. * <p> * Warning: The way Oracle JVM implements producing MethodHandle for a constructor involves * creating JNI global weak references. G1 processes such references serially. As a result, * calling this method in a tight loop can create significant GC pressure and significantly * increase application pause time. */ public static MethodHandle constructorMethodHandle(StandardErrorCode errorCode, Class<?> clazz, Class<?>... parameterTypes) { try { return MethodHandles.lookup().unreflectConstructor(clazz.getConstructor(parameterTypes)); } catch (IllegalAccessException | NoSuchMethodException e) { throw new PrestoException(errorCode, e); } }
/** * Returns a MethodHandle corresponding to the specified constructor. * <p> * Warning: The way Oracle JVM implements producing MethodHandle for a constructor involves * creating JNI global weak references. G1 processes such references serially. As a result, * calling this method in a tight loop can create significant GC pressure and significantly * increase application pause time. */ public static MethodHandle constructorMethodHandle(StandardErrorCode errorCode, Constructor constructor) { try { return MethodHandles.lookup().unreflectConstructor(constructor); } catch (IllegalAccessException e) { throw new PrestoException(errorCode, e); } } }
private static MethodHandle constructor( Class<?> target, Class<?>... parameters ) throws Exception { return MethodHandles.lookup().unreflectConstructor( target.getConstructor( parameters ) ); }