001    /*
002     * Copyright 2010-2015 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.kotlin.resolve.constants;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
021    import org.jetbrains.kotlin.descriptors.ClassDescriptor;
022    import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor;
023    import org.jetbrains.kotlin.types.JetType;
024    
025    import static org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilPackage.getClassObjectType;
026    
027    public class EnumValue extends CompileTimeConstant<ClassDescriptor> {
028    
029        public EnumValue(@NotNull ClassDescriptor value, boolean usesVariableAsConstant) {
030            super(value, true, false, usesVariableAsConstant);
031        }
032    
033        @NotNull
034        @Override
035        public JetType getType(@NotNull KotlinBuiltIns kotlinBuiltIns) {
036            return getType();
037        }
038    
039        @NotNull
040        private JetType getType() {
041            JetType type = getClassObjectType(value);
042            assert type != null : "Enum entry must have a class object type: " + value;
043            return type;
044        }
045    
046        @NotNull
047        @Override
048        public ClassDescriptor getValue() {
049            ClassDescriptor value = super.getValue();
050            assert value != null : "Guaranteed by constructor";
051            return value;
052        }
053    
054        @Override
055        public <R, D> R accept(AnnotationArgumentVisitor<R, D> visitor, D data) {
056            return visitor.visitEnumValue(this, data);
057        }
058    
059        @Override
060        public String toString() {
061            return getType() + "." + value.getName();
062        }
063    
064        @Override
065        public boolean equals(Object o) {
066            if (this == o) return true;
067            if (o == null || getClass() != o.getClass()) return false;
068    
069            return value.equals(((EnumValue) o).value);
070        }
071    
072        @Override
073        public int hashCode() {
074            return value.hashCode();
075        }
076    }
077