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
.util
.xml
;
19 import com
.intellij
.openapi
.module
.Module
;
20 import com
.intellij
.openapi
.util
.text
.StringUtil
;
21 import com
.intellij
.psi
.PsiClass
;
22 import com
.intellij
.psi
.PsiElement
;
23 import com
.intellij
.psi
.PsiReference
;
24 import com
.intellij
.psi
.impl
.source
.resolve
.reference
.impl
.providers
.JavaClassReferenceProvider
;
25 import com
.intellij
.psi
.search
.GlobalSearchScope
;
26 import org
.jetbrains
.annotations
.NotNull
;
27 import org
.jetbrains
.annotations
.Nullable
;
30 * @author Dmitry Avdeev
32 public class PsiClassConverter
extends Converter
<PsiClass
> implements CustomReferenceConverter
<PsiClass
> {
34 public PsiClass
fromString(final String s
, final ConvertContext context
) {
35 final DomElement element
= context
.getInvocationElement();
36 final GlobalSearchScope scope
= element
instanceof GenericDomValue ?
getScope((GenericDomValue
)element
) : null;
37 return DomJavaUtil
.findClass(s
, context
.getFile(), context
.getModule(), scope
);
41 public String
getErrorMessage(@Nullable final String s
, final ConvertContext context
) {
45 public String
toString(final PsiClass t
, final ConvertContext context
) {
46 return t
== null ?
null:t
.getQualifiedName();
50 public PsiReference
[] createReferences(GenericDomValue
<PsiClass
> genericDomValue
, PsiElement element
, ConvertContext context
) {
52 ExtendClass extendClass
= genericDomValue
.getAnnotation(ExtendClass
.class);
53 final JavaClassReferenceProvider provider
= createClassReferenceProvider(genericDomValue
, context
, extendClass
);
54 return provider
.getReferencesByElement(element
);
57 protected JavaClassReferenceProvider
createClassReferenceProvider(GenericDomValue
<PsiClass
> genericDomValue
, ConvertContext context
,
58 ExtendClass extendClass
) {
59 final JavaClassReferenceProvider provider
= new JavaClassReferenceProvider(getScope(genericDomValue
), context
.getPsiManager().getProject());
60 if (extendClass
!= null) {
61 if (StringUtil
.isNotEmpty(extendClass
.value())) {
62 provider
.setOption(JavaClassReferenceProvider
.EXTEND_CLASS_NAMES
, new String
[] {extendClass
.value()});
64 if (extendClass
.instantiatable()) {
65 provider
.setOption(JavaClassReferenceProvider
.INSTANTIATABLE
, Boolean
.TRUE
);
67 if (!extendClass
.allowAbstract()) {
68 provider
.setOption(JavaClassReferenceProvider
.CONCRETE
, Boolean
.TRUE
);
70 if (!extendClass
.allowInterface()) {
71 provider
.setOption(JavaClassReferenceProvider
.NOT_INTERFACE
, Boolean
.TRUE
);
73 if (!extendClass
.allowEnum()) {
74 provider
.setOption(JavaClassReferenceProvider
.NOT_ENUM
, Boolean
.TRUE
);
76 if (extendClass
.jvmFormat()) {
77 provider
.setOption(JavaClassReferenceProvider
.JVM_FORMAT
, Boolean
.TRUE
);
79 provider
.setAllowEmpty(extendClass
.allowEmpty());
83 ClassTemplate template
= genericDomValue
.getAnnotation(ClassTemplate
.class);
84 if (template
!= null) {
85 if (StringUtil
.isNotEmpty(template
.value())) {
86 provider
.setOption(JavaClassReferenceProvider
.CLASS_TEMPLATE
, template
.value());
88 provider
.setOption(JavaClassReferenceProvider
.CLASS_KIND
, template
.kind());
91 provider
.setSoft(true);
96 protected GlobalSearchScope
getScope(final GenericDomValue domValue
) {
97 final Module module
= domValue
.getModule();
98 return module
== null ?
null : GlobalSearchScope
.moduleWithDependenciesAndLibrariesScope(module
);