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