1 /* 2 * Copyright (c) 2014, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package com.sun.tools.sjavac.pubapi; 26 27 import java.io.Serializable; 28 import java.util.Locale; 29 30 import javax.lang.model.type.ArrayType; 31 import javax.lang.model.type.DeclaredType; 32 import javax.lang.model.type.ErrorType; 33 import javax.lang.model.type.NoType; 34 import javax.lang.model.type.PrimitiveType; 35 import javax.lang.model.type.TypeKind; 36 import javax.lang.model.type.TypeMirror; 37 import javax.lang.model.type.TypeVariable; 38 import javax.lang.model.type.TypeVisitor; 39 import javax.lang.model.util.SimpleTypeVisitor9; 40 41 import com.sun.tools.javac.util.DefinedBy; 42 import com.sun.tools.javac.util.DefinedBy.Api; 43 44 public abstract class TypeDesc implements Serializable { 45 46 private static final long serialVersionUID = -8201634143915519172L; 47 48 TypeKind typeKind; 49 50 public TypeDesc(TypeKind typeKind) { 51 this.typeKind = typeKind; 52 } 53 54 public static TypeDesc decodeString(String s) { 55 s = s.trim(); 56 if (s.endsWith("[]")) { 57 String componentPart = s.substring(0, s.length()-2); 58 return new ArrayTypeDesc(decodeString(componentPart)); 59 } 60 61 if (s.startsWith("#")) 62 return new TypeVarTypeDesc(s.substring(1)); 63 64 if (s.matches("boolean|byte|char|double|float|int|long|short|void")) 65 return new PrimitiveTypeDesc(TypeKind.valueOf(s.toUpperCase(Locale.US))); 66 67 return new ReferenceTypeDesc(s); 68 } 69 70 public static String encodeAsString(TypeDesc td) { 71 if (td.typeKind.isPrimitive() || td.typeKind == TypeKind.VOID) 72 return td.typeKind.toString().toLowerCase(Locale.US); 73 74 if (td.typeKind == TypeKind.ARRAY) 75 return encodeAsString(((ArrayTypeDesc) td).compTypeDesc) + "[]"; 76 77 if (td.typeKind == TypeKind.TYPEVAR) 78 return "#" + ((TypeVarTypeDesc) td).identifier; 79 80 if (td.typeKind == TypeKind.DECLARED) 81 return ((ReferenceTypeDesc) td).javaType.toString(); 82 83 throw new AssertionError("Unhandled type: " + td.typeKind); 84 } 85 86 public static TypeDesc fromType(TypeMirror type) { 87 TypeVisitor<TypeDesc, Void> v = new SimpleTypeVisitor9<TypeDesc, Void>() { 88 @Override @DefinedBy(Api.LANGUAGE_MODEL) 89 public TypeDesc visitArray(ArrayType t, Void p) { 90 return new ArrayTypeDesc(t.getComponentType().accept(this, p)); 91 } 92 93 @Override @DefinedBy(Api.LANGUAGE_MODEL) 94 public TypeDesc visitDeclared(DeclaredType t, Void p) { 95 return new ReferenceTypeDesc(t.toString()); 96 } 97 98 @Override @DefinedBy(Api.LANGUAGE_MODEL) 99 public TypeDesc visitNoType(NoType t, Void p) { 100 return new PrimitiveTypeDesc(TypeKind.VOID); 101 } 102 103 @Override @DefinedBy(Api.LANGUAGE_MODEL) 104 public TypeDesc visitTypeVariable(TypeVariable t, Void p) { 105 return new TypeVarTypeDesc(t.toString()); 106 } 107 108 @Override @DefinedBy(Api.LANGUAGE_MODEL) 109 public TypeDesc visitPrimitive(PrimitiveType t, Void p) { 110 return new PrimitiveTypeDesc(t.getKind()); 111 } 112 113 @Override @DefinedBy(Api.LANGUAGE_MODEL) 114 public TypeDesc visitError(ErrorType t, Void p) { 115 return new ReferenceTypeDesc("<error type>"); 116 } 117 }; 118 119 TypeDesc td = v.visit(type); 120 if (td == null) 121 throw new AssertionError("Unhandled type mirror: " + type + " (" + type.getClass() + ")"); 122 return td; 123 } 124 125 @Override 126 public boolean equals(Object obj) { 127 if (getClass() != obj.getClass()) 128 return false; 129 return typeKind.equals(((TypeDesc) obj).typeKind); 130 } 131 132 @Override 133 public int hashCode() { 134 return typeKind.hashCode(); 135 } 136 }