001 /*
002 * Copyright 2010-2014 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.resolver;
018
019 import com.intellij.psi.PsiExpression;
020 import com.intellij.psi.PsiField;
021 import com.intellij.psi.impl.JavaConstantExpressionEvaluator;
022 import org.jetbrains.annotations.NotNull;
023 import org.jetbrains.annotations.Nullable;
024 import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
025 import org.jetbrains.jet.lang.descriptors.ConstructorDescriptor;
026 import org.jetbrains.jet.lang.descriptors.PropertyDescriptor;
027 import org.jetbrains.jet.lang.descriptors.SimpleFunctionDescriptor;
028 import org.jetbrains.jet.lang.resolve.BindingContextUtils;
029 import org.jetbrains.jet.lang.resolve.BindingTrace;
030 import org.jetbrains.jet.lang.resolve.CompileTimeConstantUtils;
031 import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant;
032 import org.jetbrains.jet.lang.resolve.java.structure.JavaClass;
033 import org.jetbrains.jet.lang.resolve.java.structure.JavaElement;
034 import org.jetbrains.jet.lang.resolve.java.structure.JavaField;
035 import org.jetbrains.jet.lang.resolve.java.structure.JavaMethod;
036 import org.jetbrains.jet.lang.resolve.java.structure.impl.JavaClassImpl;
037 import org.jetbrains.jet.lang.resolve.java.structure.impl.JavaElementImpl;
038 import org.jetbrains.jet.lang.resolve.java.structure.impl.JavaFieldImpl;
039 import org.jetbrains.jet.lang.resolve.java.structure.impl.JavaMethodImpl;
040 import org.jetbrains.jet.lang.resolve.name.FqName;
041
042 import javax.inject.Inject;
043
044 import static org.jetbrains.jet.lang.resolve.BindingContext.*;
045
046 public class TraceBasedJavaResolverCache implements JavaResolverCache {
047 private BindingTrace trace;
048
049 @Inject
050 public void setTrace(BindingTrace trace) {
051 this.trace = trace;
052 }
053
054 @Nullable
055 @Override
056 public ClassDescriptor getClassResolvedFromSource(@NotNull FqName fqName) {
057 return trace.get(FQNAME_TO_CLASS_DESCRIPTOR, fqName.toUnsafe());
058 }
059
060 @Nullable
061 @Override
062 public ClassDescriptor getClass(@NotNull JavaClass javaClass) {
063 return trace.get(CLASS, ((JavaClassImpl) javaClass).getPsi());
064 }
065
066 @Override
067 public void recordMethod(@NotNull JavaMethod method, @NotNull SimpleFunctionDescriptor descriptor) {
068 BindingContextUtils.recordFunctionDeclarationToDescriptor(trace, ((JavaMethodImpl) method).getPsi(), descriptor);
069 }
070
071 @Override
072 public void recordConstructor(@NotNull JavaElement element, @NotNull ConstructorDescriptor descriptor) {
073 trace.record(CONSTRUCTOR, ((JavaElementImpl) element).getPsi(), descriptor);
074 }
075
076 @Override
077 public void recordField(@NotNull JavaField field, @NotNull PropertyDescriptor descriptor) {
078 PsiField psiField = ((JavaFieldImpl) field).getPsi();
079 trace.record(VARIABLE, psiField, descriptor);
080
081 if (!descriptor.isVar()) {
082 PsiExpression initializer = psiField.getInitializer();
083 Object evaluatedExpression = JavaConstantExpressionEvaluator.computeConstantExpression(initializer, false);
084 if (evaluatedExpression != null) {
085 CompileTimeConstant<?> constant =
086 ResolverPackage.resolveCompileTimeConstantValue(evaluatedExpression,
087 CompileTimeConstantUtils.isPropertyCompileTimeConstant(descriptor),
088 descriptor.getType());
089 if (constant != null) {
090 trace.record(COMPILE_TIME_INITIALIZER, descriptor, constant);
091 }
092 }
093 }
094 }
095
096 @Override
097 public void recordClass(@NotNull JavaClass javaClass, @NotNull ClassDescriptor descriptor) {
098 trace.record(CLASS, ((JavaClassImpl) javaClass).getPsi(), descriptor);
099 }
100 }