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