Java Code Examples for java.lang.reflect.Constructor.getDeclaringClass()

The following are Jave code examples for showing how to use getDeclaringClass() of the java.lang.reflect.Constructor class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: NotifyTools   File: ConstructorUtils.java   View Source Code Vote up 6 votes
/**
 * Returns accessible version of the given constructor.
 * @param <T> the type of the constructor
 * @param ctor prototype constructor object.
 * @return <code>null</code> if accessible constructor can not be found.
 * @see java.lang.SecurityManager
 */
public static <T> Constructor<T> getAccessibleConstructor(final Constructor<T> ctor) {

    // Make sure we have a method to check
    if (ctor == null) {
        return (null);
    }

    // If the requested method is not public we cannot call it
    if (!Modifier.isPublic(ctor.getModifiers())) {
        return (null);
    }

    // If the declaring class is public, we are done
    final Class<T> clazz = ctor.getDeclaringClass();
    if (Modifier.isPublic(clazz.getModifiers())) {
        return (ctor);
    }

    // what else can we do?
    return null;
}
 
Example 2
Project: OpenJSharp   File: ReflectionFactory.java   View Source Code Vote up 6 votes
public Constructor<?> newConstructorForSerialization
    (Class<?> classToInstantiate, Constructor<?> constructorToCall)
{
    // Fast path
    if (constructorToCall.getDeclaringClass() == classToInstantiate) {
        return constructorToCall;
    }

    ConstructorAccessor acc = newConstructorAccessorForSerialization(classToInstantiate, constructorToCall);
    Constructor<?> c = newConstructor(constructorToCall.getDeclaringClass(),
                                      constructorToCall.getParameterTypes(),
                                      constructorToCall.getExceptionTypes(),
                                      constructorToCall.getModifiers(),
                                      langReflectAccess().
                                      getConstructorSlot(constructorToCall),
                                      langReflectAccess().
                                      getConstructorSignature(constructorToCall),
                                      langReflectAccess().
                                      getConstructorAnnotations(constructorToCall),
                                      langReflectAccess().
                                      getConstructorParameterAnnotations(constructorToCall));
    setConstructorAccessor(c, acc);
    return c;
}
 
Example 3
Project: guava-mock   File: NullPointerTester.java   View Source Code Vote up 5 votes
/**
 * 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);
  }
}
 
Example 4
Project: javaide   File: ReflectionFactory.java   View Source Code Vote up 5 votes
public ConstructorAccessor newConstructorAccessor(Constructor c) {
    checkInitted();

    Class declaringClass = c.getDeclaringClass();
    if (Modifier.isAbstract(declaringClass.getModifiers())) {
        return new InstantiationExceptionConstructorAccessorImpl(null);
    }
    if (declaringClass == Class.class) {
        return new InstantiationExceptionConstructorAccessorImpl
            ("Can not instantiate java.lang.Class");
    }
    // Bootstrapping issue: since we use Class.newInstance() in
    // the ConstructorAccessor generation process, we have to
    // break the cycle here.
    if (Reflection.isSubclassOf(declaringClass,
                                ConstructorAccessorImpl.class)) {
        return new BootstrapConstructorAccessorImpl(c);
    }

    if (noInflation) {
        return new MethodAccessorGenerator().
            generateConstructor(c.getDeclaringClass(),
                                c.getParameterTypes(),
                                c.getExceptionTypes(),
                                c.getModifiers());
    } else {
        NativeConstructorAccessorImpl acc =
            new NativeConstructorAccessorImpl(c);
        DelegatingConstructorAccessorImpl res =
            new DelegatingConstructorAccessorImpl(acc);
        acc.setParent(res);
        return res;
    }
}
 
Example 5
Project: javaide   File: ReflectionFactory.java   View Source Code Vote up 5 votes
public Constructor newConstructorForSerialization
    (Class classToInstantiate, Constructor constructorToCall)
{
    // Fast path
    if (constructorToCall.getDeclaringClass() == classToInstantiate) {
        return constructorToCall;
    }

    ConstructorAccessor acc = new MethodAccessorGenerator().
        generateSerializationConstructor(classToInstantiate,
                                         constructorToCall.getParameterTypes(),
                                         constructorToCall.getExceptionTypes(),
                                         constructorToCall.getModifiers(),
                                         constructorToCall.getDeclaringClass());
    Constructor c = newConstructor(constructorToCall.getDeclaringClass(),
                                   constructorToCall.getParameterTypes(),
                                   constructorToCall.getExceptionTypes(),
                                   constructorToCall.getModifiers(),
                                   langReflectAccess().
                                   getConstructorSlot(constructorToCall),
                                   langReflectAccess().
                                   getConstructorSignature(constructorToCall),
                                   langReflectAccess().
                                   getConstructorAnnotations(constructorToCall),
                                   langReflectAccess().
                                   getConstructorParameterAnnotations(constructorToCall));
    setConstructorAccessor(c, acc);
    return c;
}
 
Example 6
Project: OpenJSharp   File: ReflectionFactory.java   View Source Code Vote up 5 votes
public ConstructorAccessor newConstructorAccessor(Constructor<?> c) {
    checkInitted();

    Class<?> declaringClass = c.getDeclaringClass();
    if (Modifier.isAbstract(declaringClass.getModifiers())) {
        return new InstantiationExceptionConstructorAccessorImpl(null);
    }
    if (declaringClass == Class.class) {
        return new InstantiationExceptionConstructorAccessorImpl
            ("Can not instantiate java.lang.Class");
    }
    // Bootstrapping issue: since we use Class.newInstance() in
    // the ConstructorAccessor generation process, we have to
    // break the cycle here.
    if (Reflection.isSubclassOf(declaringClass,
                                ConstructorAccessorImpl.class)) {
        return new BootstrapConstructorAccessorImpl(c);
    }

    if (noInflation && !ReflectUtil.isVMAnonymousClass(c.getDeclaringClass())) {
        return new MethodAccessorGenerator().
            generateConstructor(c.getDeclaringClass(),
                                c.getParameterTypes(),
                                c.getExceptionTypes(),
                                c.getModifiers());
    } else {
        NativeConstructorAccessorImpl acc =
            new NativeConstructorAccessorImpl(c);
        DelegatingConstructorAccessorImpl res =
            new DelegatingConstructorAccessorImpl(acc);
        acc.setParent(res);
        return res;
    }
}
 
Example 7
Project: OpenJSharp   File: ReflectionFactory.java   View Source Code Vote up 5 votes
public Constructor<?> newConstructorForSerialization
    (Class<?> classToInstantiate, Constructor<?> constructorToCall)
{
    // Fast path
    if (constructorToCall.getDeclaringClass() == classToInstantiate) {
        return constructorToCall;
    }

    ConstructorAccessor acc = new MethodAccessorGenerator().
        generateSerializationConstructor(classToInstantiate,
                                         constructorToCall.getParameterTypes(),
                                         constructorToCall.getExceptionTypes(),
                                         constructorToCall.getModifiers(),
                                         constructorToCall.getDeclaringClass());
    Constructor<?> c = newConstructor(constructorToCall.getDeclaringClass(),
                                      constructorToCall.getParameterTypes(),
                                      constructorToCall.getExceptionTypes(),
                                      constructorToCall.getModifiers(),
                                      langReflectAccess().
                                      getConstructorSlot(constructorToCall),
                                      langReflectAccess().
                                      getConstructorSignature(constructorToCall),
                                      langReflectAccess().
                                      getConstructorAnnotations(constructorToCall),
                                      langReflectAccess().
                                      getConstructorParameterAnnotations(constructorToCall));
    setConstructorAccessor(c, acc);
    return c;
}
 
Example 8
Project: OpenJSharp   File: ReflectionFactory.java   View Source Code Vote up 5 votes
public ConstructorAccessor newConstructorAccessor(Constructor<?> c) {
    Class<?> declaringClass = c.getDeclaringClass();
    if (Modifier.isAbstract(declaringClass.getModifiers())) {
        return new InstantiationExceptionConstructorAccessorImpl(null);
    }
    if (declaringClass == Class.class) {
        return new InstantiationExceptionConstructorAccessorImpl
            ("Can not instantiate java.lang.Class");
    }
    return newConstructorAccessor0(c);
}
 
Example 9
Project: ArchUnit   File: ArchUnitException.java   View Source Code Vote up 5 votes
public static ClassResolverConfigurationException onInstantiation(
        Constructor<?> constructor, List<String> args, Exception cause) {

    Class<?> declaringClass = constructor.getDeclaringClass();
    String message = String.format("class %s threw an exception in constructor %s('%s')",
            declaringClass.getName(), declaringClass.getSimpleName(), Joiner.on("', '").join(args));
    return new ClassResolverConfigurationException(message, cause);
}
 
Example 10
Project: jdk8u-jdk   File: ReflectionFactory.java   View Source Code Vote up 5 votes
public ConstructorAccessor newConstructorAccessor(Constructor<?> c) {
    checkInitted();

    Class<?> declaringClass = c.getDeclaringClass();
    if (Modifier.isAbstract(declaringClass.getModifiers())) {
        return new InstantiationExceptionConstructorAccessorImpl(null);
    }
    if (declaringClass == Class.class) {
        return new InstantiationExceptionConstructorAccessorImpl
            ("Can not instantiate java.lang.Class");
    }
    // Bootstrapping issue: since we use Class.newInstance() in
    // the ConstructorAccessor generation process, we have to
    // break the cycle here.
    if (Reflection.isSubclassOf(declaringClass,
                                ConstructorAccessorImpl.class)) {
        return new BootstrapConstructorAccessorImpl(c);
    }

    if (noInflation && !ReflectUtil.isVMAnonymousClass(c.getDeclaringClass())) {
        return new MethodAccessorGenerator().
            generateConstructor(c.getDeclaringClass(),
                                c.getParameterTypes(),
                                c.getExceptionTypes(),
                                c.getModifiers());
    } else {
        NativeConstructorAccessorImpl acc =
            new NativeConstructorAccessorImpl(c);
        DelegatingConstructorAccessorImpl res =
            new DelegatingConstructorAccessorImpl(acc);
        acc.setParent(res);
        return res;
    }
}
 
Example 11
Project: jdk8u-jdk   File: ReflectionFactory.java   View Source Code Vote up 5 votes
/**
 * Returns an accessible constructor capable of creating instances
 * of the given class, initialized by the given constructor.
 *
 * @param classToInstantiate the class to instantiate
 * @param constructorToCall the constructor to call
 * @return an accessible constructor
 */
public Constructor<?> newConstructorForSerialization
    (Class<?> classToInstantiate, Constructor<?> constructorToCall)
{
    // Fast path
    if (constructorToCall.getDeclaringClass() == classToInstantiate) {
        return constructorToCall;
    }
    return generateConstructor(classToInstantiate, constructorToCall);
}
 
Example 12
Project: X4J   File: DefaultParanamer.java   View Source Code Vote up 5 votes
public String[] lookupParameterNames(AccessibleObject methodOrCtor, boolean throwExceptionIfMissing) {
    // Oh for some commonality between Constructor and Method !!
    Class<?>[] types = null;
    Class<?> declaringClass = null;
    String name = null;
    if (methodOrCtor instanceof Method) {
        Method method = (Method) methodOrCtor;
        types = method.getParameterTypes();
        name = method.getName();
        declaringClass = method.getDeclaringClass();
    } else {
        Constructor<?> constructor = (Constructor<?>) methodOrCtor;
        types = constructor.getParameterTypes();
        declaringClass = constructor.getDeclaringClass();
        name = "<init>";
    }

    if (types.length == 0) {
        return EMPTY_NAMES;
    }
    final String parameterTypeNames = getParameterTypeNamesCSV(types);
    final String[] names = getParameterNames(declaringClass, parameterTypeNames, name + SPACE);
    if ( names == null ){
        if (throwExceptionIfMissing) {
        throw new ParameterNamesNotFoundException("No parameter names found for class '"+declaringClass+"', methodOrCtor " + name
                +" and parameter types "+parameterTypeNames);
        } else {
            return Paranamer.EMPTY_NAMES;
        }
    }
    return names;
}
 
Example 13
Project: googles-monorepo-demo   File: NullPointerTester.java   View Source Code Vote up 5 votes
/**
 * 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);
  }
}
 
Example 14
Project: businessworks   File: InjectionPoint.java   View Source Code Vote up 5 votes
/**
 * Returns a new injection point for the specified constructor of {@code type}.
 *
 * @param constructor any single constructor present on {@code type}.
 * @param type the concrete type that defines {@code constructor}.
 * 
 * @since 3.0
 */
public static <T> InjectionPoint forConstructor(
    Constructor<T> constructor, TypeLiteral<? extends T> type) {
  if (type.getRawType() != constructor.getDeclaringClass()) {
    new Errors(type)
        .constructorNotDefinedByType(constructor, type)
        .throwConfigurationExceptionIfErrorsExist();
  }

  return new InjectionPoint(type, constructor);
}
 
Example 15
Project: openjdk-jdk10   File: ReflectionFactory.java   View Source Code Vote up 5 votes
public ConstructorAccessor newConstructorAccessor(Constructor<?> c) {
    checkInitted();

    Class<?> declaringClass = c.getDeclaringClass();
    if (Modifier.isAbstract(declaringClass.getModifiers())) {
        return new InstantiationExceptionConstructorAccessorImpl(null);
    }
    if (declaringClass == Class.class) {
        return new InstantiationExceptionConstructorAccessorImpl
            ("Can not instantiate java.lang.Class");
    }
    // Bootstrapping issue: since we use Class.newInstance() in
    // the ConstructorAccessor generation process, we have to
    // break the cycle here.
    if (Reflection.isSubclassOf(declaringClass,
                                ConstructorAccessorImpl.class)) {
        return new BootstrapConstructorAccessorImpl(c);
    }

    if (noInflation && !ReflectUtil.isVMAnonymousClass(c.getDeclaringClass())) {
        return new MethodAccessorGenerator().
            generateConstructor(c.getDeclaringClass(),
                                c.getParameterTypes(),
                                c.getExceptionTypes(),
                                c.getModifiers());
    } else {
        NativeConstructorAccessorImpl acc =
            new NativeConstructorAccessorImpl(c);
        DelegatingConstructorAccessorImpl res =
            new DelegatingConstructorAccessorImpl(acc);
        acc.setParent(res);
        return res;
    }
}
 
Example 16
Project: GitHub   File: ASMUtils.java   View Source Code Vote up 4 votes
public static String[] lookupParameterNames(AccessibleObject methodOrCtor) {
    if (IS_ANDROID) {
        return new String[0];
    }

    final Class<?>[] types;
    final Class<?> declaringClass;
    final String name;

    if (methodOrCtor instanceof Method) {
        Method method = (Method) methodOrCtor;
        types = method.getParameterTypes();
        name = method.getName();
        declaringClass = method.getDeclaringClass();
    } else {
        Constructor<?> constructor = (Constructor<?>) methodOrCtor;
        types = constructor.getParameterTypes();
        declaringClass = constructor.getDeclaringClass();
        name = "<init>";
    }

    if (types.length == 0) {
        return new String[0];
    }

    ClassLoader classLoader = declaringClass.getClassLoader();
    if (classLoader == null) {
        classLoader = ClassLoader.getSystemClassLoader();
    }

    String className = declaringClass.getName();
    String resourceName = className.replace('.', '/') + ".class";
    InputStream is = classLoader.getResourceAsStream(resourceName);

    if (is == null) {
        return new String[0];
    }

    try {
        ClassReader reader = new ClassReader(is);
        TypeCollector visitor = new TypeCollector(name, types);
        reader.accept(visitor);
        return visitor.getParameterNamesForMethod();
    } catch (IOException e) {
        return new String[0];
    } finally {
        IOUtils.close(is);
    }
}
 
Example 17
Project: Samurai   File: AbstractBean.java   View Source Code Vote up 4 votes
public AbstractBean(String name, Constructor constructor, BeanScope beanScope) {
    this.name = name;
    this.constructor = new BeanConstructor(constructor);
    this.beanType = constructor.getDeclaringClass();
    this.beanScope = beanScope;
}
 
Example 18
Project: X4J   File: AnnotationParanamer.java   View Source Code Vote up 4 votes
public String[] lookupParameterNames(AccessibleObject methodOrCtor, boolean throwExceptionIfMissing) {
	// Oh for some commonality between Constructor and Method !!
	Class<?>[] types = null;
	Class<?> declaringClass = null;
	String name = null;
	Annotation[][] anns = null;
	if (methodOrCtor instanceof Method) {
		Method method = (Method) methodOrCtor;
		types = method.getParameterTypes();
		name = method.getName();
		declaringClass = method.getDeclaringClass();
		anns = method.getParameterAnnotations();
	} else {
		Constructor<?> constructor = (Constructor<?>) methodOrCtor;
		types = constructor.getParameterTypes();
		declaringClass = constructor.getDeclaringClass();
		name = "<init>";
		anns = constructor.getParameterAnnotations();
	}

	if (types.length == 0) {
		return EMPTY_NAMES;
	}

	final String[] names = new String[types.length];
	boolean allDone = true;
	for (int i = 0; i < names.length; i++) {
		for (int j = 0; j < anns[i].length; j++) {
			Annotation ann = anns[i][j];
			if (isNamed(ann)) {
				names[i] = getNamedValue(ann);
				break;
			}
		}
		if (names[i] == null) {
			allDone = false;
		}

	}

	// fill in blanks from fallback if possible.
	if (!allDone) {
		allDone = true;
		String[] altNames = fallback.lookupParameterNames(methodOrCtor, false);
		if (altNames.length > 0) {
			for (int i = 0; i < names.length; i++) {
				if (names[i] == null) {
					if (altNames[i] != null) {
						names[i] = altNames[i];
					} else {
						allDone = false;
					}
				}
			}
		} else {
			allDone = false;
		}
	}

	// error if applicable
	if (!allDone) {
		if (throwExceptionIfMissing) {
			throw new ParameterNamesNotFoundException("One or more @Named annotations missing for class '"
					+ declaringClass.getName() + "', methodOrCtor " + name + " and parameter types "
					+ DefaultParanamer.getParameterTypeNamesCSV(types));
		} else {
			return Paranamer.EMPTY_NAMES;
		}
	}
	return names;
}
 
Example 19
Project: X4J   File: AnnotationParanamer.java   View Source Code Vote up 4 votes
public String[] lookupParameterNames(AccessibleObject methodOrCtor, boolean throwExceptionIfMissing) {
	// Oh for some commonality between Constructor and Method !!
	Class<?>[] types = null;
	Class<?> declaringClass = null;
	String name = null;
	Annotation[][] anns = null;
	if (methodOrCtor instanceof Method) {
		Method method = (Method) methodOrCtor;
		types = method.getParameterTypes();
		name = method.getName();
		declaringClass = method.getDeclaringClass();
		anns = method.getParameterAnnotations();
	} else {
		Constructor<?> constructor = (Constructor<?>) methodOrCtor;
		types = constructor.getParameterTypes();
		declaringClass = constructor.getDeclaringClass();
		name = "<init>";
		anns = constructor.getParameterAnnotations();
	}

	if (types.length == 0) {
		return EMPTY_NAMES;
	}

	final String[] names = new String[types.length];
	boolean allDone = true;
	for (int i = 0; i < names.length; i++) {
		for (int j = 0; j < anns[i].length; j++) {
			Annotation ann = anns[i][j];
			if (isNamed(ann)) {
				names[i] = getNamedValue(ann);
				break;
			}
		}
		if (names[i] == null) {
			allDone = false;
		}

	}

	// fill in blanks from fallback if possible.
	if (!allDone) {
		allDone = true;
		String[] altNames = fallback.lookupParameterNames(methodOrCtor, false);
		if (altNames.length > 0) {
			for (int i = 0; i < names.length; i++) {
				if (names[i] == null) {
					if (altNames[i] != null) {
						names[i] = altNames[i];
					} else {
						allDone = false;
					}
				}
			}
		} else {
			allDone = false;
		}
	}

	// error if applicable
	if (!allDone) {
		if (throwExceptionIfMissing) {
			throw new ParameterNamesNotFoundException("One or more @Named annotations missing for class '"
					+ declaringClass.getName() + "', methodOrCtor " + name + " and parameter types "
					+ DefaultParanamer.getParameterTypeNamesCSV(types));
		} else {
			return Paranamer.EMPTY_NAMES;
		}
	}
	return names;
}
 
Example 20
Project: lams   File: UnsavedValueFactory.java   View Source Code Vote up 3 votes
/**
 * Instantiate a class using the provided Constructor
 *
 * @param constructor The constructor
 *
 * @return The instantiated object
 *
 * @throws InstantiationException if something went wrong
 */
private static Object instantiate(Constructor constructor) {
	try {
		return constructor.newInstance();
	}
	catch (Exception e) {
		throw new InstantiationException( "could not instantiate test object", constructor.getDeclaringClass(), e );
	}
}