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.structure.impl;
018    
019    import com.intellij.psi.*;
020    import org.jetbrains.annotations.NotNull;
021    import org.jetbrains.annotations.Nullable;
022    import org.jetbrains.jet.lang.resolve.java.structure.JavaAnnotationArgument;
023    import org.jetbrains.jet.lang.resolve.name.Name;
024    
025    public abstract class JavaAnnotationArgumentImpl<Psi extends PsiAnnotationMemberValue> extends JavaElementImpl<Psi>
026            implements JavaAnnotationArgument {
027        private final Name name;
028    
029        protected JavaAnnotationArgumentImpl(@NotNull Psi psiAnnotationMemberValue, @Nullable Name name) {
030            super(psiAnnotationMemberValue);
031            this.name = name;
032        }
033    
034        @NotNull
035        /* package */ static JavaAnnotationArgument create(@NotNull PsiAnnotationMemberValue argument, @Nullable Name name) {
036            Object value = JavaPsiFacade.getInstance(argument.getProject()).getConstantEvaluationHelper().computeConstantExpression(argument);
037            if (value != null || argument instanceof PsiLiteralExpression) {
038                return new JavaLiteralAnnotationArgumentImpl(name, value);
039            }
040    
041            if (argument instanceof PsiReferenceExpression) {
042                return new JavaReferenceAnnotationArgumentImpl((PsiReferenceExpression) argument, name);
043            }
044            else if (argument instanceof PsiArrayInitializerMemberValue) {
045                return new JavaArrayAnnotationArgumentImpl((PsiArrayInitializerMemberValue) argument, name);
046            }
047            else if (argument instanceof PsiAnnotation) {
048                return new JavaAnnotationAsAnnotationArgumentImpl((PsiAnnotation) argument, name);
049            }
050            else if (argument instanceof PsiClassObjectAccessExpression) {
051                return new JavaClassObjectAnnotationArgumentImpl((PsiClassObjectAccessExpression) argument, name);
052            }
053            else {
054                throw new UnsupportedOperationException("Unsupported annotation argument type: " + argument);
055            }
056        }
057    
058        @Override
059        @Nullable
060        public Name getName() {
061            return name;
062        }
063    }