本文整理汇总了Java中java.lang.reflect.Constructor.getParameterTypes方法的典型用法代码示例。如果您正苦于以下问题:Java Constructor.getParameterTypes方法的具体用法?Java Constructor.getParameterTypes怎么用?Java Constructor.getParameterTypes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.reflect.Constructor
的用法示例。
在下文中一共展示了Constructor.getParameterTypes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getConstructor
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
/**
* Retrieve a constructor for the given class, with arguments matching the specified Hibernate mapping
* {@link Type types}.
*
* @param clazz The class needing instantiation
* @param types The types representing the required ctor param signature
* @return The matching constructor.
* @throws PropertyNotFoundException Indicates we could not locate an appropriate constructor (todo : again with PropertyNotFoundException???)
*/
public static Constructor getConstructor(Class clazz, Type[] types) throws PropertyNotFoundException {
final Constructor[] candidates = clazz.getConstructors();
for ( int i = 0; i < candidates.length; i++ ) {
final Constructor constructor = candidates[i];
final Class[] params = constructor.getParameterTypes();
if ( params.length == types.length ) {
boolean found = true;
for ( int j = 0; j < params.length; j++ ) {
final boolean ok = params[j].isAssignableFrom( types[j].getReturnedClass() ) || (
types[j] instanceof PrimitiveType &&
params[j] == ( ( PrimitiveType ) types[j] ).getPrimitiveClass()
);
if ( !ok ) {
found = false;
break;
}
}
if ( found ) {
constructor.setAccessible( true );
return constructor;
}
}
}
throw new PropertyNotFoundException( "no appropriate constructor in class: " + clazz.getName() );
}
示例2: generateConstructors
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
private boolean generateConstructors(final Constructor<?> ctor) {
if(classOverride) {
// Generate a constructor that just delegates to ctor. This is used with class-level overrides, when we want
// to create instances without further per-instance overrides.
generateDelegatingConstructor(ctor);
return false;
}
// Generate a constructor that delegates to ctor, but takes an additional ScriptObject parameter at the
// beginning of its parameter list.
generateOverridingConstructor(ctor, false);
if (samName == null) {
return false;
}
// If all our abstract methods have a single name, generate an additional constructor, one that takes a
// ScriptFunction as its first parameter and assigns it as the implementation for all abstract methods.
generateOverridingConstructor(ctor, true);
// If the original type only has a single abstract method name, as well as a default ctor, then it can
// be automatically converted from JS function.
return ctor.getParameterTypes().length == 0;
}
示例3: findConstructor
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
private static <T> Constructor<T> findConstructor(
Class<T> clazz,
Object[] params) {
for (Constructor<?> constructor : clazz.getConstructors()) {
Class<?>[] paramTypes = constructor.getParameterTypes();
if (matches(paramTypes, params)) {
@SuppressWarnings("unchecked")
Constructor<T> rval = (Constructor<T>) constructor;
return rval;
}
}
throw new IllegalStateException(
"No appropriate constructor found for "
+ clazz.getCanonicalName());
}
示例4: findConstructor
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
private static Constructor findConstructor(Class p_findConstructor_0_, Class[] p_findConstructor_1_)
{
Constructor[] aconstructor = p_findConstructor_0_.getDeclaredConstructors();
for (int i = 0; i < aconstructor.length; ++i)
{
Constructor constructor = aconstructor[i];
Class[] aclass = constructor.getParameterTypes();
if (Reflector.matchesTypes(p_findConstructor_1_, aclass))
{
return constructor;
}
}
return null;
}
示例5: FunctionImplementationRegistry
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
public FunctionImplementationRegistry(DrillConfig config){
Stopwatch w = new Stopwatch().start();
logger.debug("Generating function registry.");
drillFuncRegistry = new DrillFunctionRegistry(config);
Set<Class<? extends PluggableFunctionRegistry>> registryClasses = PathScanner.scanForImplementations(
PluggableFunctionRegistry.class, config.getStringList(ExecConstants.FUNCTION_PACKAGES));
for (Class<? extends PluggableFunctionRegistry> clazz : registryClasses) {
for (Constructor<?> c : clazz.getConstructors()) {
Class<?>[] params = c.getParameterTypes();
if (params.length != 1 || params[0] != DrillConfig.class) {
logger.warn("Skipping PluggableFunctionRegistry constructor {} for class {} since it doesn't implement a " +
"[constructor(DrillConfig)]", c, clazz);
continue;
}
try {
PluggableFunctionRegistry registry = (PluggableFunctionRegistry)c.newInstance(config);
pluggableFuncRegistries.add(registry);
} catch(InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
logger.warn("Unable to instantiate PluggableFunctionRegistry class '{}'. Skipping it.", clazz, e);
}
break;
}
}
logger.info("Function registry loaded. {} functions loaded in {} ms.", drillFuncRegistry.size(), w.elapsed(TimeUnit.MILLISECONDS));
}
示例6: count
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
private int count(AccessibleObject methodOrCtor) {
if (methodOrCtor instanceof Method) {
Method method = (Method) methodOrCtor;
return method.getParameterTypes().length;
}
Constructor<?> constructor = (Constructor<?>) methodOrCtor;
return constructor.getParameterTypes().length;
}
示例7: getThrowableException
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
/**
* Returns a rethrowable exception for this task, if available.
* To provide accurate stack traces, if the exception was not
* thrown by the current thread, we try to create a new exception
* of the same type as the one thrown, but with the recorded
* exception as its cause. If there is no such constructor, we
* instead try to use a no-arg constructor, followed by initCause,
* to the same effect. If none of these apply, or any fail due to
* other exceptions, we return the recorded exception, which is
* still correct, although it may contain a misleading stack
* trace.
*
* @return the exception, or null if none
*/
private Throwable getThrowableException() {
int h = System.identityHashCode(this);
ExceptionNode e;
final ReentrantLock lock = exceptionTableLock;
lock.lock();
try {
expungeStaleExceptions();
ExceptionNode[] t = exceptionTable;
e = t[h & (t.length - 1)];
while (e != null && e.get() != this)
e = e.next;
} finally {
lock.unlock();
}
Throwable ex;
if (e == null || (ex = e.ex) == null)
return null;
if (e.thrower != Thread.currentThread().getId()) {
try {
Constructor<?> noArgCtor = null;
// public ctors only
for (Constructor<?> c : ex.getClass().getConstructors()) {
Class<?>[] ps = c.getParameterTypes();
if (ps.length == 0)
noArgCtor = c;
else if (ps.length == 1 && ps[0] == Throwable.class)
return (Throwable)c.newInstance(ex);
}
if (noArgCtor != null) {
Throwable wx = (Throwable)noArgCtor.newInstance();
wx.initCause(ex);
return wx;
}
} catch (Exception ignore) {
}
}
return ex;
}
示例8: getConstructorDescriptor
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
/**
* Returns the descriptor corresponding to the given constructor.
*
* @param c
* a {@link Constructor Constructor} object.
* @return the descriptor of the given constructor.
*/
public static String getConstructorDescriptor(final Constructor<?> c) {
Class<?>[] parameters = c.getParameterTypes();
StringBuilder buf = new StringBuilder();
buf.append('(');
for (int i = 0; i < parameters.length; ++i) {
getDescriptor(buf, parameters[i]);
}
return buf.append(")V").toString();
}
示例9: addConstructor
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
public void addConstructor(Constructor<?> constructor) throws Exception {
List<Type> paramTypes = new ArrayList<Type>();
for (Class<?> paramType : constructor.getParameterTypes()) {
paramTypes.add(Type.getType(paramType));
}
String methodDescriptor = Type.getMethodDescriptor(VOID_TYPE, paramTypes.toArray(EMPTY_TYPES));
MethodVisitor methodVisitor = visitor.visitMethod(Opcodes.ACC_PUBLIC, "<init>", methodDescriptor, signature(constructor), EMPTY_STRINGS);
for (Annotation annotation : constructor.getDeclaredAnnotations()) {
if (annotation.annotationType().getAnnotation(Inherited.class) != null) {
continue;
}
Retention retention = annotation.annotationType().getAnnotation(Retention.class);
AnnotationVisitor annotationVisitor = methodVisitor.visitAnnotation(Type.getType(annotation.annotationType()).getDescriptor(), retention != null && retention.value() == RetentionPolicy.RUNTIME);
annotationVisitor.visitEnd();
}
methodVisitor.visitCode();
// this.super(p0 .. pn)
methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
for (int i = 0; i < constructor.getParameterTypes().length; i++) {
methodVisitor.visitVarInsn(Type.getType(constructor.getParameterTypes()[i]).getOpcode(Opcodes.ILOAD), i + 1);
}
methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superclassType.getInternalName(), "<init>", methodDescriptor, false);
methodVisitor.visitInsn(Opcodes.RETURN);
methodVisitor.visitMaxs(0, 0);
methodVisitor.visitEnd();
}
示例10: getConstructor
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
private static <T> Constructor<T> getConstructor(final Key<T> key) {
final boolean staticClass = Modifier.isStatic(key.getType().getModifiers());
final Class<?> parentClass = key.getType().getDeclaringClass();
final boolean innerClass = !staticClass && parentClass != null;
Constructor<T> inject = null;
Constructor<T> noarg = null;
for (final Constructor<T> c : ((Constructor<T>[]) key.getType().getDeclaredConstructors())) {
if (c.isAnnotationPresent(Inject.class)) {
if (inject == null) {
inject = c;
} else {
throw new InjectException(String.format("%s has multiple @Inject constructors", key.getType()));
}
} else if (c.getParameterTypes().length == 0) {
noarg = c;
} else if (innerClass && c.getParameterTypes().length == 1) {
noarg = c;
}
}
final Constructor<T> constructor = inject != null ? inject : noarg;
if (constructor == null) {
throw new InjectException(String.format("%s doesn't have an @Inject or no-arg constructor, or a module provider", key.getType().getName()));
}
constructor.setAccessible(true);
return constructor;
}
示例11: createLoadingRenderer
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
public static LoadingRenderer createLoadingRenderer(Context context, int loadingRendererId) throws Exception {
Class<?> loadingRendererClazz = LOADING_RENDERERS.get(loadingRendererId);
Constructor<?>[] constructors = loadingRendererClazz.getDeclaredConstructors();
for (Constructor<?> constructor : constructors) {
Class<?>[] parameterTypes = constructor.getParameterTypes();
if (parameterTypes != null
&& parameterTypes.length == 1
&& parameterTypes[0].equals(Context.class)) {
constructor.setAccessible(true);
return (LoadingRenderer) constructor.newInstance(context);
}
}
throw new InstantiationException();
}
示例12: testConstructor
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
/**
* Verifies that {@code ctor} produces a {@link NullPointerException} or
* {@link UnsupportedOperationException} whenever <i>any</i> of its
* non-{@link 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);
}
}
示例13: getName
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
/**
* get constructor name.
* "()", "(java.lang.String,int)"
*
* @param c constructor.
* @return name.
*/
public static String getName(final Constructor<?> c)
{
StringBuilder ret = new StringBuilder("(");
Class<?>[] parameterTypes = c.getParameterTypes();
for(int i=0;i<parameterTypes.length;i++)
{
if( i > 0 )
ret.append(',');
ret.append(getName(parameterTypes[i]));
}
ret.append(')');
return ret.toString();
}
示例14: getSignature
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
/**
* Compute the JVM constructor descriptor for the constructor.
*/
static final String getSignature(Constructor cons) {
final StringBuffer sb = new StringBuffer();
sb.append('(');
final Class[] params = cons.getParameterTypes(); // avoid clone
for (int j = 0; j < params.length; j++) {
sb.append(getSignature(params[j]));
}
return sb.append(")V").toString();
}
示例15: testForJAXWSConstructorsInExceptions
import java.lang.reflect.Constructor; //导入方法依赖的package包/类
@Test
public void testForJAXWSConstructorsInExceptions() throws Exception {
StringBuffer errors = new StringBuffer();
List<Class<?>> classes = PackageClassReader.getClasses(
CurrencyException.class, Throwable.class,
ClassFilter.CLASSES_ONLY);
for (Class<?> clazz : getApplicationExceptions(classes)) {
Constructor<?>[] constructors = clazz.getConstructors();
boolean doesConst1Exist = false;
boolean doesConst2Exist = false;
for (Constructor<?> constructor : constructors) {
Class<?>[] types = constructor.getParameterTypes();
if (types.length == 2
&& types[0] == String.class
&& ApplicationExceptionBean.class
.isAssignableFrom(types[1])) {
doesConst1Exist = true;
}
if (types.length == 3
&& types[0] == String.class
&& ApplicationExceptionBean.class
.isAssignableFrom(types[1])
&& types[2] == Throwable.class) {
doesConst2Exist = true;
}
}
if (!doesConst1Exist) {
errors.append("Class ").append(clazz.getName())
.append(" misses constructor C(String, Bean)\n");
}
if (!doesConst2Exist) {
errors.append("Class ")
.append(clazz.getName())
.append(" misses constructor C(String, Bean, Throwable)\n");
}
}
if (errors.length() > 0) {
fail("Exceptions without constructor(String):\n"
+ errors.toString());
}
}