Write constant initializer for java properties

This commit is contained in:
Natalia Ukhorskaya
2014-02-07 15:35:43 +04:00
parent 74c4234c64
commit 17259a052e
18 changed files with 135 additions and 38 deletions
@@ -0,0 +1 @@
org.jetbrains.jet.lang.resolve.java.structure.impl.JavaPropertyInitializerEvaluatorImpl
@@ -77,20 +77,6 @@ public class TraceBasedJavaResolverCache implements JavaResolverCache {
public void recordField(@NotNull JavaField field, @NotNull PropertyDescriptor descriptor) {
PsiField psiField = ((JavaFieldImpl) field).getPsi();
trace.record(VARIABLE, psiField, descriptor);
if (!descriptor.isVar()) {
PsiExpression initializer = psiField.getInitializer();
Object evaluatedExpression = JavaConstantExpressionEvaluator.computeConstantExpression(initializer, false);
if (evaluatedExpression != null) {
CompileTimeConstant<?> constant =
ResolverPackage.resolveCompileTimeConstantValue(evaluatedExpression,
CompileTimeConstantUtils.isPropertyCompileTimeConstant(descriptor),
descriptor.getType());
if (constant != null) {
trace.record(COMPILE_TIME_INITIALIZER, descriptor, constant);
}
}
}
}
@Override
@@ -17,8 +17,10 @@
package org.jetbrains.jet.lang.resolve.java.structure.impl;
import com.intellij.psi.PsiEnumConstant;
import com.intellij.psi.PsiExpression;
import com.intellij.psi.PsiField;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.resolve.java.structure.JavaField;
import org.jetbrains.jet.lang.resolve.java.structure.JavaType;
@@ -37,4 +39,9 @@ public class JavaFieldImpl extends JavaMemberImpl<PsiField> implements JavaField
public JavaType getType() {
return JavaTypeImpl.create(getPsi().getType());
}
@Nullable
public PsiExpression getInitializer() {
return getPsi().getInitializer();
}
}
@@ -0,0 +1,45 @@
/*
* Copyright 2010-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.lang.resolve.java.structure.impl;
import com.intellij.psi.PsiExpression;
import com.intellij.psi.impl.JavaConstantExpressionEvaluator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.PropertyDescriptor;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant;
import org.jetbrains.jet.lang.resolve.constants.ConstantUtils;
import org.jetbrains.jet.lang.resolve.java.structure.JavaField;
import org.jetbrains.jet.lang.resolve.java.structure.JavaPropertyInitializerEvaluator;
public class JavaPropertyInitializerEvaluatorImpl extends JavaPropertyInitializerEvaluator {
@Nullable
@Override
public CompileTimeConstant<?> getInitializerConstant(@NotNull JavaField field, @NotNull PropertyDescriptor descriptor) {
PsiExpression initializer = ((JavaFieldImpl) field).getInitializer();
Object evaluatedExpression = JavaConstantExpressionEvaluator.computeConstantExpression(initializer, false);
if (evaluatedExpression != null) {
return ConstantUtils.createCompileTimeConstant(
evaluatedExpression,
DescriptorUtils.isPropertyCompileTimeConstant(descriptor),
false,
descriptor.getType());
}
return null;
}
}