public static String callToString(Constructor thiz) { return thiz.toString(); }
@Override public String toString() { return constructor.toString(); } }
@Override public String toString() { if (m_method != null) return m_method.toString(); else return m_constructor.toString(); } }
@Override public String toString() { if (m_method != null) return m_method.toString(); else return m_constructor.toString(); }
private static Type propertyTypeFromConstructor(Constructor maybeConstructor) { Type[] paramTypes = maybeConstructor.getGenericParameterTypes(); if (paramTypes.length != 1) { throw new IllegalStateException("Constructor '" + maybeConstructor.toString() + "' is annotated with @BQConfigProperty, but does not match expected signature." + " It must take exactly one parameter"); } return paramTypes[0]; }
protected ConfigurationSerializable deserializeViaCtor(Constructor<? extends ConfigurationSerializable> ctor, Map<String, ?> args) { try { return ctor.newInstance(args); } catch (Throwable ex) { Logger.getLogger(ConfigurationSerialization.class.getName()).log( Level.SEVERE, "Could not call constructor '" + ctor.toString() + "' of " + clazz + " for deserialization", ex instanceof InvocationTargetException ? ex.getCause() : ex); } return null; }
@SuppressWarnings("unchecked") private static <V> V instantiate(String policyClassName, Integer[] args, String raw) throws Exception { for (Constructor<?> con : Class.forName(policyClassName).getConstructors()) { Class<?>[] parameterTypes = con.getParameterTypes(); // match constructor by number of arguments first if (args.length != parameterTypes.length) continue; // check if the constructor parameter types are compatible with argument types (which are integer) // note that we allow long.class arguments too because integer is cast to long by runtime. boolean intsOrLongs = true; for (Class<?> pc : parameterTypes) { if (!pc.equals(int.class) && !pc.equals(long.class)) { intsOrLongs = false; break; } } // we found a constructor with required number of parameters but times didn't match, let's carry on if (!intsOrLongs) continue; if (log.isDebugEnabled()) log.debug("About to instantiate class {} with {} arguments", con.toString(), args.length); return (V) con.newInstance(args); } throw new Exception("Failed to identify a class matching the Astyanax Retry Policy config string \"" + raw + "\""); }
public MetaConstructor(Constructor<?> constructor, MetaClass methodClass) { super(StringUtil.getUnqualifiedMemberName(constructor.getName())); this.constructorToString = constructor.toString(); this.metaClass = methodClass; returnType = Void.TYPE; paramTypes = Arrays.asList(constructor.getParameterTypes()); modifier = constructor.getModifiers(); isVarArgs = constructor.isVarArgs(); if (DEBUG_MEMBER_CREATION) { logger.debug("Created MetaConstructor: {}", toString()); } }
public MetaConstructor(Constructor<?> constructor, MetaClass methodClass) { super(StringUtil.getUnqualifiedMemberName(constructor.getName())); this.constructorToString = constructor.toString(); this.metaClass = methodClass; returnType = Void.TYPE; paramTypes = Arrays.asList(constructor.getParameterTypes()); modifier = constructor.getModifiers(); isVarArgs = constructor.isVarArgs(); if (DEBUG_MEMBER_CREATION) { logger.debug("Created MetaConstructor: {}", toString()); } }
if (!checkAccessible(caller, this)) { throw new IllegalAccessException(String.format("Attempt to access constructor %s(%s) from class %s", clazz.getName(), toString(parameterTypes), caller.getName()));
".\n Constructor: " + constructor.toString() + ".\n Arguments: " + argumentTypes, ex);
@Override public String toString() { return constructor.toString(); } }
Class obj2Class = object2.getClass(); try { Constructor[] ctors = obj2Class.getDeclaredConstructors(); for (Constructor cc : ctors) { System.out.println("my ctor is " + cc.toString()); } } catch (Exception ex) { ex.printStackTrace(); }
Assert.assertEquals("public reflection.constructors.ClassForNewInstance(char,char)", c.toString());
public static Object createObject(Constructor constructor, Object[] arguments) { System.out.println("Constructor: " + constructor.toString()); Object object = null; try { object = constructor.newInstance(arguments); System.out.println("Object: " + object.toString()); return object; } catch (InstantiationException e) { //handle it } catch (IllegalAccessException e) { //handle it } catch (IllegalArgumentException e) { //handle it } catch (InvocationTargetException e) { //handle it } return object; } }
@Test public void testMatchingConstructor() throws Exception { final Constructor<?> reference = X.class.getConstructor(A.class, A.class); assertNotNull(reference); for (Constructor<?> c : X.class.getConstructors()) { System.out.println(c.toString()); } assertNotNull(ReflectionUtil.getMatchingConstructorForClass(X.class, A.class, A.class)); assertEquals(reference, ReflectionUtil.getMatchingConstructorForClass(X.class, A.class, A.class)); assertNotNull(ReflectionUtil.getMatchingConstructorForClass(X.class, A.class, C.class)); assertNotNull(ReflectionUtil.getMatchingConstructorForClass(X.class, A.class)); assertNotNull(ReflectionUtil.getMatchingConstructorForClass(X.class, B.class)); assertNull(ReflectionUtil.getMatchingConstructorForClass(X.class, A.class, B.class)); // type erasure :( assertNull(ReflectionUtil.getMatchingConstructorForClass(X.class, B.class, B.class)); assertNull(ReflectionUtil.getMatchingConstructorForClass(X.class, B.class, A.class)); assertNull(ReflectionUtil.getMatchingConstructorForClass(X.class, A.class, B.class, B.class)); assertNull(ReflectionUtil.getMatchingConstructorForClass(X.class)); }
throw new RuntimeException(Messages.MESSAGES.failedToConstruct(constructor.toString()), e); throw new RuntimeException(Messages.MESSAGES.failedToConstruct(constructor.toString()), e); throw new RuntimeException(Messages.MESSAGES.failedToConstruct(constructor.toString()), e.getCause()); String msg = Messages.MESSAGES.badArguments(constructor.toString() + " ("); boolean first = false; for (Object arg : args)
throw new InternalServerErrorException(Messages.MESSAGES.failedProcessingArguments(constructor.toString()), e); }).thenApply(args -> { try throw new InternalServerErrorException(Messages.MESSAGES.failedToConstruct(constructor.toString()), e); throw new InternalServerErrorException(Messages.MESSAGES.failedToConstruct(constructor.toString()), e); throw new ApplicationException(Messages.MESSAGES.failedToConstruct(constructor.toString()), e.getCause()); String msg = Messages.MESSAGES.badArguments(constructor.toString() + " ("); boolean first = false; for (Object arg : args)
fail( "Unexpected constructor: " + c.toString() );