2 * Copyright 2000-2010 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.
16 package com.intellij.psi.augment;
18 import com.intellij.openapi.diagnostic.Logger;
19 import com.intellij.openapi.extensions.ExtensionPointName;
20 import com.intellij.openapi.extensions.Extensions;
21 import com.intellij.psi.PsiElement;
22 import com.intellij.psi.PsiType;
23 import com.intellij.psi.PsiTypeElement;
24 import org.jetbrains.annotations.NotNull;
25 import org.jetbrains.annotations.Nullable;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.List;
32 public abstract class PsiAugmentProvider {
33 private static final Logger LOG = Logger.getInstance("#" + PsiAugmentProvider.class.getName());
34 public static final ExtensionPointName<PsiAugmentProvider> EP_NAME = ExtensionPointName.create("com.intellij.lang.psiAugmentProvider");
37 public abstract <Psi extends PsiElement> List<Psi> getAugments(@NotNull PsiElement element, @NotNull Class<Psi> type);
40 public static <Psi extends PsiElement> List<Psi> collectAugments(@NotNull final PsiElement element, @NotNull final Class<Psi> type) {
41 List<Psi> result = Collections.emptyList();
42 for (PsiAugmentProvider provider : Extensions.getExtensions(EP_NAME)) {
43 List<Psi> augments = provider.getAugments(element, type);
44 if (!augments.isEmpty()) {
45 if (result.isEmpty()) result = new ArrayList<Psi>(augments.size());
46 result.addAll(augments);
54 * Extends {@link PsiTypeElement#getType()} so type could be retrieved from external place
55 * e.g. from variable initializer in lombok case (http://projectlombok.org/features/val.html)
57 * @param typeElement place where inference takes place,
58 * also nested PsiTypeElement-s (e.g. for List<String> PsiTypeElements corresponding to both List and String would be suggested)
59 * @return inferred type or null, if inference is not applicable
64 protected PsiType inferType(PsiTypeElement typeElement) {
69 public static PsiType getInferredType(PsiTypeElement typeElement) {
70 for (PsiAugmentProvider provider : Extensions.getExtensions(EP_NAME)) {
72 final PsiType type = provider.inferType(typeElement);
78 LOG.error("provider: " + provider, e);