• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 Google Inc. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.turbine.binder.sym;
18 
19 import com.google.errorprone.annotations.Immutable;
20 
21 /**
22  * A class symbol.
23  *
24  * <p>Turbine identifies classes by their binary string name. Symbols are immutable and do not hold
25  * any semantic information: the information that has been determined at the current phase (e.g.
26  * about super-types and members) is held externally.
27  */
28 // TODO(cushon): investigate performance impact of interning names/symbols
29 @Immutable
30 public class ClassSymbol implements Symbol {
31 
32   public static final ClassSymbol OBJECT = new ClassSymbol("java/lang/Object");
33   public static final ClassSymbol STRING = new ClassSymbol("java/lang/String");
34   public static final ClassSymbol ENUM = new ClassSymbol("java/lang/Enum");
35   public static final ClassSymbol ANNOTATION = new ClassSymbol("java/lang/annotation/Annotation");
36   public static final ClassSymbol INHERITED = new ClassSymbol("java/lang/annotation/Inherited");
37   public static final ClassSymbol CLONEABLE = new ClassSymbol("java/lang/Cloneable");
38   public static final ClassSymbol SERIALIZABLE = new ClassSymbol("java/io/Serializable");
39   public static final ClassSymbol DEPRECATED = new ClassSymbol("java/lang/Deprecated");
40   public static final ClassSymbol PROFILE_ANNOTATION = new ClassSymbol("jdk/Profile+Annotation");
41   public static final ClassSymbol PROPRIETARY_ANNOTATION =
42       new ClassSymbol("sun/Proprietary+Annotation");
43   public static final ClassSymbol ERROR = new ClassSymbol("<error>");
44 
45   public static final ClassSymbol CHARACTER = new ClassSymbol("java/lang/Character");
46   public static final ClassSymbol SHORT = new ClassSymbol("java/lang/Short");
47   public static final ClassSymbol INTEGER = new ClassSymbol("java/lang/Integer");
48   public static final ClassSymbol LONG = new ClassSymbol("java/lang/Long");
49   public static final ClassSymbol FLOAT = new ClassSymbol("java/lang/Float");
50   public static final ClassSymbol DOUBLE = new ClassSymbol("java/lang/Double");
51   public static final ClassSymbol BOOLEAN = new ClassSymbol("java/lang/Boolean");
52   public static final ClassSymbol BYTE = new ClassSymbol("java/lang/Byte");
53 
54   private final String className;
55 
ClassSymbol(String className)56   public ClassSymbol(String className) {
57     this.className = className;
58   }
59 
60   @Override
hashCode()61   public int hashCode() {
62     return className.hashCode();
63   }
64 
65   @Override
toString()66   public String toString() {
67     return className.replace('/', '.');
68   }
69 
70   @Override
equals(Object o)71   public boolean equals(Object o) {
72     return o instanceof ClassSymbol && className.equals(((ClassSymbol) o).className);
73   }
74 
75   /** The JVMS 4.2.1 binary name of the class. */
binaryName()76   public String binaryName() {
77     return className;
78   }
79 
80   @Override
symKind()81   public Kind symKind() {
82     return Kind.CLASS;
83   }
84 
simpleName()85   public String simpleName() {
86     return binaryName().substring(binaryName().lastIndexOf('/') + 1);
87   }
88 
packageName()89   public String packageName() {
90     int idx = binaryName().lastIndexOf('/');
91     return idx != -1 ? binaryName().substring(0, idx) : "";
92   }
93 
owner()94   public PackageSymbol owner() {
95     return new PackageSymbol(packageName());
96   }
97 }
98