1 /*
2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 import java.beans.BeanInfo;
25 import java.beans.IntrospectionException;
26 import java.beans.Introspector;
27 import java.io.IOException;
28 import java.net.URI;
29 import java.nio.file.FileSystem;
30 import java.nio.file.FileSystems;
31 import java.nio.file.FileVisitResult;
32 import java.nio.file.Files;
33 import java.nio.file.Path;
34 import java.nio.file.SimpleFileVisitor;
35 import java.nio.file.attribute.BasicFileAttributes;
36 
37 /**
38  * @test
39  * @bug 8205454
40  */
41 public final class TypoInBeanDescription {
42 
43     private static final String[] typos = {"&", "<", ">", """};
44 
main(final String[] args)45     public static void main(final String[] args) throws IOException {
46         FileSystem fs = FileSystems.getFileSystem(URI.create("jrt:/"));
47         Files.walkFileTree(fs.getPath("/modules/java.desktop"), new SimpleFileVisitor<>() {
48             @Override
49             public FileVisitResult visitFile(Path file,
50                                              BasicFileAttributes attrs) {
51                 file = file.subpath(2, file.getNameCount());
52                 String name = file.toString();
53                 if (name.endsWith(".class")) {
54                     name = name.substring(0, name.indexOf(".")).replace('/', '.');
55 
56                     final Class<?> type;
57                     try {
58                         type = Class.forName(name);
59                     } catch (Throwable e) {
60                         return FileVisitResult.CONTINUE;
61                     }
62                     final BeanInfo beanInfo;
63                     try {
64                         beanInfo = Introspector.getBeanInfo(type);
65                     } catch (IntrospectionException e) {
66                         return FileVisitResult.CONTINUE;
67                     }
68                     test(beanInfo);
69                 }
70                 return FileVisitResult.CONTINUE;
71             }
72         });
73     }
74 
test(final BeanInfo beanInfo)75     private static void test(final BeanInfo beanInfo) {
76         for (var pd : beanInfo.getPropertyDescriptors()) {
77             String d = pd.getShortDescription();
78             String n = pd.getName();
79             String dn = pd.getDisplayName();
80             for (String typo : typos) {
81                 if (d.contains(typo) || n.contains(typo) || dn.contains(typo)) {
82                     throw new RuntimeException("Wrong name: " + beanInfo.getBeanDescriptor());
83                 }
84             }
85         }
86     }
87 }
88