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.constants;
018
019 import org.jetbrains.annotations.NotNull;
020 import org.jetbrains.annotations.Nullable;
021 import org.jetbrains.jet.lang.descriptors.annotations.AnnotationArgumentVisitor;
022 import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
023 import org.jetbrains.jet.lang.types.*;
024 import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
025 import org.jetbrains.jet.lang.descriptors.annotations.Annotations;
026
027 import java.util.Collections;
028
029 public class IntegerValueTypeConstant extends CompileTimeConstant<Number> {
030
031 private final IntegerValueTypeConstructor typeConstructor;
032
033 public IntegerValueTypeConstant(@NotNull Number value, boolean canBeUsedInAnnotations) {
034 super(value, canBeUsedInAnnotations, true);
035 this.typeConstructor = new IntegerValueTypeConstructor(value.longValue());
036 }
037
038 @NotNull
039 @Override
040 public JetType getType(@NotNull KotlinBuiltIns kotlinBuiltIns) {
041 return new JetTypeImpl(
042 Annotations.EMPTY, typeConstructor,
043 false, Collections.<TypeProjection>emptyList(),
044 ErrorUtils.createErrorScope("Scope for number value type (" + typeConstructor.toString() + ")", true));
045 }
046
047 @Nullable
048 @Override
049 @Deprecated
050 public Number getValue() {
051 throw new UnsupportedOperationException("Use IntegerValueTypeConstant.getValue(expectedType) instead");
052 }
053
054 @NotNull
055 public JetType getType(@NotNull JetType expectedType) {
056 return TypeUtils.getPrimitiveNumberType(typeConstructor, expectedType);
057 }
058
059 @NotNull
060 public Number getValue(@NotNull JetType expectedType) {
061 Number numberValue = typeConstructor.getValue();
062 KotlinBuiltIns builtIns = KotlinBuiltIns.getInstance();
063
064 JetType valueType = getType(expectedType);
065 if (valueType.equals(builtIns.getIntType())) {
066 return numberValue.intValue();
067 }
068 else if (valueType.equals(builtIns.getByteType())) {
069 return numberValue.byteValue();
070 }
071 else if (valueType.equals(builtIns.getShortType())) {
072 return numberValue.shortValue();
073 }
074 else {
075 return numberValue.longValue();
076 }
077 }
078
079 @Override
080 public <R, D> R accept(AnnotationArgumentVisitor<R, D> visitor, D data) {
081 return visitor.visitNumberTypeValue(this, data);
082 }
083
084 @Override
085 public String toString() {
086 return typeConstructor.toString();
087 }
088 }