【发布时间】:2016-08-03 05:21:24
【问题描述】:
好的,所以在我正在开发的插件中,我需要获取传递给注解的参数值。我在#idea-users freenode 频道上得到了一个解决方案,我应该将 PsiAnnotationMemberValue 转换为 PsiLiteral,然后调用 getValue()。虽然这适用于原语和字符串之类的东西,但现在我正在尝试获取自定义枚举值。当我尝试这样做时,我的代码抛出了带有以下错误消息的 ClassCastException:
java.lang.ClassCastException: com.intellij.psi.impl.source.tree.java.PsiReferenceExpressionImpl cannot be cast to com.intellij.psi.PsiLiteral
代码:
@Override
public boolean eventIgnoresCancelled(PsiMethod method) {
PsiLiteral literal = null;
for (PsiAnnotation annotation : method.getModifierList().getAnnotations()) {
if(annotation.getQualifiedName().contains("IsCancelled")) {
literal = ((PsiLiteral) annotation.findAttributeValue("value"));
}
}
if(literal == null || literal.getValue() == null) {
return false;
}
Object tristateValue = literal.getValue();
try {
String name = (String) tristateValue.getClass().getMethod("name").invoke(tristateValue);
return name.equalsIgnoreCase("TRUE") || name.equalsIgnoreCase("UNDEFINED");
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace();
}
return false;
}
【问题讨论】:
标签: java intellij-idea intellij-plugin