本文整理汇总了Java中java.lang.reflect.Member类的典型用法代码示例。如果您正苦于以下问题:Java Member类的具体用法?Java Member怎么用?Java Member使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Member类属于java.lang.reflect包,在下文中一共展示了Member类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getParameterIndexByType
import java.lang.reflect.Member; //导入依赖的package包/类
/**
* Returns the index of the parameter declared with the given type, ensuring that there is exactly one such parameter.
*
* @throws NoSuchFieldError if there is no or more than one parameter with that type.
*/
public static int getParameterIndexByType(final Member method, final Class<?> type) {
final Class<?>[] classes = (method instanceof Method) ?
((Method) method).getParameterTypes() : ((Constructor<?>) method).getParameterTypes();
int idx = -1;
for (int i = 0; i < classes.length; ++i) {
if (classes[i] == type) {
if (idx == -1) {
idx = i;
} else {
throw new NoSuchFieldError("More than one parameter of type " + type + " found in " + method);
}
}
}
if (idx != -1) {
return idx;
} else {
throw new NoSuchFieldError("No parameter of type " + type + " found in " + method);
}
}
示例2: getTarget
import java.lang.reflect.Member; //导入依赖的package包/类
@Override
MethodHandle getTarget(final CallSiteDescriptor desc) {
final MethodHandles.Lookup lookup = AccessController.doPrivileged(
(PrivilegedAction<MethodHandles.Lookup>)()->desc.getLookup(),
GET_LOOKUP_CONTEXT);
if(target instanceof Method) {
final MethodHandle mh = unreflect(lookup, (Method)target);
if(Modifier.isStatic(((Member)target).getModifiers())) {
return StaticClassIntrospector.editStaticMethodHandle(mh);
}
return mh;
}
return StaticClassIntrospector.editConstructorMethodHandle(unreflectConstructor(lookup,
(Constructor<?>)target));
}
示例3: testPatchingPublic
import java.lang.reflect.Member; //导入依赖的package包/类
public void testPatchingPublic() throws Exception {
Class<?> c = new L().loadClass(C.class.getName());
assertNotSame(c, C.class);
Member m;
m = c.getDeclaredConstructor(boolean.class);
assertEquals(0, m.getModifiers() & Modifier.PUBLIC);
assertEquals(Modifier.PRIVATE, m.getModifiers() & Modifier.PRIVATE);
m = c.getDeclaredConstructor(int.class);
assertEquals(Modifier.PUBLIC, m.getModifiers() & Modifier.PUBLIC);
assertEquals(0, m.getModifiers() & Modifier.PRIVATE);
m = c.getDeclaredMethod("m1");
assertEquals(0, m.getModifiers() & Modifier.PUBLIC);
assertEquals(Modifier.PRIVATE, m.getModifiers() & Modifier.PRIVATE);
m = c.getDeclaredMethod("m2");
assertEquals(Modifier.PUBLIC, m.getModifiers() & Modifier.PUBLIC);
assertEquals(0, m.getModifiers() & Modifier.PRIVATE);
}
示例4: testExecutingConstructor
import java.lang.reflect.Member; //导入依赖的package包/类
public void testExecutingConstructor() throws Exception {
ClassLoader l = new L();
Class<?> c = l.loadClass(CAPI.class.getName());
Member m = c.getDeclaredConstructor(int.class);
Constructor ctor = (Constructor)m;
ctor.setAccessible(true);
Object o = ctor.newInstance(5);
assertSame(c, o.getClass());
assertTrue("Invalid API superclass", Superclazz.class.isInstance(o));
assertEquals("@ConstructorDelegate method did not execute", 5, ((Superclazz)o).val);
Field f = o.getClass().getField("otherVal");
Object v = f.get(o);
assertEquals("Patched API constructor did not execute", v, 1);
}
示例5: typesMatch
import java.lang.reflect.Member; //导入依赖的package包/类
private static boolean typesMatch (Member function, Class<?>[] formals, Object[] actuals) {
if ((actuals.length == formals.length) ||
(actuals.length >= formals.length && isVarArgs(function))) {
int idx = 0;
// check each parameter individually
for (; idx < formals.length - 1; idx++) {
if (!isInstance(formals[idx], actuals[idx])) {
return false;
}
}
// check each of the last actual args to see if they can be one of varargs
Class<?> type =
(formals[idx].isArray()) ? formals[idx].getComponentType() : formals[idx];
for (; idx < actuals.length; idx++) {
if (!isInstance(type, actuals[idx])) {
return false;
}
}
// it was possible, and nothing else returned false, so
return true;
}
// sizes don't match
return false;
}
示例6: getParameterTypes
import java.lang.reflect.Member; //导入依赖的package包/类
/**
* Returns the resolved generic parameter types of {@code methodOrConstructor}.
*
* @param methodOrConstructor a method or constructor defined by this or any supertype.
* @since 2.0
*/
public List<TypeLiteral<?>> getParameterTypes(Member methodOrConstructor) {
Type[] genericParameterTypes;
if (methodOrConstructor instanceof Method) {
Method method = (Method) methodOrConstructor;
if (!method.getDeclaringClass().isAssignableFrom(rawType)) {
throw new IllegalArgumentException(method + " is not defined by a supertype of " + type);
}
genericParameterTypes = method.getGenericParameterTypes();
} else if (methodOrConstructor instanceof Constructor) {
Constructor constructor = (Constructor) methodOrConstructor;
if (!constructor.getDeclaringClass().isAssignableFrom(rawType)) {
throw new IllegalArgumentException(constructor + " does not construct a supertype of " + type);
}
genericParameterTypes = constructor.getGenericParameterTypes();
} else {
throw new IllegalArgumentException("Not a method or a constructor: " + methodOrConstructor);
}
return resolveAll(genericParameterTypes);
}
示例7: getReadMember
import java.lang.reflect.Member; //导入依赖的package包/类
Member getReadMember(Class targetClass) throws NameNotFoundException {
// mapping: public field (same name), method (getAttribute()),
// method (attribute())
List key = new ArrayList();
key.add(targetClass);
key.add(_name);
Member m = (Member) _cache.get(key);
if (m != null)
return m;
m = getReadField(targetClass);
if (m == null)
m = getReadMethod(targetClass);
if (m != null)
_cache.putIfAbsent(key, m);
else
throw new NameNotFoundException(
LocalizedStrings.AttributeDescriptor_NO_PUBLIC_ATTRIBUTE_NAMED_0_WAS_FOUND_IN_CLASS_1
.toLocalizedString(new Object[] {_name, targetClass.getName()}));
// override security for nonpublic derived classes with public members
((AccessibleObject) m).setAccessible(true);
return m;
}
示例8: removePrivateAndSort
import java.lang.reflect.Member; //导入依赖的package包/类
static MethodSignature[] removePrivateAndSort(Member[] m) {
int numNonPrivate = 0;
for (int i = 0; i < m.length; i++) {
if (! Modifier.isPrivate(m[i].getModifiers())) {
numNonPrivate++;
}
}
MethodSignature[] cm = new MethodSignature[numNonPrivate];
int cmi = 0;
for (int i = 0; i < m.length; i++) {
if (! Modifier.isPrivate(m[i].getModifiers())) {
cm[cmi] = new MethodSignature(m[i]);
cmi++;
}
}
if (cmi > 0)
Arrays.sort(cm, cm[0]);
return cm;
}
示例9: forMember
import java.lang.reflect.Member; //导入依赖的package包/类
private List<Dependency<?>> forMember(Member member, TypeLiteral<?> type,
Annotation[][] parameterAnnotations) {
Errors errors = new Errors(member);
Iterator<Annotation[]> annotationsIterator = Arrays.asList(parameterAnnotations).iterator();
List<Dependency<?>> dependencies = new ArrayList<>();
int index = 0;
for (TypeLiteral<?> parameterType : type.getParameterTypes(member)) {
try {
Annotation[] paramAnnotations = annotationsIterator.next();
Key<?> key = Annotations.getKey(parameterType, member, paramAnnotations, errors);
dependencies.add(newDependency(key, Nullability.allowsNull(paramAnnotations), index));
index++;
} catch (ErrorsException e) {
errors.merge(e.getErrors());
}
}
errors.throwConfigurationExceptionIfErrorsExist();
return Collections.unmodifiableList(dependencies);
}
示例10: newFastClassForMember
import java.lang.reflect.Member; //导入依赖的package包/类
/**
* Returns a FastClass proxy for invoking the given member or {@code null} if access rules
* disallow it.
*
* <p>FastClass works by generating a type in the same package as the target {@code type}. This
* may or may not work depending on the access level of the class/member. It breaks down into the
* following cases depending on accessibility:
* <ul>
* <li>Public: This always works since we can generate the type into the
* {@link BridgeClassLoader} which ensures there are no versioning issues.
* <li>Package private and Protected: This works as long as:
* <ul>
* <li>We can generate into the same classloader as the type. This is not possible for JDK
* types which use the 'bootstrap' loader.
* <li>The classloader of the type has the same version of {@code FastClass} as we do. This
* may be violated when running in OSGI bundles.
* </ul>
* <li>Private: This never works.
* </ul>
*
* If we are unable to generate the type, then we return null and callers should work around by
* using normal java reflection.
*/
public static net.sf.cglib.reflect.FastClass newFastClassForMember(Class<?> type, Member member) {
if (!new net.sf.cglib.core.VisibilityPredicate(type, false).evaluate(member)) {
// the member cannot be indexed by fast class. Bail out.
return null;
}
boolean publiclyCallable = isPubliclyCallable(member);
if (!publiclyCallable && !hasSameVersionOfCglib(type.getClassLoader())) {
// The type is in a classloader with a different version of cglib and is not publicly visible
// (so we can't use the bridge classloader to work around). Bail out.
return null;
}
net.sf.cglib.reflect.FastClass.Generator generator
= new net.sf.cglib.reflect.FastClass.Generator();
if (publiclyCallable) {
// Use the bridge classloader if we can
generator.setClassLoader(getClassLoader(type));
}
generator.setType(type);
generator.setNamingPolicy(FASTCLASS_NAMING_POLICY);
if (logger.isLoggable(Level.FINE)) {
logger.fine("Loading " + type + " FastClass with " + generator.getClassLoader());
}
return generator.create();
}
示例11: addInjectorsForMembers
import java.lang.reflect.Member; //导入依赖的package包/类
private static <M extends Member & AnnotatedElement> void addInjectorsForMembers(
TypeLiteral<?> typeLiteral, Factory<M> factory, boolean statics,
Collection<InjectionPoint> injectionPoints, Errors errors) {
for (M member : factory.getMembers(getRawType(typeLiteral.getType()))) {
if (isStatic(member) != statics) {
continue;
}
Inject inject = member.getAnnotation(Inject.class);
if (inject == null) {
continue;
}
try {
injectionPoints.add(factory.create(typeLiteral, member, errors));
} catch (ConfigurationException ignorable) {
if (!inject.optional()) {
errors.merge(ignorable.getErrorMessages());
}
}
}
}
示例12: formatInjectionPoint
import java.lang.reflect.Member; //导入依赖的package包/类
public static void formatInjectionPoint(Formatter formatter, Dependency<?> dependency,
InjectionPoint injectionPoint) {
Member member = injectionPoint.getMember();
Class<? extends Member> memberType = MoreTypes.memberType(member);
if (memberType == Field.class) {
dependency = injectionPoint.getDependencies().get(0);
formatter.format(" while locating %s%n", convert(dependency.getKey()));
formatter.format(" for field at %s%n", StackTraceElements.forMember(member));
} else if (dependency != null) {
formatter.format(" while locating %s%n", convert(dependency.getKey()));
formatter.format(" for parameter %s at %s%n",
dependency.getParameterIndex(), StackTraceElements.forMember(member));
} else {
formatSource(formatter, injectionPoint.getMember());
}
}
示例13: getMemberOf
import java.lang.reflect.Member; //导入依赖的package包/类
/**
* Returns the member that the given node index represents on the given class.
*
* @param index the node index (inclusive)
* @param c the class that have this node
* @param args the arguments that will be used to access this node.
* @return the member
* @see #getMemberOf(int)
* @see #getMemberOf(int, Map)
* @see #getMemberOf(int, Class)
* @see #getLastMember(Class, Map)
* @see #needArguments()
* @throws IndexOutOfBoundsException if the index is invalid
* @throws IllegalArgumentException if the static path expression not support the given root obj
*/
public Member getMemberOf(int index, Class<?> c, Map<String, Object> args) {
checkIfSupport(c, true);
checkRange("Index", index, false);
Member member = null;
int i = 0;
for (ExpressionNode node : NODES) {
member = node.getMember(c, args);
if (i == index) {
return member;
}
if (i < LAST_INDEX) {
c = getMemberType(member);
while (c.isArray()) {
c = c.getComponentType();
}
}
i++;
}
return member;
}
示例14: conservativeCheckMemberAccess
import java.lang.reflect.Member; //导入依赖的package包/类
/**
* Does a conservative approximation of member access check. Use this if
* you don't have an actual 'userland' caller Class/ClassLoader available.
* This might be more restrictive than a precise member access check where
* you have a caller, but should never allow a member access that is
* forbidden.
*
* @param m the {@code Member} about to be accessed
*/
public static void conservativeCheckMemberAccess(Member m) throws SecurityException{
final SecurityManager sm = System.getSecurityManager();
if (sm == null)
return;
// Check for package access on the declaring class.
//
// In addition, unless the member and the declaring class are both
// public check for access declared member permissions.
//
// This is done regardless of ClassLoader relations between the {@code
// Member m} and any potential caller.
final Class<?> declaringClass = m.getDeclaringClass();
checkPackageAccess(declaringClass);
if (Modifier.isPublic(m.getModifiers()) &&
Modifier.isPublic(declaringClass.getModifiers()))
return;
// Check for declared member access.
sm.checkPermission(SecurityConstants.CHECK_MEMBER_ACCESS_PERMISSION);
}
示例15: setAccessibleWorkaround
import java.lang.reflect.Member; //导入依赖的package包/类
static boolean setAccessibleWorkaround(final AccessibleObject o) {
if (o == null || o.isAccessible()) {
return false;
}
final Member m = (Member) o;
if (!o.isAccessible() && Modifier.isPublic(m.getModifiers()) && isPackageAccess(m
.getDeclaringClass().getModifiers())) {
try {
o.setAccessible(true);
return true;
} catch (final SecurityException e) { // NOPMD
// ignore in favor of subsequent IllegalAccessException
}
}
return false;
}