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.jvm.kotlinSignature;
018    
019    import com.intellij.openapi.project.Project;
020    import com.intellij.util.containers.ComparatorUtil;
021    import org.jetbrains.annotations.NotNull;
022    import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor;
023    import org.jetbrains.kotlin.descriptors.impl.TypeParameterDescriptorImpl;
024    import org.jetbrains.kotlin.load.java.structure.JavaField;
025    import org.jetbrains.kotlin.psi.KtProperty;
026    import org.jetbrains.kotlin.psi.KtPsiFactoryKt;
027    import org.jetbrains.kotlin.types.KotlinType;
028    
029    import java.util.HashMap;
030    
031    public class AlternativeFieldSignatureData extends ElementAlternativeSignatureData {
032        private KotlinType altReturnType;
033    
034        public AlternativeFieldSignatureData(
035                @NotNull JavaField field,
036                @NotNull KotlinType originalReturnType,
037                @NotNull Project project,
038                boolean isVar
039        ) {
040            String signature = SignaturesUtil.getKotlinSignature(field);
041    
042            if (signature == null) {
043                setAnnotated(false);
044                return;
045            }
046    
047            setAnnotated(true);
048            KtProperty altPropertyDeclaration = KtPsiFactoryKt.KtPsiFactory(project).createProperty(signature);
049    
050            try {
051                checkForSyntaxErrors(altPropertyDeclaration);
052                checkFieldAnnotation(altPropertyDeclaration, field, isVar);
053                altReturnType = computeReturnType(originalReturnType, altPropertyDeclaration.getTypeReference(),
054                                                  new HashMap<TypeParameterDescriptor, TypeParameterDescriptorImpl>());
055            }
056            catch (AlternativeSignatureMismatchException e) {
057                setError(e.getMessage());
058            }
059        }
060    
061        @NotNull
062        public KotlinType getReturnType() {
063            checkForErrors();
064            return altReturnType;
065        }
066    
067        private static void checkFieldAnnotation(@NotNull KtProperty altProperty, @NotNull JavaField field, boolean isVar) {
068            if (!ComparatorUtil.equalsNullable(field.getName().asString(), altProperty.getName())) {
069                throw new AlternativeSignatureMismatchException("Field name mismatch, original: %s, alternative: %s",
070                                                                field.getName().asString(), altProperty.getName());
071            }
072    
073            if (altProperty.getTypeReference() == null) {
074                throw new AlternativeSignatureMismatchException("Field annotation for shouldn't have type reference");
075            }
076    
077            if (altProperty.getGetter() != null || altProperty.getSetter() != null) {
078                throw new AlternativeSignatureMismatchException("Field annotation for shouldn't have getters and setters");
079            }
080    
081            if (altProperty.isVar() != isVar) {
082                throw new AlternativeSignatureMismatchException("Wrong mutability in annotation for field");
083            }
084    
085            if (altProperty.hasInitializer()) {
086                throw new AlternativeSignatureMismatchException("Default value is not expected in annotation for field");
087            }
088        }
089    }