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.calls;
018
019 import org.jetbrains.annotations.NotNull;
020 import org.jetbrains.annotations.Nullable;
021 import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
022
023 import java.lang.ref.WeakReference;
024 import java.util.*;
025
026 public class CallResolverExtensionProvider {
027
028 private final static CompositeExtension DEFAULT =
029 new CompositeExtension(Arrays.asList(new NeedSyntheticCallResolverExtension(), new TypeParameterAsReifiedCheck()));
030
031 private WeakReference<Map<DeclarationDescriptor, List<CallResolverExtension>>> extensionsCache;
032
033 @NotNull
034 public CallResolverExtension createExtension(@Nullable DeclarationDescriptor descriptor) {
035 if (descriptor == null) {
036 return DEFAULT;
037 }
038 return new CompositeExtension(createExtensions(descriptor));
039 }
040
041 // create extension list with default one at the end
042 @NotNull
043 private List<CallResolverExtension> createExtensions(@NotNull DeclarationDescriptor declaration) {
044 Map<DeclarationDescriptor, List<CallResolverExtension>> map;
045 if (extensionsCache == null || (map = extensionsCache.get()) == null) {
046 map = new HashMap<DeclarationDescriptor, List<CallResolverExtension>>();
047 extensionsCache = new WeakReference<Map<DeclarationDescriptor, List<CallResolverExtension>>>(map);
048 }
049
050 List<CallResolverExtension> extensions = map.get(declaration);
051 if (extensions != null) {
052 return extensions;
053 }
054
055 extensions = new ArrayList<CallResolverExtension>();
056
057 DeclarationDescriptor parent = declaration.getContainingDeclaration();
058 if (parent != null) {
059 extensions.addAll(createExtensions(parent));
060 extensions.remove(extensions.size() - 1);//remove default from parent list
061 }
062
063 appendExtensionsFor(declaration, extensions);
064
065 List<CallResolverExtension> immutableResult = Collections.unmodifiableList(extensions);
066 map.put(declaration, immutableResult);
067
068 return immutableResult;
069 }
070
071 // with default one at the end
072 private void appendExtensionsFor(DeclarationDescriptor declarationDescriptor, List<CallResolverExtension> extensions) {
073 // add your extensions here
074 extensions.add(DEFAULT);
075 }
076 }