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