Skip to content

Instantly share code, notes, and snippets.

@pavi2410
Last active Apr 22, 2020
Embed
What would you like to do?
IntelliJ Structural Replace for changing annotation value
import com.intellij.psi.impl.source.tree.java.*
import com.intellij.psi.*
import com.intellij.psi.impl.*
import java.util.stream.*
def str = ""
if (LString instanceof PsiLiteralExpressionImpl) {
str = LString.value
} else if (LString instanceof PsiPolyadicExpressionImpl) {
PsiPolyadicExpression parentExpression = LString
StringBuilder computedValue = new StringBuilder();
for (PsiExpression operand : parentExpression.getOperands()) {
if (operand instanceof PsiReference) {
PsiElement probableDefinition = ((PsiReference) operand).resolve();
if (probableDefinition instanceof PsiVariable) {
PsiExpression initializer = ((PsiVariable) probableDefinition).getInitializer();
if (initializer != null) {
Object value = JavaConstantExpressionEvaluator.computeConstantExpression(initializer, true);
if (value instanceof String) {
computedValue.append(value);
}
}
}
} else {
Object value = JavaConstantExpressionEvaluator.computeConstantExpression(operand, true);
if (value instanceof String) {
computedValue.append(value);
}
}
}
str = computedValue.toString();
}
return str.tokenize(",").stream().map { it.trim() }.map { "\"$it\"" }.collect(Collectors.joining(",\n "))
@UsesPermissions({
$LList$
})
@UsesPermissions(permissionNames = $LString$)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment