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.kt;
018
019 import com.intellij.psi.PsiAnnotation;
020 import com.intellij.psi.PsiAnnotationMemberValue;
021 import com.intellij.psi.PsiLiteralExpression;
022 import org.jetbrains.annotations.NotNull;
023 import org.jetbrains.annotations.Nullable;
024
025 public class PsiAnnotationUtils {
026
027 private PsiAnnotationUtils() {
028 }
029
030 @NotNull
031 public static String getStringAttribute(@Nullable PsiAnnotation annotation, @NotNull String field, @NotNull String defaultValue) {
032 return getAttribute(annotation, field, defaultValue);
033 }
034
035 public static boolean getBooleanAttribute(@Nullable PsiAnnotation annotation, @NotNull String field, boolean defaultValue) {
036 return getAttribute(annotation, field, defaultValue);
037 }
038
039 public static int getIntAttribute(@Nullable PsiAnnotation annotation, @NotNull String field, int defaultValue) {
040 return getAttribute(annotation, field, defaultValue);
041 }
042
043 @NotNull
044 private static <T> T getAttribute(@Nullable PsiAnnotation annotation, @NotNull String field, @NotNull T defaultValue) {
045 if (annotation == null) {
046 return defaultValue;
047 }
048 else {
049 PsiAnnotationMemberValue value = annotation.findAttributeValue(field);
050 if (value instanceof PsiLiteralExpression) {
051 PsiLiteralExpression attributeValue = (PsiLiteralExpression) value;
052 return (T) attributeValue.getValue();
053 }
054 else {
055 return defaultValue;
056 }
057 }
058 }
059
060 }