update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / codeInsight / template / macro / AnnotatedMacro.java
blobeca2fae104ecff7eba613205be0544fe480b4e12
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.intellij.codeInsight.template.macro;
19 import com.intellij.codeInsight.lookup.LookupElement;
20 import com.intellij.codeInsight.lookup.LookupElementBuilder;
21 import com.intellij.codeInsight.template.*;
22 import com.intellij.psi.JavaPsiFacade;
23 import com.intellij.psi.PsiClass;
24 import com.intellij.psi.PsiManager;
25 import com.intellij.psi.PsiMember;
26 import com.intellij.psi.search.GlobalSearchScope;
27 import com.intellij.psi.search.searches.AnnotatedMembersSearch;
28 import com.intellij.util.Query;
29 import org.jetbrains.annotations.NonNls;
30 import org.jetbrains.annotations.NotNull;
32 import java.util.LinkedHashSet;
33 import java.util.Set;
35 /**
36 * Created by IntelliJ IDEA.
37 * User: Maxim.Mossienko
38 * Date: 11.06.2009
39 * Time: 0:20:54
40 * To change this template use File | Settings | File Templates.
42 public class AnnotatedMacro implements Macro {
44 @NonNls
45 public String getName() {
46 return "annotated";
49 public String getDescription() {
50 return "annotated(\"annotation qname\")";
53 @NonNls
54 public String getDefaultValue() {
55 return "";
58 private Query<PsiMember> findAnnotated(ExpressionContext context, Expression[] params) {
59 if (params == null || params.length == 0) return null;
60 PsiManager instance = PsiManager.getInstance(context.getProject());
62 final String paramResult = params[0].calculateResult(context).toString();
63 if (paramResult == null) return null;
64 final GlobalSearchScope scope = GlobalSearchScope.allScope(context.getProject());
65 final PsiClass myBaseClass = JavaPsiFacade.getInstance(instance.getProject()).findClass(paramResult, scope);
67 if (myBaseClass != null) {
68 return AnnotatedMembersSearch.search(myBaseClass, scope);
70 return null;
73 public Result calculateResult(@NotNull Expression[] expressions, ExpressionContext expressionContext) {
74 final Query<PsiMember> psiMembers = findAnnotated(expressionContext, expressions);
76 if (psiMembers != null) {
77 final PsiMember member = psiMembers.findFirst();
79 if (member != null) {
80 return new TextResult(member instanceof PsiClass ? ((PsiClass)member).getQualifiedName():member.getName());
83 return null;
86 public Result calculateQuickResult(@NotNull Expression[] expressions, ExpressionContext expressionContext) {
87 return calculateResult(expressions, expressionContext);
90 public LookupElement[] calculateLookupItems(@NotNull Expression[] params, ExpressionContext context) {
91 final Query<PsiMember> query = findAnnotated(context, params);
93 if (query != null) {
94 Set<LookupElement> set = new LinkedHashSet<LookupElement>();
95 final String secondParamValue = params.length > 1 ? params[1].calculateResult(context).toString() : null;
96 final boolean isShortName = secondParamValue != null && !Boolean.valueOf(secondParamValue);
97 final PsiClass findInClass =
98 secondParamValue != null ? JavaPsiFacade.getInstance(context.getProject()).findClass(secondParamValue) : null;
100 for (PsiMember object : query.findAll()) {
101 if (findInClass != null && !object.getContainingClass().equals(findInClass)) continue;
102 boolean isClazz = object instanceof PsiClass;
103 final String name = isShortName || !isClazz ? object.getName() : ((PsiClass) object).getQualifiedName();
104 set.add(LookupElementBuilder.create(name));
107 return set.toArray(new LookupElement[set.size()]);
109 return LookupElement.EMPTY_ARRAY;