1/* 2 * Copyright (c) 2014, 2015, 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 26package com.sun.tools.sjavac.pubapi; 27 28import java.io.Serializable; 29 30import javax.lang.model.type.ArrayType; 31import javax.lang.model.type.DeclaredType; 32import javax.lang.model.type.ErrorType; 33import javax.lang.model.type.NoType; 34import javax.lang.model.type.PrimitiveType; 35import javax.lang.model.type.TypeKind; 36import javax.lang.model.type.TypeMirror; 37import javax.lang.model.type.TypeVariable; 38import javax.lang.model.type.TypeVisitor; 39import javax.lang.model.util.SimpleTypeVisitor9; 40 41import com.sun.tools.javac.code.Type.ClassType; 42import com.sun.tools.javac.util.DefinedBy; 43import com.sun.tools.javac.util.DefinedBy.Api; 44import com.sun.tools.javac.util.StringUtils; 45 46public abstract class TypeDesc implements Serializable { 47 48 private static final long serialVersionUID = -8201634143915519172L; 49 50 TypeKind typeKind; 51 52 public TypeDesc(TypeKind typeKind) { 53 this.typeKind = typeKind; 54 } 55 56 public static TypeDesc decodeString(String s) { 57 s = s.trim(); 58 if (s.endsWith("[]")) { 59 String componentPart = s.substring(0, s.length()-2); 60 return new ArrayTypeDesc(decodeString(componentPart)); 61 } 62 63 if (s.startsWith("#")) 64 return new TypeVarTypeDesc(s.substring(1)); 65 66 if (s.matches("boolean|byte|char|double|float|int|long|short|void")) { 67 TypeKind tk = TypeKind.valueOf(StringUtils.toUpperCase(s)); 68 return new PrimitiveTypeDesc(tk); 69 } 70 71 return new ReferenceTypeDesc(s); 72 } 73 74 public static String encodeAsString(TypeDesc td) { 75 if (td.typeKind.isPrimitive() || td.typeKind == TypeKind.VOID) 76 return StringUtils.toLowerCase(td.typeKind.toString()); 77 78 if (td.typeKind == TypeKind.ARRAY) 79 return encodeAsString(((ArrayTypeDesc) td).compTypeDesc) + "[]"; 80 81 if (td.typeKind == TypeKind.TYPEVAR) 82 return "#" + ((TypeVarTypeDesc) td).identifier; 83 84 if (td.typeKind == TypeKind.DECLARED) 85 return ((ReferenceTypeDesc) td).javaType.toString(); 86 87 throw new AssertionError("Unhandled type: " + td.typeKind); 88 } 89 90 public static TypeDesc fromType(TypeMirror type) { 91 TypeVisitor<TypeDesc, Void> v = new SimpleTypeVisitor9<TypeDesc, Void>() { 92 @Override @DefinedBy(Api.LANGUAGE_MODEL) 93 public TypeDesc visitArray(ArrayType t, Void p) { 94 return new ArrayTypeDesc(t.getComponentType().accept(this, p)); 95 } 96 97 @Override @DefinedBy(Api.LANGUAGE_MODEL) 98 public TypeDesc visitDeclared(DeclaredType t, Void p) { 99 return new ReferenceTypeDesc(((ClassType) t).tsym.flatName().toString()); 100 } 101 102 @Override @DefinedBy(Api.LANGUAGE_MODEL) 103 public TypeDesc visitNoType(NoType t, Void p) { 104 return new PrimitiveTypeDesc(TypeKind.VOID); 105 } 106 107 @Override @DefinedBy(Api.LANGUAGE_MODEL) 108 public TypeDesc visitTypeVariable(TypeVariable t, Void p) { 109 return new TypeVarTypeDesc(t.toString()); 110 } 111 112 @Override @DefinedBy(Api.LANGUAGE_MODEL) 113 public TypeDesc visitPrimitive(PrimitiveType t, Void p) { 114 return new PrimitiveTypeDesc(t.getKind()); 115 } 116 117 @Override @DefinedBy(Api.LANGUAGE_MODEL) 118 public TypeDesc visitError(ErrorType t, Void p) { 119 return new ReferenceTypeDesc("<error type>"); 120 } 121 }; 122 123 TypeDesc td = v.visit(type); 124 if (td == null) 125 throw new AssertionError("Unhandled type mirror: " + type + " (" + type.getClass() + ")"); 126 return td; 127 } 128 129 @Override 130 public boolean equals(Object obj) { 131 if (getClass() != obj.getClass()) 132 return false; 133 return typeKind.equals(((TypeDesc) obj).typeKind); 134 } 135 136 @Override 137 public int hashCode() { 138 return typeKind.hashCode(); 139 } 140} 141