test/langtools/jdk/javadoc/doclet/testJavaFX/propgen/PropGen.java
changeset 49879 601277b1d582
equal deleted inserted replaced
49878:2422d4e027b0 49879:601277b1d582
       
     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 package propgen;
       
    25 
       
    26 import java.io.ByteArrayOutputStream;
       
    27 import java.io.IOException;
       
    28 import java.io.PrintStream;
       
    29 import java.nio.file.Files;
       
    30 import java.nio.file.Path;
       
    31 import java.nio.file.Paths;
       
    32 import java.util.EnumSet;
       
    33 import java.util.Set;
       
    34 import java.util.stream.Collectors;
       
    35 
       
    36 public class PropGen {
       
    37 
       
    38     /**
       
    39      * @param args the command line arguments
       
    40      */
       
    41     public static void main(String[] args) throws IOException {
       
    42         new PropGen().run();
       
    43     }
       
    44 
       
    45     final PrintStream out;
       
    46 
       
    47     final Path outFile;
       
    48     final ByteArrayOutputStream baos;
       
    49 
       
    50     PropGen() {
       
    51         out = System.out;
       
    52         outFile = null;
       
    53         baos = null;
       
    54     }
       
    55 
       
    56     public PropGen(Path outDir) throws IOException {
       
    57         outFile = Paths.get(outDir.toString(), "Demo.java");
       
    58         baos = new ByteArrayOutputStream();
       
    59         out = new PrintStream(baos);
       
    60     }
       
    61 
       
    62     enum Kind {
       
    63         FIELD(1),
       
    64         GETTER(2),
       
    65         SETTER(4),
       
    66         PROPERTY(8);
       
    67         Kind(int bit) {
       
    68             this.bit = bit;
       
    69         }
       
    70         int bit;
       
    71     }
       
    72 
       
    73     public void run() throws IOException {
       
    74         out.println("import javafx.beans.property.Property;");
       
    75         out.println("public class Demo {");
       
    76         for (int i = 1; i < 16; i++) {
       
    77             Set<Kind> set = EnumSet.noneOf(Kind.class);
       
    78             for (Kind k : Kind.values()) {
       
    79                 if ((i & k.bit) == k.bit) {
       
    80                     set.add(k);
       
    81                 }
       
    82             }
       
    83             addItems(set);
       
    84         }
       
    85         out.println("}");
       
    86         if (baos != null && outFile != null) {
       
    87             Files.write(outFile, baos.toByteArray());
       
    88         }
       
    89     }
       
    90 
       
    91     void addItems(Set<Kind> kinds) {
       
    92         String name = kinds.stream()
       
    93                 .map(k -> k.name())
       
    94                 .map(s -> s.substring(0, 1))
       
    95                 .collect(Collectors.joining(""))
       
    96                 .toLowerCase();
       
    97         if (kinds.contains(Kind.FIELD)) {
       
    98             out.println("    /** Field for " + name + ". */");
       
    99             out.println("    public Property " + name + ";");
       
   100         }
       
   101         if (kinds.contains(Kind.GETTER)) {
       
   102             out.println("    /**");
       
   103             out.println("     * Getter for " + name + ".");
       
   104             out.println("     * @return a " + name);
       
   105             out.println("     */");
       
   106             out.println("    public Object " + mname("get", name) + "() { return null; }");
       
   107         }
       
   108         if (kinds.contains(Kind.SETTER)) {
       
   109             out.println("    /**");
       
   110             out.println("     * Setter for " + name + ".");
       
   111             out.println("     * @param o a " + name);
       
   112             out.println("     */");
       
   113             out.println("    public void " + mname("set", name) + "(Object o) {  }");
       
   114         }
       
   115         if (kinds.contains(Kind.PROPERTY)) {
       
   116             out.println("    /**");
       
   117             out.println("     * Property for " + name + ".");
       
   118             out.println("     * @return the property for " + name);
       
   119             out.println("     */");
       
   120             out.println("    public Property " + name + "Property() { return null; }");
       
   121         }
       
   122         out.println();
       
   123     }
       
   124 
       
   125     String mname(String prefix, String base) {
       
   126         return prefix + Character.toUpperCase(base.charAt(0)) + base.substring(1);
       
   127     }
       
   128 
       
   129 }