Path: blob/master/test/jdk/java/beans/Performance/Test4058433.java
41149 views
/*1* Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.Image;24import java.beans.BeanDescriptor;25import java.beans.BeanInfo;26import java.beans.EventSetDescriptor;27import java.beans.FeatureDescriptor;28import java.beans.IndexedPropertyDescriptor;29import java.beans.Introspector;30import java.beans.MethodDescriptor;31import java.beans.ParameterDescriptor;32import java.beans.PropertyDescriptor;33import java.lang.reflect.Array;34import java.lang.reflect.Method;35import java.net.URI;36import java.nio.file.FileSystem;37import java.nio.file.FileSystems;38import java.nio.file.FileVisitResult;39import java.nio.file.Files;40import java.nio.file.Path;41import java.nio.file.SimpleFileVisitor;42import java.nio.file.attribute.BasicFileAttributes;43import java.util.Arrays;44import java.util.Comparator;45import java.util.Enumeration;46import java.util.Map.Entry;47import java.util.Objects;48import java.util.TreeMap;49import java.util.TreeSet;5051/*52* @test53* @bug 405843354* @summary Generates BeanInfo for public classes in AWT, Accessibility, and Swing55* @author Sergey Malenkov56*/57public class Test4058433 implements Comparator<Object> {58@Override59public int compare(Object one, Object two) {60if (one instanceof Method && two instanceof Method) {61Method oneMethod = (Method) one;62Method twoMethod = (Method) two;63int result = oneMethod.getName().compareTo(twoMethod.getName());64if (result != 0) {65return result;66}67}68if (one instanceof FeatureDescriptor && two instanceof FeatureDescriptor) {69FeatureDescriptor oneFD = (FeatureDescriptor) one;70FeatureDescriptor twoFD = (FeatureDescriptor) two;71int result = oneFD.getName().compareTo(twoFD.getName());72if (result != 0) {73return result;74}75}76return one.toString().compareTo(two.toString());77}7879public static void main(String[] args) throws Exception {80FileSystem fs = FileSystems.getFileSystem(URI.create("jrt:/"));81fs.getFileStores();8283TreeSet<Class<?>> types = new TreeSet<>(new Test4058433());84Files.walkFileTree(fs.getPath("/modules/java.desktop"), new SimpleFileVisitor<Path>() {85@Override86public FileVisitResult visitFile(Path file,87BasicFileAttributes attrs) {88file = file.subpath(2, file.getNameCount());89if (file.startsWith("java/awt/")90|| file.startsWith("javax/accessibility/")91|| file.startsWith("javax/swing/")) {92String name =file.toString();93if (name.endsWith(".class")) {94name = name.substring(0, name.indexOf(".")).replace('/', '.');9596final Class<?> type;97try {98type = Class.forName(name);99} catch (ClassNotFoundException e) {100throw new RuntimeException(e);101}102if (!BeanInfo.class.isAssignableFrom(type) && !type.isInterface()103&& !type.isEnum() && !type.isAnnotation()104&& !type.isAnonymousClass()) {105if (null == type.getDeclaringClass()) {106types.add(type);107}108}109}110}111return FileVisitResult.CONTINUE;112}113});114115System.out.println("found " + types.size() + " classes");116long time = -System.currentTimeMillis();117for (Class<?> type : types) {118System.out.println("========================================");119BeanInfo info = Introspector.getBeanInfo(type);120121BeanDescriptor bd = info.getBeanDescriptor();122System.out.println(bd.getBeanClass());123print("customizer", bd.getCustomizerClass());124print(bd);125print("mono 16x16", info.getIcon(BeanInfo.ICON_MONO_16x16));126print("mono 32x32", info.getIcon(BeanInfo.ICON_MONO_32x32));127print("color 16x16", info.getIcon(BeanInfo.ICON_COLOR_16x16));128print("color 32x32", info.getIcon(BeanInfo.ICON_COLOR_32x32));129130PropertyDescriptor[] pds = info.getPropertyDescriptors();131PropertyDescriptor dpd = getDefault(pds, info.getDefaultPropertyIndex());132System.out.println(pds.length + " property descriptors");133Arrays.sort(pds, new Test4058433());134for (PropertyDescriptor pd : pds) {135print(pd);136if (dpd == pd) {137System.out.println("default property");138}139print("bound", pd.isBound());140print("constrained", pd.isConstrained());141print("property editor", pd.getPropertyEditorClass());142print("property type", pd.getPropertyType());143print("read method", pd.getReadMethod());144print("write method", pd.getWriteMethod());145if (pd instanceof IndexedPropertyDescriptor) {146IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;147print("indexed property type", ipd.getIndexedPropertyType());148print("indexed read method", ipd.getIndexedReadMethod());149print("indexed write method", ipd.getIndexedWriteMethod());150}151}152EventSetDescriptor[] esds = info.getEventSetDescriptors();153EventSetDescriptor desd = getDefault(esds, info.getDefaultEventIndex());154System.out.println(esds.length + " event set descriptors");155Arrays.sort(esds, new Test4058433());156for (EventSetDescriptor esd : esds) {157print(esd);158if (desd == esd) {159System.out.println("default event set");160}161print("in default", esd.isInDefaultEventSet());162print("unicast", esd.isUnicast());163print("listener type", esd.getListenerType());164print("get listener method", esd.getGetListenerMethod());165print("add listener method", esd.getAddListenerMethod());166print("remove listener method", esd.getRemoveListenerMethod());167Method[] methods = esd.getListenerMethods();168Arrays.sort(methods, new Test4058433());169for (Method method : methods) {170print("listener method", method);171}172print(esd.getListenerMethodDescriptors());173}174print(info.getMethodDescriptors());175}176time += System.currentTimeMillis();177System.out.println("DONE IN " + time + " MS");178}179180private static <T> T getDefault(T[] array, int index) {181return (index == -1) ? null : array[index];182}183184private static void print(MethodDescriptor[] mds) {185System.out.println(mds.length + " method descriptors");186Arrays.sort(mds, new Test4058433());187for (MethodDescriptor md : mds) {188print(md);189print("method", md.getMethod());190ParameterDescriptor[] pds = md.getParameterDescriptors();191if (pds != null) {192System.out.println(pds.length + " parameter descriptors");193for (ParameterDescriptor pd : pds) {194print(pd);195}196}197}198}199200private static void print(FeatureDescriptor descriptor) {201String name = descriptor.getName();202String display = descriptor.getDisplayName();203String description = descriptor.getShortDescription();204System.out.println("name: " + name);205if (!Objects.equals(name, display)) {206System.out.println("display name: " + display);207}208if (!Objects.equals(display, description)) {209System.out.println("description: " + description.trim());210}211print("expert", descriptor.isExpert());212print("hidden", descriptor.isHidden());213print("preferred", descriptor.isPreferred());214TreeMap<String,Object> map = new TreeMap<>();215Enumeration<String> enumeration = descriptor.attributeNames();216while (enumeration.hasMoreElements()) {217String id = enumeration.nextElement();218Object value = descriptor.getValue(id);219if (value.getClass().isArray()) {220TreeSet<String> set = new TreeSet<>();221int index = 0;222int length = Array.getLength(value);223while (index < length) {224set.add(Array.get(value, index++) + ", " +225Array.get(value, index++) + ", " +226Array.get(value, index++));227}228value = set.toString();229}230map.put(id, value);231}232for (Entry<String,Object> entry : map.entrySet()) {233System.out.println(entry.getKey() + ": " + entry.getValue());234}235}236237private static void print(String id, boolean flag) {238if (flag) {239System.out.println(id + " is set");240}241}242243private static void print(String id, Class<?> type) {244if (type != null) {245System.out.println(id + ": " + type.getName());246}247}248249private static void print(String id, Method method) {250if (method != null) {251System.out.println(id + ": " + method);252}253}254255private static void print(String name, Image image) {256if (image != null) {257System.out.println(name + " icon is exist");258}259}260}261262263