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.descriptors.impl.PropertyGetterDescriptorImpl;
025    import org.jetbrains.kotlin.descriptors.impl.PropertySetterDescriptorImpl;
026    import org.jetbrains.kotlin.name.Name;
027    import org.jetbrains.kotlin.types.KotlinType;
028    
029    import java.util.List;
030    
031    public class JavaPropertyDescriptor extends PropertyDescriptorImpl implements JavaCallableMemberDescriptor {
032        private final boolean isStaticFinal;
033        public JavaPropertyDescriptor(
034                @NotNull DeclarationDescriptor containingDeclaration,
035                @NotNull Annotations annotations,
036                @NotNull Modality modality,
037                @NotNull Visibility visibility,
038                boolean isVar,
039                @NotNull Name name,
040                @NotNull SourceElement source,
041                @Nullable PropertyDescriptor original,
042                boolean isStaticFinal
043        ) {
044            super(containingDeclaration, original, annotations, modality, visibility, isVar, name, Kind.DECLARATION, source,
045                  /* lateInit = */ false, /* isConst = */ false);
046    
047            this.isStaticFinal = isStaticFinal;
048        }
049    
050        @Override
051        public boolean hasSynthesizedParameterNames() {
052            return false;
053        }
054    
055        @NotNull
056        @Override
057        public JavaCallableMemberDescriptor enhance(
058                @Nullable KotlinType enhancedReceiverType,
059                @NotNull List<KotlinType> enhancedValueParametersTypes,
060                @NotNull KotlinType enhancedReturnType
061        ) {
062            JavaPropertyDescriptor enhanced = new JavaPropertyDescriptor(
063                    getContainingDeclaration(),
064                    getAnnotations(),
065                    getModality(),
066                    getVisibility(),
067                    isVar(),
068                    getName(),
069                    getSource(),
070                    getOriginal(),
071                    isStaticFinal
072            );
073    
074            PropertyGetterDescriptorImpl newGetter = null;
075            PropertyGetterDescriptorImpl getter = getGetter();
076            if (getter != null) {
077                newGetter = new PropertyGetterDescriptorImpl(
078                        enhanced, getter.getAnnotations(), getter.getModality(), getter.getVisibility(),
079                        getter.hasBody(), getter.isDefault(), getter.isExternal(), getKind(), getter, getter.getSource());
080                newGetter.setInitialSignatureDescriptor(getter.getInitialSignatureDescriptor());
081                newGetter.initialize(enhancedReturnType);
082            }
083    
084            PropertySetterDescriptorImpl newSetter = null;
085            PropertySetterDescriptor setter = getSetter();
086            if (setter != null) {
087                newSetter = new PropertySetterDescriptorImpl(
088                        enhanced, setter.getAnnotations(), setter.getModality(), setter.getVisibility(),
089                        setter.hasBody(), setter.isDefault(), setter.isExternal(), getKind(), setter, setter.getSource());
090                newSetter.setInitialSignatureDescriptor(newSetter.getInitialSignatureDescriptor());
091                newSetter.initialize(setter.getValueParameters().get(0));
092            }
093    
094            enhanced.initialize(newGetter, newSetter);
095            enhanced.setSetterProjectedOut(isSetterProjectedOut());
096            if (compileTimeInitializer != null) {
097                enhanced.setCompileTimeInitializer(compileTimeInitializer);
098            }
099    
100            for (PropertyDescriptor descriptor : getOverriddenDescriptors()) {
101                enhanced.addOverriddenDescriptor(descriptor);
102            }
103    
104            enhanced.setType(
105                    enhancedReturnType,
106                    getTypeParameters(), // TODO
107                    getDispatchReceiverParameter(),
108                    enhancedReceiverType
109            );
110            return enhanced;
111        }
112    
113        @Override
114        public boolean isConst() {
115            return isStaticFinal && ConstUtil.canBeUsedForConstVal(getType());
116        }
117    }