public enum JavaLanguage { JAVA("Forever"), SCALA("Next generation") {}, KOTLIN("Future") {}; private final String claim; JavaLanguage(String claim) { this.claim = claim; } public String getClaim() { return claim; } }
public enum LanguageUtils { ; /** java-doc */ LanguageUtils() { throw new IllegalStateException(" "); } /** java-doc */ public static String[] getKeyWords(String languageName) { if (languageName != null) { // return loadFromResource(languageName + "/keywords.dat"); } throw new IllegalStateException(" "); } /** java-doc */ private static synchronized String[] loadFromResource(String resourceName) { String[] items = null; // return items; } /** */ }
// JavaLanguage.java public static void main(String[] args) { // it's true if (JAVA.getClass() == SCALA.getClass().getSuperclass()) { System.out.println(" !"); } // it's true if (JAVA.getClass() == KOTLIN.getClass().getSuperclass()) { System.out.println(" !"); } }
Choose your |
Are you for Java?
|
Are you for Scala?
|
Are you for Kotlin?
|
Highlander.valueOf("JAVA");
This is the point to access the array to store a single element, to beat bash ...
Highlander.values()[0];
package com.enums; import java.io.*; public class EnumGeneratorShort implements Closeable { private BufferedWriter writer; public EnumGeneratorShort(File outputFile) throws IOException { writer = new BufferedWriter(new FileWriter(outputFile)); } void append(String line) throws IOException { writer.append(line); } void appendLine(String line) throws IOException { writer.append(line).append("\n"); } @Override public void close() throws IOException { if (writer != null) { writer.close(); } } /** enum*/ private static final int ENUM_COUNT = XXXX; // XXXX - private static final char[] ALPHABET = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; public static void main(String[] args) throws IOException { System.out.println("Start generation"); File outputFile = new File("src/main/java/com/enums/BigEnumShort.java"); try (EnumGeneratorShort enumGen = new EnumGeneratorShort(outputFile)) { enumGen.appendLine("package com.enums;"); enumGen.appendLine(""); enumGen.appendLine("public enum BigEnumShort {"); enumGen.append("A"); int index = 1; for (; index < ENUM_COUNT; index++) { enumGen.appendLine(","); String name = getName(index, ""); if ("if".equals(name) || "do".equals(name)) { name = getName(++index, ""); } enumGen.append(name); } enumGen.appendLine(";"); enumGen.appendLine(""); enumGen.appendLine(" public static void main(String[] args) {"); enumGen.appendLine(" System.out.println(\"Find enum \" + BigEnumShort.valueOf(\"B\"));"); enumGen.appendLine(" }"); enumGen.appendLine("}"); System.out.println("End generation. Total " + index); } } public static String getName(int index, String before) { if (index < ALPHABET.length) { return before + ALPHABET[index]; } int tail = index / ALPHABET.length; int current = index % ALPHABET.length; return getName(tail, before + ALPHABET[current]); } }
A place | Decompiler | Result |
one | fernflower.jar | OK |
2 | jad | Ok |
3 | procyon | Waited |
four | cfr_0_115.jar | Not wait |
Effective Java, 2nd Edition, by Joshua Bloch *
Wikipedia
Right singleton in java
Comment by apanasevich
Comment by Sirikid
Source: https://habr.com/ru/post/321152/