本文整理匯總了Java中java.lang.reflect.Constructor.getDeclaringClass方法的典型用法代碼示例。如果您正苦於以下問題:Java Constructor.getDeclaringClass方法的具體用法?Java Constructor.getDeclaringClass怎麽用?Java Constructor.getDeclaringClass使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.lang.reflect.Constructor
的用法示例。
在下文中一共展示了Constructor.getDeclaringClass方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getAccessibleConstructor
import java.lang.reflect.Constructor; //導入方法依賴的package包/類
/**
* 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;
}
示例2: if
import java.lang.reflect.Constructor; //導入方法依賴的package包/類
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;
}
示例3: 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);
}
}
示例4: newConstructorAccessor
import java.lang.reflect.Constructor; //導入方法依賴的package包/類
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;
}
}
示例5: if
import java.lang.reflect.Constructor; //導入方法依賴的package包/類
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;
}
示例6: newConstructorAccessor
import java.lang.reflect.Constructor; //導入方法依賴的package包/類
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;
}
}
示例7: if
import java.lang.reflect.Constructor; //導入方法依賴的package包/類
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;
}
示例8: newConstructorAccessor
import java.lang.reflect.Constructor; //導入方法依賴的package包/類
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);
}
示例9: onInstantiation
import java.lang.reflect.Constructor; //導入方法依賴的package包/類
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);
}
示例10: if
import java.lang.reflect.Constructor; //導入方法依賴的package包/類
/**
* 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);
}
示例11: lookupParameterNames
import java.lang.reflect.Constructor; //導入方法依賴的package包/類
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;
}
示例12: forConstructor
import java.lang.reflect.Constructor; //導入方法依賴的package包/類
/**
* 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);
}
示例13: lookupParameterNames
import java.lang.reflect.Constructor; //導入方法依賴的package包/類
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);
}
}
示例14: AbstractBean
import java.lang.reflect.Constructor; //導入方法依賴的package包/類
public AbstractBean(String name, Constructor constructor, BeanScope beanScope) {
this.name = name;
this.constructor = new BeanConstructor(constructor);
this.beanType = constructor.getDeclaringClass();
this.beanScope = beanScope;
}
示例15: lookupParameterNames
import java.lang.reflect.Constructor; //導入方法依賴的package包/類
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;
}