mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-112302 Convert Java annotation arguments to Groovy
This commit is contained in:
+16
-17
@@ -22,7 +22,6 @@ import com.intellij.psi.impl.PsiImplUtil;
|
||||
import com.intellij.psi.impl.light.LightClassReference;
|
||||
import com.intellij.psi.impl.light.LightElement;
|
||||
import com.intellij.psi.meta.PsiMetaData;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.containers.ContainerUtilRt;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -32,8 +31,10 @@ import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationArgumentList;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationMemberValue;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationNameValuePair;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement;
|
||||
import org.jetbrains.plugins.groovy.refactoring.convertJavaToGroovy.AnnotationArgConverter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -91,11 +92,7 @@ public class GrLightAnnotation extends LightElement implements GrAnnotation {
|
||||
|
||||
@Override
|
||||
public String getText() {
|
||||
StringBuilder buffer = new StringBuilder();
|
||||
buffer.append('@').append(myQualifiedName);
|
||||
buffer.append(myAnnotationArgList.getText());
|
||||
|
||||
return buffer.toString();
|
||||
return "@" + myQualifiedName + myAnnotationArgList.getText();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -141,19 +138,21 @@ public class GrLightAnnotation extends LightElement implements GrAnnotation {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void addAttribute(PsiNameValuePair attribute) {
|
||||
if (attribute instanceof GrAnnotationNameValuePair) {
|
||||
myAnnotationArgList.addAttribute((GrAnnotationNameValuePair)attribute);
|
||||
public void addAttribute(PsiNameValuePair pair) {
|
||||
if (pair instanceof GrAnnotationNameValuePair) {
|
||||
myAnnotationArgList.addAttribute((GrAnnotationNameValuePair)pair);
|
||||
}
|
||||
else {
|
||||
try {
|
||||
GrAnnotation annotation =
|
||||
GroovyPsiElementFactory.getInstance(getProject()).createAnnotationFromText("@Anno(" + attribute.getText() + ")");
|
||||
myAnnotationArgList.addAttribute(annotation.getParameterList().getAttributes()[0]);
|
||||
}
|
||||
catch (IncorrectOperationException e) {
|
||||
//do nothing
|
||||
}
|
||||
GrAnnotationMemberValue newValue = new AnnotationArgConverter().convert(pair.getValue());
|
||||
if (newValue == null) return;
|
||||
|
||||
String name = pair.getName();
|
||||
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(pair.getProject());
|
||||
String annotationText;
|
||||
annotationText = name != null ? "@A(" + name + "=" + newValue.getText() + ")"
|
||||
: "@A(" + newValue.getText() + ")";
|
||||
GrAnnotation annotation = factory.createAnnotationFromText(annotationText);
|
||||
myAnnotationArgList.addAttribute(annotation.getParameterList().getAttributes()[0]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+142
@@ -0,0 +1,142 @@
|
||||
/*
|
||||
* Copyright 2000-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.plugins.groovy.refactoring.convertJavaToGroovy;
|
||||
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationMemberValue;
|
||||
|
||||
/**
|
||||
* Created by Max Medvedev on 8/19/13
|
||||
*/
|
||||
public class AnnotationArgConverter {
|
||||
@Nullable
|
||||
public GrAnnotationMemberValue convert(PsiAnnotationMemberValue value) {
|
||||
final StringBuilder buffer = new StringBuilder();
|
||||
|
||||
buffer.append("@A(");
|
||||
generateText(value, buffer);
|
||||
buffer.append(")");
|
||||
|
||||
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(value.getProject());
|
||||
try {
|
||||
return factory.createAnnotationFromText(buffer.toString()).getParameterList().getAttributes()[0].getValue();
|
||||
}
|
||||
catch (IncorrectOperationException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void generateText(PsiAnnotationMemberValue value, final StringBuilder buffer) {
|
||||
value.accept(new JavaElementVisitor() {
|
||||
@Override
|
||||
public void visitAnnotation(PsiAnnotation annotation) {
|
||||
buffer.append("@");
|
||||
PsiJavaCodeReferenceElement ref = annotation.getNameReferenceElement();
|
||||
if (ref == null) return;
|
||||
|
||||
PsiElement resolved = ref.resolve();
|
||||
|
||||
if (resolved instanceof PsiClass && ((PsiClass)resolved).getQualifiedName() != null) {
|
||||
buffer.append(((PsiClass)resolved).getQualifiedName());
|
||||
}
|
||||
else {
|
||||
buffer.append(ref.getText());
|
||||
}
|
||||
|
||||
PsiAnnotationParameterList parameterList = annotation.getParameterList();
|
||||
parameterList.accept(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitAnnotationParameterList(PsiAnnotationParameterList list) {
|
||||
PsiNameValuePair[] attributes = list.getAttributes();
|
||||
if (attributes.length > 0) {
|
||||
buffer.append('(');
|
||||
for (PsiNameValuePair attribute : attributes) {
|
||||
attribute.accept(this);
|
||||
buffer.append(',');
|
||||
}
|
||||
buffer.replace(buffer.length() - 1, buffer.length(), ")");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitNameValuePair(PsiNameValuePair pair) {
|
||||
String name = pair.getName();
|
||||
PsiAnnotationMemberValue value = pair.getValue();
|
||||
|
||||
if (name != null) {
|
||||
buffer.append(name);
|
||||
buffer.append('=');
|
||||
}
|
||||
|
||||
if (value != null) {
|
||||
value.accept(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitExpression(PsiExpression expression) {
|
||||
buffer.append(expression.getText());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitAnnotationArrayInitializer(PsiArrayInitializerMemberValue initializer) {
|
||||
PsiAnnotationMemberValue[] initializers = initializer.getInitializers();
|
||||
processInitializers(initializers);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitNewExpression(PsiNewExpression expression) {
|
||||
PsiArrayInitializerExpression arrayInitializer = expression.getArrayInitializer();
|
||||
if (arrayInitializer == null) {
|
||||
super.visitNewExpression(expression);
|
||||
}
|
||||
else {
|
||||
PsiType type = expression.getType();
|
||||
if (type == null) {
|
||||
type = PsiType.getJavaLangObject(expression.getManager(), expression.getResolveScope()).createArrayType();
|
||||
}
|
||||
buffer.append('(');
|
||||
arrayInitializer.accept(this);
|
||||
buffer.append(" as ");
|
||||
buffer.append(type.getCanonicalText());
|
||||
buffer.append(")");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitArrayInitializerExpression(PsiArrayInitializerExpression arrayInitializer) {
|
||||
processInitializers(arrayInitializer.getInitializers());
|
||||
}
|
||||
|
||||
private void processInitializers(PsiAnnotationMemberValue[] initializers) {
|
||||
buffer.append('[');
|
||||
for (PsiAnnotationMemberValue initializer : initializers) {
|
||||
initializer.accept(this);
|
||||
buffer.append(',');
|
||||
}
|
||||
if (initializers.length > 0) {
|
||||
buffer.delete(buffer.length() - 1, buffer.length());
|
||||
}
|
||||
buffer.append(']');
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright 2000-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.plugins.groovy.refactoring.convertJavaToGroovy
|
||||
|
||||
import com.intellij.ide.highlighter.JavaFileType
|
||||
import com.intellij.psi.PsiAnnotationMemberValue
|
||||
import com.intellij.psi.PsiJavaFile
|
||||
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase
|
||||
import org.jetbrains.annotations.NotNull
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationMemberValue
|
||||
|
||||
/**
|
||||
* Created by Max Medvedev on 8/19/13
|
||||
*/
|
||||
class ConvertAnnotationMemberValueTest extends LightCodeInsightFixtureTestCase {
|
||||
void testSimpleExpression1() {
|
||||
doTest('1 + 1', '1 + 1')
|
||||
}
|
||||
|
||||
void testSimpleExpression2() {
|
||||
doTest('foo(bar)', 'foo(bar)')
|
||||
}
|
||||
|
||||
void testSimpleAnnotation1() {
|
||||
doTest('@A', '@A')
|
||||
}
|
||||
|
||||
void testSimpleAnnotation2() {
|
||||
doTest('@A()', '@A')
|
||||
}
|
||||
|
||||
void testSimpleAnnotation3() {
|
||||
doTest('@A(1, value = 2)', '@A(1,value=2)')
|
||||
}
|
||||
|
||||
void testAnnotationInitializer() {
|
||||
doTest('@A({1, 2, 3})', '@A([1,2,3])')
|
||||
}
|
||||
|
||||
void testArrayInitializer() {
|
||||
doTest('new int[]{1, 2, 3}', '([1,2,3] as int[])')
|
||||
}
|
||||
|
||||
void doTest(@NotNull String java, @NotNull String expectedGroovy) {
|
||||
myFixture.configureByText(JavaFileType.INSTANCE, '@A(' + java + ') class Clazz{}')
|
||||
|
||||
PsiJavaFile file = myFixture.file as PsiJavaFile
|
||||
PsiAnnotationMemberValue value = file.classes[0].modifierList.annotations[0].parameterList.attributes[0].value
|
||||
GrAnnotationMemberValue result = new AnnotationArgConverter().convert(value)
|
||||
|
||||
assertEquals(expectedGroovy, result.text)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user