/** Returns an array of Class objects that represent the formal parameter types, in declaration order, of the constructor. */ public Class[] getParameterTypes () { return constructor.getParameterTypes(); }
/** Returns an array of Class objects that represent the formal parameter types, in declaration order, of the constructor. */ public Class[] getParameterTypes () { return constructor.getParameterTypes(); }
@Override public Boolean apply(Constructor<?> input) { return asList(input.getParameterTypes()).contains(String.class); } })
private static Constructor<?> getEndpointConstructor() { for (Constructor<?> current : TyrusEndpointWrapper.class.getConstructors()) { Class<?>[] types = current.getParameterTypes(); if (Endpoint.class == types[0] && EndpointConfig.class == types[1]) { return current; } } throw new IllegalStateException("No compatible Tyrus version found"); }
public MapDeserializer(Class type) { if (type == null) type = HashMap.class; _type = type; Constructor[] ctors = type.getConstructors(); for (int i = 0; i < ctors.length; i++) { if (ctors[i].getParameterTypes().length == 0) _ctor = ctors[i]; } if (_ctor == null) { try { _ctor = HashMap.class.getConstructor(new Class[0]); } catch (Exception e) { throw new IllegalStateException(e); } } }
public ReflectiveConstructorExecutor(Constructor<?> ctor) { this.ctor = ctor; if (ctor.isVarArgs()) { Class<?>[] paramTypes = ctor.getParameterTypes(); this.varargsPosition = paramTypes.length - 1; } else { this.varargsPosition = null; } }
/** * Create an array of descriptors representing the parameter types for the supplied * constructor. Returns a zero sized array if there are no parameters. * @param ctor a Constructor * @return a String array of descriptors, one entry for each constructor parameter */ public static String[] toParamDescriptors(Constructor<?> ctor) { return toDescriptors(ctor.getParameterTypes()); }
protected Constructor<?> getUserDeclaredConstructor(Constructor<?> constructor) { Class<?> declaringClass = constructor.getDeclaringClass(); Class<?> userClass = ClassUtils.getUserClass(declaringClass); if (userClass != declaringClass) { try { return userClass.getDeclaredConstructor(constructor.getParameterTypes()); } catch (NoSuchMethodException ex) { // No equivalent constructor on user class (superclass)... // Let's proceed with the given constructor as we usually would. } } return constructor; }
/** * Returns the descriptor corresponding to the given constructor. * * @param constructor a {@link Constructor} object. * @return the descriptor of the given constructor. */ public static String getConstructorDescriptor(final Constructor<?> constructor) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append('('); Class<?>[] parameters = constructor.getParameterTypes(); for (Class<?> parameter : parameters) { appendDescriptor(parameter, stringBuilder); } return stringBuilder.append(")V").toString(); }
/** * Verifies that {@code ctor} produces a {@link NullPointerException} or {@link * UnsupportedOperationException} whenever <i>any</i> of its non-nullable parameters are null. */ public void testConstructor(Constructor<?> ctor) { Class<?> declaringClass = ctor.getDeclaringClass(); checkArgument( Modifier.isStatic(declaringClass.getModifiers()) || declaringClass.getEnclosingClass() == null, "Cannot test constructor of non-static inner class: %s", declaringClass.getName()); Class<?>[] types = ctor.getParameterTypes(); for (int nullIndex = 0; nullIndex < types.length; nullIndex++) { testConstructorParameter(ctor, nullIndex); } }
private static boolean isApplicable(Member member, Class<?> sourceClass) { if (member instanceof Method) { Method method = (Method) member; return (!Modifier.isStatic(method.getModifiers()) ? ClassUtils.isAssignable(method.getDeclaringClass(), sourceClass) : method.getParameterTypes()[0] == sourceClass); } else if (member instanceof Constructor) { Constructor<?> ctor = (Constructor<?>) member; return (ctor.getParameterTypes()[0] == sourceClass); } else { return false; } }
/** * get constructor desc. * "()V", "(Ljava/lang/String;I)V" * * @param c constructor. * @return desc */ public static String getDesc(final Constructor<?> c) { StringBuilder ret = new StringBuilder("("); Class<?>[] parameterTypes = c.getParameterTypes(); for (int i = 0; i < parameterTypes.length; i++) { ret.append(getDesc(parameterTypes[i])); } ret.append(')').append('V'); return ret.toString(); }
/** * get constructor desc. * "()V", "(Ljava/lang/String;I)V" * * @param c constructor. * @return desc */ public static String getDesc(final Constructor<?> c) { StringBuilder ret = new StringBuilder("("); Class<?>[] parameterTypes = c.getParameterTypes(); for (int i = 0; i < parameterTypes.length; i++) { ret.append(getDesc(parameterTypes[i])); } ret.append(')').append('V'); return ret.toString(); }
private String getMethodDescriptor(Constructor c) { StringBuilder buf = new StringBuilder(); buf.append('('); for (Class p : c.getParameterTypes()) buf.append(Type.getDescriptor(p)); buf.append(")V"); return buf.toString(); } }
public int compare(Constructor<?> constructorA, Constructor<?> constructorB) { int argLengths = constructorB.getParameterTypes().length - constructorA.getParameterTypes().length; if (argLengths == 0) { int constructorAMockableParamsSize = countMockableParams(constructorA); int constructorBMockableParamsSize = countMockableParams(constructorB); return constructorBMockableParamsSize - constructorAMockableParamsSize; } return argLengths; }
private void checkParameterized(Constructor<?> constructor, Field field) { if(constructor.getParameterTypes().length == 0) { throw new MockitoException("the field " + field.getName() + " of type " + field.getType() + " has no parameterized constructor"); } }
@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; }
private int countMockableParams(Constructor<?> constructor) { int constructorMockableParamsSize = 0; for (Class<?> aClass : constructor.getParameterTypes()) { if(MockUtil.typeMockabilityOf(aClass).mockable()){ constructorMockableParamsSize++; } } return constructorMockableParamsSize; } };
public static Signature getSignature(Member member) { if (member instanceof Method) { return new Signature(member.getName(), Type.getMethodDescriptor((Method) member)); } else if (member instanceof Constructor) { Type[] types = TypeUtils.getTypes(((Constructor) member).getParameterTypes()); return new Signature(Constants.CONSTRUCTOR_NAME, Type.getMethodDescriptor(Type.VOID_TYPE, types)); } else { throw new IllegalArgumentException("Cannot get signature of a field"); } }
@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); } }