Rewrite JavaAnnotationArgument interfaces and implementations to Kotlin

This commit is contained in:
Alexander Udalov
2014-08-27 13:30:53 +04:00
parent ec02382976
commit 4c51368244
17 changed files with 133 additions and 434 deletions
@@ -1,63 +0,0 @@
/*
* Copyright 2010-2013 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.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.resolve.java.structure.JavaAnnotationArgument;
import org.jetbrains.jet.lang.resolve.name.Name;
public abstract class JavaAnnotationArgumentImpl<Psi extends PsiAnnotationMemberValue> extends JavaElementImpl<Psi>
implements JavaAnnotationArgument {
private final Name name;
protected JavaAnnotationArgumentImpl(@NotNull Psi psiAnnotationMemberValue, @Nullable Name name) {
super(psiAnnotationMemberValue);
this.name = name;
}
@NotNull
/* package */ static JavaAnnotationArgument create(@NotNull PsiAnnotationMemberValue argument, @Nullable Name name) {
Object value = JavaPsiFacade.getInstance(argument.getProject()).getConstantEvaluationHelper().computeConstantExpression(argument);
if (value != null || argument instanceof PsiLiteralExpression) {
return new JavaLiteralAnnotationArgumentImpl(name, value);
}
if (argument instanceof PsiReferenceExpression) {
return new JavaEnumValueAnnotationArgumentImpl((PsiReferenceExpression) argument, name);
}
else if (argument instanceof PsiArrayInitializerMemberValue) {
return new JavaArrayAnnotationArgumentImpl((PsiArrayInitializerMemberValue) argument, name);
}
else if (argument instanceof PsiAnnotation) {
return new JavaAnnotationAsAnnotationArgumentImpl((PsiAnnotation) argument, name);
}
else if (argument instanceof PsiClassObjectAccessExpression) {
return new JavaClassObjectAnnotationArgumentImpl((PsiClassObjectAccessExpression) argument, name);
}
else {
throw new UnsupportedOperationException("Unsupported annotation argument type: " + argument);
}
}
@Override
@Nullable
public Name getName() {
return name;
}
}
@@ -1,37 +0,0 @@
/*
* Copyright 2010-2013 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.PsiAnnotation;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.resolve.java.structure.JavaAnnotation;
import org.jetbrains.jet.lang.resolve.java.structure.JavaAnnotationAsAnnotationArgument;
import org.jetbrains.jet.lang.resolve.name.Name;
public class JavaAnnotationAsAnnotationArgumentImpl extends JavaAnnotationArgumentImpl<PsiAnnotation>
implements JavaAnnotationAsAnnotationArgument {
protected JavaAnnotationAsAnnotationArgumentImpl(@NotNull PsiAnnotation psiAnnotation, @Nullable Name name) {
super(psiAnnotation, name);
}
@Override
@NotNull
public JavaAnnotation getAnnotation() {
return new JavaAnnotationImpl(getPsi());
}
}
@@ -38,7 +38,7 @@ public class JavaAnnotationImpl extends JavaElementImpl<PsiAnnotation> implement
@Nullable
public JavaAnnotationArgument findArgument(@NotNull Name name) {
PsiAnnotationMemberValue attribute = getPsi().findAttributeValue(name.asString());
return attribute == null ? null : JavaAnnotationArgumentImpl.create(attribute, name);
return attribute == null ? null : JavaAnnotationArgumentImpl.OBJECT$.create(attribute, name);
}
@Override
@@ -1,41 +0,0 @@
/*
* Copyright 2010-2013 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.PsiArrayInitializerMemberValue;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.resolve.java.structure.JavaAnnotationArgument;
import org.jetbrains.jet.lang.resolve.java.structure.JavaArrayAnnotationArgument;
import org.jetbrains.jet.lang.resolve.name.Name;
import java.util.List;
import static org.jetbrains.jet.lang.resolve.java.structure.impl.JavaElementCollectionFromPsiArrayUtil.namelessAnnotationArguments;
public class JavaArrayAnnotationArgumentImpl extends JavaAnnotationArgumentImpl<PsiArrayInitializerMemberValue>
implements JavaArrayAnnotationArgument {
protected JavaArrayAnnotationArgumentImpl(@NotNull PsiArrayInitializerMemberValue psiArrayInitializerMemberValue, @Nullable Name name) {
super(psiArrayInitializerMemberValue, name);
}
@Override
@NotNull
public List<JavaAnnotationArgument> getElements() {
return namelessAnnotationArguments(getPsi().getInitializers());
}
}
@@ -1,37 +0,0 @@
/*
* Copyright 2010-2013 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.PsiClassObjectAccessExpression;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.resolve.java.structure.JavaClassObjectAnnotationArgument;
import org.jetbrains.jet.lang.resolve.java.structure.JavaType;
import org.jetbrains.jet.lang.resolve.name.Name;
public class JavaClassObjectAnnotationArgumentImpl extends JavaAnnotationArgumentImpl<PsiClassObjectAccessExpression>
implements JavaClassObjectAnnotationArgument {
public JavaClassObjectAnnotationArgumentImpl(@NotNull PsiClassObjectAccessExpression expression, @Nullable Name name) {
super(expression, name);
}
@NotNull
@Override
public JavaType getReferencedType() {
return JavaTypeImpl.create(getPsi().getOperand().getType());
}
}
@@ -111,15 +111,6 @@ public class JavaElementCollectionFromPsiArrayUtil {
}
};
private static final Factory<PsiAnnotationMemberValue, JavaAnnotationArgument> NAMELESS_ANNOTATION_ARGUMENTS =
new Factory<PsiAnnotationMemberValue, JavaAnnotationArgument>() {
@NotNull
@Override
public JavaAnnotationArgument create(@NotNull PsiAnnotationMemberValue psiAnnotationMemberValue) {
return JavaAnnotationArgumentImpl.create(psiAnnotationMemberValue, null);
}
};
private static final Factory<PsiNameValuePair, JavaAnnotationArgument> NAMED_ANNOTATION_ARGUMENTS =
new Factory<PsiNameValuePair, JavaAnnotationArgument>() {
@NotNull
@@ -128,7 +119,7 @@ public class JavaElementCollectionFromPsiArrayUtil {
String name = psiNameValuePair.getName();
PsiAnnotationMemberValue value = psiNameValuePair.getValue();
assert value != null : "Annotation argument value cannot be null: " + name;
return JavaAnnotationArgumentImpl.create(value, name == null ? null : Name.identifier(name));
return JavaAnnotationArgumentImpl.OBJECT$.create(value, name == null ? null : Name.identifier(name));
}
};
}
@@ -204,11 +195,6 @@ public class JavaElementCollectionFromPsiArrayUtil {
return convert(annotations, Factories.ANNOTATIONS);
}
@NotNull
public static List<JavaAnnotationArgument> namelessAnnotationArguments(@NotNull PsiAnnotationMemberValue[] memberValues) {
return convert(memberValues, Factories.NAMELESS_ANNOTATION_ARGUMENTS);
}
@NotNull
public static Collection<JavaAnnotationArgument> namedAnnotationArguments(@NotNull PsiNameValuePair[] nameValuePairs) {
return convert(nameValuePairs, Factories.NAMED_ANNOTATION_ARGUMENTS);
@@ -1,45 +0,0 @@
/*
* Copyright 2010-2013 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.PsiElement;
import com.intellij.psi.PsiEnumConstant;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiReferenceExpression;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.resolve.java.structure.JavaEnumValueAnnotationArgument;
import org.jetbrains.jet.lang.resolve.java.structure.JavaField;
import org.jetbrains.jet.lang.resolve.name.Name;
public class JavaEnumValueAnnotationArgumentImpl extends JavaAnnotationArgumentImpl<PsiReferenceExpression>
implements JavaEnumValueAnnotationArgument {
protected JavaEnumValueAnnotationArgumentImpl(@NotNull PsiReferenceExpression psiReferenceExpression, @Nullable Name name) {
super(psiReferenceExpression, name);
}
@Override
@Nullable
public JavaField resolve() {
PsiElement element = getPsi().resolve();
if (element == null) return null;
if (!(element instanceof PsiEnumConstant)) {
throw new IllegalStateException("Reference argument should be an enum value, but was " + element + ": " + element.getText());
}
return new JavaFieldImpl((PsiField) element);
}
}
@@ -1,43 +0,0 @@
/*
* Copyright 2010-2013 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 org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.resolve.java.structure.JavaLiteralAnnotationArgument;
import org.jetbrains.jet.lang.resolve.name.Name;
public class JavaLiteralAnnotationArgumentImpl implements JavaLiteralAnnotationArgument {
private final Name name;
private final Object value;
public JavaLiteralAnnotationArgumentImpl(@Nullable Name name, @Nullable Object value) {
this.name = name;
this.value = value;
}
@Override
@Nullable
public Name getName() {
return name;
}
@Override
@Nullable
public Object getValue() {
return value;
}
}
@@ -0,0 +1,83 @@
/*
* 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.*
import org.jetbrains.jet.lang.resolve.java.structure.*
import org.jetbrains.jet.lang.resolve.name.Name
abstract class JavaAnnotationArgumentImpl<Psi : PsiAnnotationMemberValue>(
psiAnnotationMemberValue: Psi,
override val name: Name?
) : JavaElementImpl<Psi>(psiAnnotationMemberValue), JavaAnnotationArgument {
class object {
fun create(argument: PsiAnnotationMemberValue, name: Name?): JavaAnnotationArgument {
val value = JavaPsiFacade.getInstance(argument.getProject()).getConstantEvaluationHelper().computeConstantExpression(argument)
if (value != null || argument is PsiLiteralExpression) {
return JavaLiteralAnnotationArgumentImpl(name, value)
}
return when (argument) {
is PsiReferenceExpression -> JavaEnumValueAnnotationArgumentImpl(argument as PsiReferenceExpression, name)
is PsiArrayInitializerMemberValue -> JavaArrayAnnotationArgumentImpl(argument as PsiArrayInitializerMemberValue, name)
is PsiAnnotation -> JavaAnnotationAsAnnotationArgumentImpl(argument as PsiAnnotation, name)
is PsiClassObjectAccessExpression -> JavaClassObjectAnnotationArgumentImpl(argument as PsiClassObjectAccessExpression, name)
else -> throw UnsupportedOperationException("Unsupported annotation argument type: " + argument)
}
}
}
}
class JavaLiteralAnnotationArgumentImpl(
override val name: Name?,
override val value: Any?
) : JavaLiteralAnnotationArgument
class JavaArrayAnnotationArgumentImpl(
psiValue: PsiArrayInitializerMemberValue,
name: Name?
) : JavaAnnotationArgumentImpl<PsiArrayInitializerMemberValue>(psiValue, name), JavaArrayAnnotationArgument {
override fun getElements() = getPsi().getInitializers().map { JavaAnnotationArgumentImpl.create(it, null) }
}
class JavaEnumValueAnnotationArgumentImpl(
psiReference: PsiReferenceExpression,
name: Name?
) : JavaAnnotationArgumentImpl<PsiReferenceExpression>(psiReference, name), JavaEnumValueAnnotationArgument {
override fun resolve(): JavaField? {
val element = getPsi().resolve()
return when (element) {
null -> null
is PsiEnumConstant -> JavaFieldImpl(element)
else -> throw IllegalStateException("Reference argument should be an enum value, but was $element: ${element.getText()}")
}
}
}
class JavaClassObjectAnnotationArgumentImpl(
psiExpression: PsiClassObjectAccessExpression,
name: Name?
) : JavaAnnotationArgumentImpl<PsiClassObjectAccessExpression>(psiExpression, name), JavaClassObjectAnnotationArgument {
override fun getReferencedType() = JavaTypeImpl.create(getPsi().getOperand().getType())
}
class JavaAnnotationAsAnnotationArgumentImpl(
psiAnnotation: PsiAnnotation,
name: Name?
) : JavaAnnotationArgumentImpl<PsiAnnotation>(psiAnnotation, name), JavaAnnotationAsAnnotationArgument {
override fun getAnnotation() = JavaAnnotationImpl(getPsi())
}