MessageType.java revision 4202:2bd34895dda2
1/* 2 * Copyright (c) 2014, 2016, 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 24package propertiesparser.parser; 25 26/** 27 * Common interface to all kinds of diagnostic argument types. 28 */ 29public interface MessageType { 30 31 /** 32 * Visitor method. 33 */ 34 <R, A> R accept(Visitor<R, A> v, A a); 35 36 /** 37 * The type as mentioned in the resource file. 38 */ 39 String kindName(); 40 41 /** 42 * A custom type is a type for which a predefined alternative does not exist. As such, it is an 43 * handy option when prototyping - but usages of custom types should be avoided in product-quality 44 * resource file comments. 45 * 46 * Example: 'com.sun.tools.javac.code.Flags.Flag' 47 */ 48 public static class CustomType implements MessageType { 49 50 /** The string-based representation of this type. */ 51 public String typeString; 52 53 public CustomType(String typeString) { 54 this.typeString = typeString; 55 } 56 57 @Override 58 public String kindName() { 59 return typeString; 60 } 61 62 @Override 63 public <R, A> R accept(Visitor<R, A> v, A a) { 64 return v.visitCustomType(this, a); 65 } 66 } 67 68 /** 69 * A predefined type. All common types mentioned in the resource file comments are meant to 70 * be included here. 71 */ 72 public enum SimpleType implements MessageType { 73 74 ANNOTATION("annotation", "Compound", "com.sun.tools.javac.code.Attribute"), 75 BOOLEAN("boolean", "boolean", null), 76 COLLECTION("collection", "Collection", "java.util"), 77 FLAG("flag", "Flag", "com.sun.tools.javac.code.Flags"), 78 FRAGMENT("fragment", "Fragment", null), 79 DIAGNOSTIC("diagnostic", "JCDiagnostic", "com.sun.tools.javac.util"), 80 MODIFIER("modifier", "Modifier", "javax.lang.model.element"), 81 FILE("file", "File", "java.io"), 82 FILE_OBJECT("file object", "JavaFileObject", "javax.tools"), 83 PATH("path", "Path", "java.nio.file"), 84 NAME("name", "Name", "com.sun.tools.javac.util"), 85 NUMBER("number", "int", null), 86 OPTION_NAME("option name", "Option", "com.sun.tools.javac.main"), 87 SOURCE_VERSION("source version", "SourceVersion", "javax.lang.model"), 88 STRING("string", "String", null), 89 SYMBOL("symbol", "Symbol", "com.sun.tools.javac.code"), 90 SYMBOL_KIND("symbol kind", "Kind", "com.sun.tools.javac.code.Kinds"), 91 KIND_NAME("kind name", "KindName", "com.sun.tools.javac.code.Kinds"), 92 TOKEN("token", "TokenKind", "com.sun.tools.javac.parser.Tokens"), 93 TYPE("type", "Type", "com.sun.tools.javac.code"), 94 SET("set", "Set", "java.util"), 95 LIST("list", "List", "java.util"), 96 OBJECT("object", "Object", null), 97 UNUSED("unused", "Void", null), 98 UNKNOWN("<unknown>", "UnknownType", null); 99 100 /** name of the predefined type as mentioned in the resource file. */ 101 public final String kindName; 102 103 /** string-based representation of the type */ 104 public final String clazz; 105 106 /** type qualifier (might be null) */ 107 public final String qualifier; 108 109 SimpleType(String kindName, String clazz, String qualifier) { 110 this.kindName = kindName; 111 this.clazz = clazz; 112 this.qualifier = qualifier; 113 } 114 115 @Override 116 public String kindName() { 117 return kindName; 118 } 119 120 @Override 121 public <R, A> R accept(Visitor<R, A> v, A a) { 122 return v.visitSimpleType(this, a); 123 } 124 } 125 126 /** 127 * A compound type is a collection of some element type. 128 * 129 * Example: list of string 130 */ 131 public static class CompoundType implements MessageType { 132 133 /** 134 * Compound type kind. 135 */ 136 public enum Kind { 137 COLLECTION("collection of", SimpleType.COLLECTION), 138 LIST("list of", SimpleType.LIST), 139 SET("set of", SimpleType.SET); 140 141 public final String kindName; 142 public final SimpleType clazz; 143 144 Kind(String kindName, SimpleType clazz) { 145 this.kindName = kindName; 146 this.clazz = clazz; 147 } 148 } 149 150 /** The compound type kind. */ 151 public final Kind kind; 152 153 /** The element type. */ 154 public final MessageType elemtype; 155 156 public CompoundType(Kind kind, MessageType elemtype) { 157 this.kind = kind; 158 this.elemtype = elemtype; 159 } 160 161 @Override 162 public String kindName() { 163 return kind.kindName; 164 } 165 166 @Override 167 public <R, A> R accept(Visitor<R, A> v, A a) { 168 return v.visitCompoundType(this, a); 169 } 170 } 171 172 /** 173 * A union type represents an alternative between two (or more) types. It can be useful to 174 * define the type of an argument which can assume multiple (unrelated) values; union types 175 * are only meant to be used in cases where the alternative comes up frequently enough in the 176 * resource file comments - in order to avoid cluttered comments. 177 * 178 * Example: message segment 179 */ 180 public static class UnionType implements MessageType { 181 182 /** 183 * Union type kind. 184 */ 185 public enum Kind { 186 MESSAGE_SEGMENT("message segment", SimpleType.DIAGNOSTIC, SimpleType.FRAGMENT), 187 FILE_NAME("file name", SimpleType.FILE, SimpleType.FILE_OBJECT, SimpleType.PATH); 188 189 final String kindName; 190 final SimpleType[] choices; 191 192 Kind(String kindName, SimpleType... choices) { 193 this.kindName = kindName; 194 this.choices = choices; 195 } 196 } 197 198 /** The union type kind. */ 199 public final Kind kind; 200 201 /** The union type alternatives. */ 202 public final MessageType[] choices; 203 204 UnionType(Kind kind) { 205 this(kind, kind.choices); 206 } 207 208 protected UnionType(Kind kind, MessageType[] choices) { 209 this.choices = choices; 210 this.kind = kind; 211 } 212 213 @Override 214 public String kindName() { 215 return kind.kindName; 216 } 217 218 @Override 219 public <R, A> R accept(Visitor<R, A> v, A a) { 220 return v.visitUnionType(this, a); 221 } 222 } 223 224 /** 225 * A subclass of union type representing 'explicit' alternatives in the resource file comments. 226 * Note: as the token 'or' is parsed with lowest priority, it is not possible, for instance, 227 * to form a compound type out of an 'or' type. In such cases a plain union type should be used 228 * instead. 229 * 230 * Examples: symbol or type 231 */ 232 public static class OrType extends UnionType { 233 234 public static final String OR_NAME = "or"; 235 236 @Override 237 public String kindName() { 238 return OR_NAME; 239 } 240 241 public OrType(MessageType... choices) { 242 super(null, choices); 243 } 244 } 245 246 /** 247 * Visitor class. 248 */ 249 public static abstract class Visitor<R, A> { 250 public abstract R visitCustomType(CustomType t, A a); 251 public abstract R visitSimpleType(SimpleType t, A a); 252 public abstract R visitCompoundType(CompoundType t, A a); 253 public abstract R visitUnionType(UnionType t, A a); 254 } 255} 256