protected Object instantiate() throws Exception { return _constructor.newInstance(_constructorArgs); }
/** Returns an array of Class objects that represent the formal parameter types, in declaration order, of the constructor. */ public Class[] getParameterTypes () { return constructor.getParameterTypes(); }
public static XAConnection createXAConnection(Object factory, Connection physicalConn) throws SQLException { try { if (constructor == null) { constructor = JdbcXAConnection.class.getDeclaredConstructor(JdbcDataSourceFactory.class, int.class, JdbcConnection.class); constructor.setAccessible(true); } int id = getNextId(XA_DATA_SOURCE); return constructor.newInstance(factory, id, physicalConn); } catch (Exception e) { throw new SQLException("createXAConnection error", e); } }
public ReflectiveConstructorExecutor(Constructor<?> ctor) { this.ctor = ctor; if (ctor.isVarArgs()) { Class<?>[] paramTypes = ctor.getParameterTypes(); this.varargsPosition = paramTypes.length - 1; } else { this.varargsPosition = null; } }
public Serialization(Constructor<?> ctor) { clazz = ctor.getDeclaringClass(); args = ctor.getParameterTypes(); } }
/** * Make the given constructor accessible, explicitly setting it accessible * if necessary. The {@code setAccessible(true)} method is only called * when actually necessary, to avoid unnecessary conflicts with a JVM * SecurityManager (if active). * @param ctor the constructor to make accessible * @see java.lang.reflect.Constructor#setAccessible */ @SuppressWarnings("deprecation") // on JDK 9 public static void makeAccessible(Constructor<?> ctor) { if ((!Modifier.isPublic(ctor.getModifiers()) || !Modifier.isPublic(ctor.getDeclaringClass().getModifiers())) && !ctor.isAccessible()) { ctor.setAccessible(true); } }
private static <X> @Nullable X newFromConstructor(Constructor<X> constructor, Throwable cause) { Class<?>[] paramTypes = constructor.getParameterTypes(); Object[] params = new Object[paramTypes.length]; for (int i = 0; i < paramTypes.length; i++) { Class<?> paramType = paramTypes[i]; if (paramType.equals(String.class)) { params[i] = cause.toString(); } else if (paramType.equals(Throwable.class)) { params[i] = cause; } else { return null; } } try { return constructor.newInstance(params); } catch (IllegalArgumentException | InstantiationException | IllegalAccessException | InvocationTargetException e) { return null; } }
@Override public T newChannel() { try { return constructor.newInstance(); } catch (Throwable t) { throw new ChannelException("Unable to create Channel from class " + constructor.getDeclaringClass(), t); } }
@Override public TypedValue execute(EvaluationContext context, Object... arguments) throws AccessException { try { ReflectionHelper.convertArguments( context.getTypeConverter(), arguments, this.ctor, this.varargsPosition); if (this.ctor.isVarArgs()) { arguments = ReflectionHelper.setupArgumentsForVarargsInvocation( this.ctor.getParameterTypes(), arguments); } ReflectionUtils.makeAccessible(this.ctor); return new TypedValue(this.ctor.newInstance(arguments)); } catch (Exception ex) { throw new AccessException("Problem invoking constructor: " + this.ctor, ex); } }
public BeanDeserializer(Class cl) { _type = cl; _methodMap = getMethodMap(cl); _readResolve = getReadResolve(cl); Constructor[] constructors = cl.getConstructors(); int bestLength = Integer.MAX_VALUE; for (int i = 0; i < constructors.length; i++) { if (constructors[i].getParameterTypes().length < bestLength) { _constructor = constructors[i]; bestLength = _constructor.getParameterTypes().length; } } if (_constructor != null) { _constructor.setAccessible(true); Class[] params = _constructor.getParameterTypes(); _constructorArgs = new Object[params.length]; for (int i = 0; i < params.length; i++) { _constructorArgs[i] = getParamArg(params[i]); } } }
public void setAccessible (boolean accessible) { constructor.setAccessible(accessible); }
/** * Verifies that {@code ctor} produces a {@link NullPointerException} or {@link * UnsupportedOperationException} when the parameter in position {@code paramIndex} is null. If * this parameter is marked nullable, this method does nothing. */ public void testConstructorParameter(Constructor<?> ctor, int paramIndex) { ctor.setAccessible(true); testParameter(null, Invokable.from(ctor), paramIndex, ctor.getDeclaringClass()); }
@Override Type[] getGenericParameterTypes() { Type[] types = constructor.getGenericParameterTypes(); if (types.length > 0 && mayNeedHiddenThis()) { Class<?>[] rawParamTypes = constructor.getParameterTypes(); if (types.length == rawParamTypes.length && rawParamTypes[0] == getDeclaringClass().getEnclosingClass()) { // first parameter is the hidden 'this' return Arrays.copyOfRange(types, 1, types.length); } } return types; }
/** * Compares constructor signatures: names and parameters. */ public static boolean compareSignatures(Constructor first, Constructor second) { if (!first.getName().equals(second.getName())) { return false; } return compareParameters(first.getParameterTypes(), second.getParameterTypes()); }
/** Returns the Class object representing the class or interface that declares the constructor. */ public Class getDeclaringClass () { return constructor.getDeclaringClass(); }
@Override public boolean isCompilable() { if (!(this.cachedExecutor instanceof ReflectiveConstructorExecutor) || this.exitTypeDescriptor == null) { return false; } if (getChildCount() > 1) { for (int c = 1, max = getChildCount();c < max; c++) { if (!this.children[c].isCompilable()) { return false; } } } ReflectiveConstructorExecutor executor = (ReflectiveConstructorExecutor) this.cachedExecutor; if (executor == null) { return false; } Constructor<?> constructor = executor.getConstructor(); return (Modifier.isPublic(constructor.getModifiers()) && Modifier.isPublic(constructor.getDeclaringClass().getModifiers())); }
@Override public T newChannel() { try { return constructor.newInstance(); } catch (Throwable t) { throw new ChannelException("Unable to create Channel from class " + constructor.getDeclaringClass(), t); } }
public Serialization(Constructor<?> ctor) { clazz = ctor.getDeclaringClass(); args = ctor.getParameterTypes(); } }
public void setAccessible (boolean accessible) { constructor.setAccessible(accessible); }
private Executable(final Constructor<?> constructor) { parameterTypes = constructor.getParameterTypes(); isVarArgs = constructor.isVarArgs(); }