下面列出了怎么用java.beans.MethodDescriptor的API类实例代码及写法,或者点击链接到github查看源代码。
public void testEventSetDescriptorStringClassMethodDescriptorArrayMethodMethod_ListenerMDNull()
throws IntrospectionException, NoSuchMethodException {
String eventSetName = "MockPropertyChange";
Class<?> listenerType = MockPropertyChangeListener.class;
Class<MockSourceClass> sourceClass = MockSourceClass.class;
Method addMethod = sourceClass.getMethod(
"addMockPropertyChangeListener", listenerType);
Method removeMethod = sourceClass.getMethod(
"removeMockPropertyChangeListener", listenerType);
EventSetDescriptor esd = new EventSetDescriptor(eventSetName,
listenerType, (MethodDescriptor[]) null, addMethod,
removeMethod);
assertNull(esd.getListenerMethodDescriptors());
assertNull(esd.getListenerMethods());
}
public void testMethodDescriptorMethod() throws SecurityException,
NoSuchMethodException {
String beanName = "MethodDescriptorTest.bean";
MockJavaBean bean = new MockJavaBean(beanName);
Method method = bean.getClass()
.getMethod("getBeanName", (Class[]) null);
MethodDescriptor md = new MethodDescriptor(method);
assertSame(method, md.getMethod());
assertNull(md.getParameterDescriptors());
assertEquals(method.getName(), md.getDisplayName());
assertEquals(method.getName(), md.getName());
assertEquals(method.getName(), md.getShortDescription());
assertNotNull(md.attributeNames());
assertFalse(md.isExpert());
assertFalse(md.isHidden());
assertFalse(md.isPreferred());
}
public void testSetInDefaultEventSet_false() throws SecurityException,
NoSuchMethodException, IntrospectionException {
String eventSetName = "MockPropertyChange";
Class<?> listenerType = MockPropertyChangeListener.class;
Method[] listenerMethods = {
listenerType.getMethod("mockPropertyChange",
new Class[] { MockPropertyChangeEvent.class }),
listenerType.getMethod("mockPropertyChange2",
new Class[] { MockPropertyChangeEvent.class }), };
MethodDescriptor[] listenerMethodDescriptors = {
new MethodDescriptor(listenerMethods[0]),
new MethodDescriptor(listenerMethods[1]), };
Class<MockSourceClass> sourceClass = MockSourceClass.class;
Method addMethod = sourceClass.getMethod(
"addMockPropertyChangeListener", new Class[] { listenerType });
Method removeMethod = sourceClass.getMethod(
"removeMockPropertyChangeListener",
new Class[] { listenerType });
EventSetDescriptor esd = new EventSetDescriptor(eventSetName,
listenerType, listenerMethodDescriptors, addMethod,
removeMethod);
assertTrue(esd.isInDefaultEventSet());
esd.setInDefaultEventSet(false);
assertFalse(esd.isInDefaultEventSet());
}
private List<Method> findCandidateWriteMethods(MethodDescriptor[] methodDescriptors) {
List<Method> matches = new ArrayList<Method>();
for (MethodDescriptor methodDescriptor : methodDescriptors) {
Method method = methodDescriptor.getMethod();
if (isCandidateWriteMethod(method)) {
matches.add(method);
}
}
// Sort non-void returning write methods to guard against the ill effects of
// non-deterministic sorting of methods returned from Class#getDeclaredMethods
// under JDK 7. See http://bugs.sun.com/view_bug.do?bug_id=7023180
Collections.sort(matches, new Comparator<Method>() {
@Override
public int compare(Method m1, Method m2) {
return m2.toString().compareTo(m1.toString());
}
});
return matches;
}
private static Method findSetter(Class<?> clazz, String setterName, String key, Class<?> paramClazz, boolean lenient) {
BeanInfo beanInfo;
try {
beanInfo = Introspector.getBeanInfo(clazz);
} catch (IntrospectionException ie) {
if (lenient) {
return null;
}
throw new RuntimeException("Error getting bean info for class : " + clazz.getName(), ie);
}
for (final boolean matchParamClazz: new boolean[]{true, false}) {
for (final MethodDescriptor desc : beanInfo.getMethodDescriptors()) {
final Method m = desc.getMethod();
final Class<?> p[] = m.getParameterTypes();
if (m.getName().equals(setterName) && p.length == 1 &&
(!matchParamClazz || paramClazz.equals(p[0]))) {
return m;
}
}
}
if (lenient) {
return null;
}
throw new RuntimeException("No setter corrresponding to '" + key + "' in " + clazz.getName());
}
public BeanInfo getBeanInfo(Class<?> beanClass) throws IntrospectionException {
Introspector.flushFromCaches(beanClass);
BeanInfo bi = Introspector.getBeanInfo(beanClass);
BeanDescriptor bd = bi.getBeanDescriptor();
MethodDescriptor mds[] = bi.getMethodDescriptors();
EventSetDescriptor esds[] = bi.getEventSetDescriptors();
PropertyDescriptor pds[] = bi.getPropertyDescriptors();
List<PropertyDescriptor> filteredPDList = new ArrayList<PropertyDescriptor>();
List<String> nonPropList = Arrays.asList(getNonProperties());
for(PropertyDescriptor pd : pds){
if(!nonPropList.contains(pd.getName()) && pd.getWriteMethod() != null && pd.getReadMethod() != null)
filteredPDList.add(pd);
}
int defaultEvent = bi.getDefaultEventIndex();
int defaultProperty = bi.getDefaultPropertyIndex();
return new GenericBeanInfo(bd, esds, defaultEvent,
filteredPDList.toArray(new PropertyDescriptor[filteredPDList.size()]),
defaultProperty, mds, null);
}
public void testGetBeanInfo_StaticMethods() throws Exception {
BeanInfo beanInfo = Introspector.getBeanInfo(StaticClazz.class);
PropertyDescriptor[] propertyDescriptors = beanInfo
.getPropertyDescriptors();
assertEquals(1, propertyDescriptors.length);
assertTrue(contains("class", Class.class, propertyDescriptors));
MethodDescriptor[] methodDescriptors = beanInfo.getMethodDescriptors();
assertTrue(contains("getStaticMethod", methodDescriptors));
assertTrue(contains("setStaticMethod", methodDescriptors));
beanInfo = Introspector.getBeanInfo(StaticClazzWithProperty.class);
propertyDescriptors = beanInfo.getPropertyDescriptors();
assertEquals(1, propertyDescriptors.length);
methodDescriptors = beanInfo.getMethodDescriptors();
assertTrue(contains("getStaticName", methodDescriptors));
assertTrue(contains("setStaticName", methodDescriptors));
}
private List<Method> findCandidateWriteMethods(MethodDescriptor[] methodDescriptors) {
List<Method> matches = new ArrayList<Method>();
for (MethodDescriptor methodDescriptor : methodDescriptors) {
Method method = methodDescriptor.getMethod();
if (isCandidateWriteMethod(method)) {
matches.add(method);
}
}
// Sort non-void returning write methods to guard against the ill effects of
// non-deterministic sorting of methods returned from Class#getDeclaredMethods
// under JDK 7. See http://bugs.sun.com/view_bug.do?bug_id=7023180
Collections.sort(matches, new Comparator<Method>() {
@Override
public int compare(Method m1, Method m2) {
return m2.toString().compareTo(m1.toString());
}
});
return matches;
}
private static Method findMethod(Class<?> targetType, String methodName,
Class<?>[] argTypes) throws IntrospectionException {
BeanInfo beanInfo = Introspector.getBeanInfo(targetType);
if (beanInfo != null) {
for(MethodDescriptor methodDesc: beanInfo.getMethodDescriptors()) {
Method method = methodDesc.getMethod();
Class<?>[] parameterTypes = method.getParameterTypes();
if (methodName.equals(method.getName()) && argTypes.length == parameterTypes.length) {
boolean matchedArgTypes = true;
for (int i = 0; i < argTypes.length; i++) {
if (getWrapperIfPrimitive(argTypes[i]) != parameterTypes[i]) {
matchedArgTypes = false;
break;
}
}
if (matchedArgTypes) {
return method;
}
}
}
}
return null;
}
public void testGetBeanInfoClassint_IGNORE_ALL_Method()
throws IntrospectionException {
BeanInfo info = Introspector.getBeanInfo(MockFooSub.class,
Introspector.IGNORE_ALL_BEANINFO);
MethodDescriptor[] mds = info.getMethodDescriptors();
int getMethod = 0;
int setMethod = 0;
for (MethodDescriptor element : mds) {
String name = element.getName();
if (name.startsWith("getText")) {
getMethod++;
assertEquals("getText", name);
}
if (name.startsWith("setText")) {
setMethod++;
assertEquals("setText", name);
}
}
assertEquals(1, getMethod);
assertEquals(1, setMethod);
}
public void testSetUnicast_false() throws SecurityException,
NoSuchMethodException, IntrospectionException {
String eventSetName = "MockPropertyChange";
Class<?> listenerType = MockPropertyChangeListener.class;
Method[] listenerMethods = {
listenerType.getMethod("mockPropertyChange",
MockPropertyChangeEvent.class),
listenerType.getMethod("mockPropertyChange2",
MockPropertyChangeEvent.class) };
MethodDescriptor[] listenerMethodDescriptors = {
new MethodDescriptor(listenerMethods[0]),
new MethodDescriptor(listenerMethods[1]), };
Class<MockSourceClass> sourceClass = MockSourceClass.class;
Method addMethod = sourceClass.getMethod(
"addMockPropertyChangeListener",listenerType);
Method removeMethod = sourceClass.getMethod(
"removeMockPropertyChangeListener", listenerType);
EventSetDescriptor esd = new EventSetDescriptor(eventSetName,
listenerType, listenerMethodDescriptors, addMethod,
removeMethod);
assertFalse(esd.isUnicast());
esd.setInDefaultEventSet(false);
assertFalse(esd.isInDefaultEventSet());
}
public void test_EventSetDescriptor_Constructor() throws Exception {
EventSetDescriptor eventSetDescriptor = new EventSetDescriptor(
(String) null, (Class<?>) null, new MethodDescriptor[] { null,
null }, (Method) null, (Method) null);
assertNull(eventSetDescriptor.getName());
assertNull(eventSetDescriptor.getListenerType());
assertNull(eventSetDescriptor.getAddListenerMethod());
assertNull(eventSetDescriptor.getRemoveListenerMethod());
try {
eventSetDescriptor.getListenerMethods();
fail("should throw NullPointerException");
} catch (NullPointerException e) {
// Expected
}
}
private List<Method> findCandidateWriteMethods(MethodDescriptor[] methodDescriptors) {
List<Method> matches = new ArrayList<>();
for (MethodDescriptor methodDescriptor : methodDescriptors) {
Method method = methodDescriptor.getMethod();
if (isCandidateWriteMethod(method)) {
matches.add(method);
}
}
// Sort non-void returning write methods to guard against the ill effects of
// non-deterministic sorting of methods returned from Class#getDeclaredMethods
// under JDK 7. See https://bugs.java.com/view_bug.do?bug_id=7023180
matches.sort((m1, m2) -> m2.toString().compareTo(m1.toString()));
return matches;
}
public static void main(final String[] args) throws Exception {
Class cl = getStub();
System.out.println("cl.getClassLoader() = " + cl.getClassLoader());
final BeanInfo beanInfo = Introspector.getBeanInfo(cl, Object.class);
MethodDescriptor[] mds = beanInfo.getMethodDescriptors();
System.out.println("mds = " + Arrays.toString(mds));
loader.close();
loader=null;
cl=null;
Util.generateOOME();
mds = beanInfo.getMethodDescriptors();
System.out.println("mds = " + Arrays.toString(mds));
}
private static void testEventSetDescriptor() {
try {
MethodDescriptor[] array = { new MethodDescriptor(MyDPD.class.getMethod("getArray")) };
EventSetDescriptor descriptor = new EventSetDescriptor(null, null, array, null, null);
test(descriptor.getListenerMethodDescriptors());
array[0] = null;
test(descriptor.getListenerMethodDescriptors());
descriptor.getListenerMethodDescriptors()[0] = null;
test(descriptor.getListenerMethodDescriptors());
}
catch (Exception exception) {
throw new Error("unexpected error", exception);
}
}
private static void testEventSetDescriptor() {
try {
MethodDescriptor[] array = { new MethodDescriptor(MyDPD.class.getMethod("getArray")) };
EventSetDescriptor descriptor = new EventSetDescriptor(null, null, array, null, null);
test(descriptor.getListenerMethodDescriptors());
array[0] = null;
test(descriptor.getListenerMethodDescriptors());
descriptor.getListenerMethodDescriptors()[0] = null;
test(descriptor.getListenerMethodDescriptors());
}
catch (Exception exception) {
throw new Error("unexpected error", exception);
}
}
private static void testMethodDescriptor() {
try {
ParameterDescriptor[] array = { new ParameterDescriptor() };
MethodDescriptor descriptor = new MethodDescriptor(MyDPD.class.getMethod("getArray"), array);
test(descriptor.getParameterDescriptors());
array[0] = null;
test(descriptor.getParameterDescriptors());
descriptor.getParameterDescriptors()[0] = null;
test(descriptor.getParameterDescriptors());
}
catch (Exception exception) {
throw new Error("unexpected error", exception);
}
}
private static void testEventSetDescriptor() {
try {
MethodDescriptor[] array = { new MethodDescriptor(MyDPD.class.getMethod("getArray")) };
EventSetDescriptor descriptor = new EventSetDescriptor(null, null, array, null, null);
test(descriptor.getListenerMethodDescriptors());
array[0] = null;
test(descriptor.getListenerMethodDescriptors());
descriptor.getListenerMethodDescriptors()[0] = null;
test(descriptor.getListenerMethodDescriptors());
}
catch (Exception exception) {
throw new Error("unexpected error", exception);
}
}
public static void main(String[] args) throws IntrospectionException {
Class type = BASE64Encoder.class;
System.setSecurityManager(new SecurityManager());
BeanInfo info = Introspector.getBeanInfo(type);
for (MethodDescriptor md : info.getMethodDescriptors()) {
Method method = md.getMethod();
System.out.println(method);
String name = method.getDeclaringClass().getName();
if (name.startsWith("sun.misc.")) {
throw new Error("found inaccessible method");
}
}
}
private static void testMethodDescriptor() {
try {
ParameterDescriptor[] array = { new ParameterDescriptor() };
MethodDescriptor descriptor = new MethodDescriptor(MyDPD.class.getMethod("getArray"), array);
test(descriptor.getParameterDescriptors());
array[0] = null;
test(descriptor.getParameterDescriptors());
descriptor.getParameterDescriptors()[0] = null;
test(descriptor.getParameterDescriptors());
}
catch (Exception exception) {
throw new Error("unexpected error", exception);
}
}
private static void testMethodDescriptor() {
try {
ParameterDescriptor[] array = { new ParameterDescriptor() };
MethodDescriptor descriptor = new MethodDescriptor(MyDPD.class.getMethod("getArray"), array);
test(descriptor.getParameterDescriptors());
array[0] = null;
test(descriptor.getParameterDescriptors());
descriptor.getParameterDescriptors()[0] = null;
test(descriptor.getParameterDescriptors());
}
catch (Exception exception) {
throw new Error("unexpected error", exception);
}
}
/**
* Gets the methodDescriptors attribute ...
*
* @return The methodDescriptors value
*/
public MethodDescriptor[] getMethodDescriptors() {
Vector descriptors = new Vector();
MethodDescriptor descriptor = null;
Method[] m;
Method method;
try {
m = Class.forName("com.l2fprod.common.swing.JDirectoryChooser").getMethods();
} catch (ClassNotFoundException e) {
return new MethodDescriptor[0];
}
return (MethodDescriptor[]) descriptors.toArray(new MethodDescriptor[descriptors.size()]);
}
private List<Method> findCandidateWriteMethods(MethodDescriptor[] methodDescriptors) {
List<Method> matches = new ArrayList<>();
for (MethodDescriptor methodDescriptor : methodDescriptors) {
Method method = methodDescriptor.getMethod();
if (isCandidateWriteMethod(method)) {
matches.add(method);
}
}
// Sort non-void returning write methods to guard against the ill effects of
// non-deterministic sorting of methods returned from Class#getDeclaredMethods
// under JDK 7. See http://bugs.sun.com/view_bug.do?bug_id=7023180
matches.sort((m1, m2) -> m2.toString().compareTo(m1.toString()));
return matches;
}
/**
* Gets the methodDescriptors attribute ...
*
* @return The methodDescriptors value
*/
public MethodDescriptor[] getMethodDescriptors() {
Vector descriptors = new Vector();
MethodDescriptor descriptor = null;
Method[] m;
Method method;
try {
m = Class.forName("com.l2fprod.common.swing.JFontChooser").getMethods();
} catch (ClassNotFoundException e) {
return new MethodDescriptor[0];
}
return (MethodDescriptor[]) descriptors.toArray(new MethodDescriptor[descriptors.size()]);
}
/**
* Gets the methodDescriptors attribute ...
*
* @return The methodDescriptors value
*/
public MethodDescriptor[] getMethodDescriptors() {
Vector descriptors = new Vector();
MethodDescriptor descriptor = null;
Method[] m;
Method method;
try {
m = Class.forName("com.l2fprod.common.swing.JCollapsiblePane").getMethods();
} catch (ClassNotFoundException e) {
return new MethodDescriptor[0];
}
return (MethodDescriptor[]) descriptors.toArray(new MethodDescriptor[descriptors.size()]);
}
private static void test(Class<?> type, Class<?> bean) throws Exception {
for (MethodDescriptor md : Introspector.getBeanInfo(bean).getMethodDescriptors()) {
if (md.getName().equals("getFoo")) {
if (type != md.getMethod().getReturnType()) {
throw new Error("unexpected type");
}
}
}
}
public static void main(String[] args) throws IntrospectionException {
Class type = BASE64Encoder.class;
System.setSecurityManager(new SecurityManager());
BeanInfo info = Introspector.getBeanInfo(type);
for (MethodDescriptor md : info.getMethodDescriptors()) {
Method method = md.getMethod();
System.out.println(method);
String name = method.getDeclaringClass().getName();
if (name.startsWith("sun.misc.")) {
throw new Error("found inaccessible method");
}
}
}
private static void testEventSetDescriptor() {
try {
MethodDescriptor[] array = { new MethodDescriptor(MyDPD.class.getMethod("getArray")) };
EventSetDescriptor descriptor = new EventSetDescriptor(null, null, array, null, null);
test(descriptor.getListenerMethodDescriptors());
array[0] = null;
test(descriptor.getListenerMethodDescriptors());
descriptor.getListenerMethodDescriptors()[0] = null;
test(descriptor.getListenerMethodDescriptors());
}
catch (Exception exception) {
throw new Error("unexpected error", exception);
}
}
private static void testMethodDescriptor() {
try {
ParameterDescriptor[] array = { new ParameterDescriptor() };
MethodDescriptor descriptor = new MethodDescriptor(MyDPD.class.getMethod("getArray"), array);
test(descriptor.getParameterDescriptors());
array[0] = null;
test(descriptor.getParameterDescriptors());
descriptor.getParameterDescriptors()[0] = null;
test(descriptor.getParameterDescriptors());
}
catch (Exception exception) {
throw new Error("unexpected error", exception);
}
}
private static void testEventSetDescriptor() {
try {
MethodDescriptor[] array = { new MethodDescriptor(MyDPD.class.getMethod("getArray")) };
EventSetDescriptor descriptor = new EventSetDescriptor(null, null, array, null, null);
test(descriptor.getListenerMethodDescriptors());
array[0] = null;
test(descriptor.getListenerMethodDescriptors());
descriptor.getListenerMethodDescriptors()[0] = null;
test(descriptor.getListenerMethodDescriptors());
}
catch (Exception exception) {
throw new Error("unexpected error", exception);
}
}