001    /*
002     * Copyright 2010-2013 JetBrains s.r.o.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.jetbrains.jet.lang.resolve.java;
018    
019    import org.jetbrains.annotations.Nullable;
020    import org.jetbrains.jet.lang.types.lang.PrimitiveType;
021    import org.jetbrains.asm4.Type;
022    
023    import java.util.HashMap;
024    import java.util.Map;
025    
026    public enum JvmPrimitiveType {
027        BOOLEAN(PrimitiveType.BOOLEAN, "boolean", "java.lang.Boolean", Type.BOOLEAN_TYPE),
028        CHAR(PrimitiveType.CHAR, "char", "java.lang.Character", Type.CHAR_TYPE),
029        BYTE(PrimitiveType.BYTE, "byte", "java.lang.Byte", Type.BYTE_TYPE),
030        SHORT(PrimitiveType.SHORT, "short", "java.lang.Short", Type.SHORT_TYPE),
031        INT(PrimitiveType.INT, "int", "java.lang.Integer", Type.INT_TYPE),
032        FLOAT(PrimitiveType.FLOAT, "float", "java.lang.Float", Type.FLOAT_TYPE),
033        LONG(PrimitiveType.LONG, "long", "java.lang.Long", Type.LONG_TYPE),
034        DOUBLE(PrimitiveType.DOUBLE, "double", "java.lang.Double", Type.DOUBLE_TYPE),
035        ;
036        
037        private final PrimitiveType primitiveType;
038        private final String name;
039        private final JvmClassName wrapper;
040        private final Type asmType;
041        private final char jvmLetter;
042        private final Type asmArrayType;
043        private final JvmClassName iterator;
044    
045        private JvmPrimitiveType(PrimitiveType primitiveType, String name, String wrapperClassName, Type asmType) {
046            this.primitiveType = primitiveType;
047            this.name = name;
048            this.wrapper = JvmClassName.byFqNameWithoutInnerClasses(wrapperClassName);
049            this.asmType = asmType;
050            this.jvmLetter = asmType.getDescriptor().charAt(0);
051            this.asmArrayType = makeArrayType(asmType);
052            this.iterator = JvmClassName.byFqNameWithoutInnerClasses("jet." + primitiveType.getTypeName() + "Iterator");
053        }
054        
055        private static Type makeArrayType(Type type) {
056            StringBuilder sb = new StringBuilder(2);
057            sb.append('[');
058            sb.append(type.getDescriptor());
059            return Type.getType(sb.toString());
060        }
061    
062        public PrimitiveType getPrimitiveType() {
063            return primitiveType;
064        }
065    
066        public String getName() {
067            return name;
068        }
069    
070        public JvmClassName getWrapper() {
071            return wrapper;
072        }
073    
074        public Type getAsmType() {
075            return asmType;
076        }
077    
078        public Type getAsmArrayType() {
079            return asmArrayType;
080        }
081    
082        public JvmClassName getIterator() {
083            return iterator;
084        }
085    
086        public char getJvmLetter() {
087            return jvmLetter;
088        }
089    
090    
091    
092        private static class MapByAsmTypeHolder {
093            private static final Map<Integer, JvmPrimitiveType> map;
094            
095            static {
096                map = new HashMap<Integer, JvmPrimitiveType>();
097                for (JvmPrimitiveType jvmPrimitiveType : values()) {
098                    map.put(jvmPrimitiveType.getAsmType().getSort(), jvmPrimitiveType);
099                }
100            }
101        }
102    
103        @Nullable
104        public static JvmPrimitiveType getByAsmType(Type type) {
105            return MapByAsmTypeHolder.map.get(type.getSort());
106        }
107        
108        
109        private static class MapByWrapperAsmTypeHolder {
110            private static final Map<Type, JvmPrimitiveType> map;
111    
112            static {
113                map = new HashMap<Type, JvmPrimitiveType>();
114                for (JvmPrimitiveType jvmPrimitiveType : values()) {
115                    map.put(jvmPrimitiveType.getWrapper().getAsmType(), jvmPrimitiveType);
116                }
117            }
118        }
119        
120        @Nullable
121        public static JvmPrimitiveType getByWrapperAsmType(Type type) {
122            return MapByWrapperAsmTypeHolder.map.get(type);
123        }
124    
125        @Nullable
126        public static JvmPrimitiveType getByWrapperClass(JvmClassName className) {
127            return getByWrapperAsmType(className.getAsmType());
128        }
129    }