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.load.java.descriptors;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.annotations.Nullable;
021    import org.jetbrains.kotlin.descriptors.*;
022    import org.jetbrains.kotlin.descriptors.annotations.Annotations;
023    import org.jetbrains.kotlin.descriptors.impl.PropertyDescriptorImpl;
024    import org.jetbrains.kotlin.name.Name;
025    import org.jetbrains.kotlin.types.JetType;
026    
027    import java.util.List;
028    
029    public class JavaPropertyDescriptor extends PropertyDescriptorImpl implements JavaCallableMemberDescriptor {
030        private final boolean isStaticFinal;
031        public JavaPropertyDescriptor(
032                @NotNull DeclarationDescriptor containingDeclaration,
033                @NotNull Annotations annotations,
034                @NotNull Visibility visibility,
035                boolean isVar,
036                @NotNull Name name,
037                @NotNull SourceElement source,
038                @Nullable PropertyDescriptor original,
039                boolean isStaticFinal
040        ) {
041            super(containingDeclaration, original, annotations, Modality.FINAL, visibility, isVar, name, Kind.DECLARATION, source,
042                  /* lateInit = */ false, /* isConst = */ false);
043    
044            this.isStaticFinal = isStaticFinal;
045        }
046    
047        @Override
048        public boolean hasSynthesizedParameterNames() {
049            return false;
050        }
051    
052        @NotNull
053        @Override
054        public JavaCallableMemberDescriptor enhance(
055                @Nullable JetType enhancedReceiverType,
056                @NotNull List<JetType> enhancedValueParametersTypes,
057                @NotNull JetType enhancedReturnType
058        ) {
059            JavaPropertyDescriptor enhanced = new JavaPropertyDescriptor(
060                    getContainingDeclaration(),
061                    getAnnotations(),
062                    getVisibility(),
063                    isVar(),
064                    getName(),
065                    getSource(),
066                    getOriginal(),
067                    isStaticFinal
068            );
069            assert getGetter() == null : "Field must not have a getter: " + this;
070            assert getSetter() == null : "Field must not have a setter: " + this;
071            enhanced.initialize(null, null);
072            enhanced.setSetterProjectedOut(isSetterProjectedOut());
073            if (compileTimeInitializer != null) {
074                enhanced.setCompileTimeInitializer(compileTimeInitializer);
075            }
076            enhanced.setType(
077                    enhancedReturnType,
078                    getTypeParameters(), // TODO
079                    getDispatchReceiverParameter(),
080                    enhancedReceiverType
081            );
082            return enhanced;
083        }
084    
085        @Override
086        public boolean isConst() {
087            return isStaticFinal && ConstUtil.canBeUsedForConstVal(getType());
088        }
089    }