java-如何实例化TypeMirror
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-如何实例化TypeMirror,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3587字,纯文字阅读大概需要6分钟。
内容图文
![java-如何实例化TypeMirror](/upload/InfoBanner/zyjiaocheng/661/c47ede804ca44ee1b15f9746cec603ac.jpg)
我有一个AnnotationProcessor,它可以读取spring webmvc注释并根据发现的内容生成代码.
该代码很好用,但是我需要弄清楚如何对一个以javax.lang.model.type.TypeMirror作为参数的方法进行单元测试,并吐出它的String类型表示,包括泛型(例如java.util.Map< java.lang.String,java.util.List< java.lang.String>>是一个示例,它说明了如果我传递了代表Map< String,List< String>>作为参数的TypeMirror,它将返回什么.
因此,为了对该方法进行单元测试(我将代码基于以下答案:
Getting the qualified class name of generic type with Java 6 annotation processor)我想在单元测试期间模拟或创建TypeMirror.
在我的实际代码中,我使用VariableElement.asType()获得了所需的TypeMirror,但是运行调试器使我发现VariableElement的实际实现是一个核心Java类,而不是api的一部分:
http://www.docjar.com/docs/api/com/sun/tools/javac/code/Symbol $VarSymbol.html
而TypeMirror隐式代码就是这个,类似地埋在jdk的非公开部分:
http://www.docjar.com/docs/api/com/sun/tools/javac/code/Type $ClassType.html
我宁愿不实例化内部Java类型-实例化TypeMirror(或VariableElement)的“正确”方法是什么?有人嘲笑他们可以给我一个例子吗?
这是我要进行单元测试的方法:
private void typeToString(final TypeMirror type, final StringBuilder result,
final char innerClassSeparator) {
type.accept(new SimpleTypeVisitor7<Void, Void>() {
@Override
public Void visitDeclared(DeclaredType declaredType, Void v) {
TypeElement typeElement = (TypeElement) declaredType.asElement();
String rawType = rawTypeToString(typeElement, innerClassSeparator);
result.append(Util.getUnqualifiedClassName(rawType));
//java.lang.* is auto-included by the compiler for all classes
if (!rawType.startsWith("java.lang")) {
importTypes.add(rawType);
}
List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments();
if (!typeArguments.isEmpty()) {
result.append("<");
for (int i = 0; i < typeArguments.size(); i++) {
if (i != 0) {
result.append(", ");
}
typeToString(typeArguments.get(i), result, innerClassSeparator);
}
result.append(">");
}
return null;
}
@Override
public Void visitPrimitive(PrimitiveType primitiveType, Void v) {
result.append(box((PrimitiveType) type).getName());
return null;
}
@Override
public Void visitArray(ArrayType arrayType, Void v) {
TypeMirror type = arrayType.getComponentType();
if (type instanceof PrimitiveType) {
result.append(type.toString()); // Don't box, since this is an array.
} else {
typeToString(arrayType.getComponentType(), result, innerClassSeparator);
}
result.append("[]");
return null;
}
@Override
public Void visitTypeVariable(TypeVariable typeVariable, Void v) {
result.append(typeVariable.asElement().getSimpleName());
return null;
}
@Override
public Void visitError(ErrorType errorType, Void v) {
// Error type found, a type may not yet have been generated, but we need the type
// so we can generate the correct code in anticipation of the type being available
// to the compiler.
// Paramterized types which don't exist are returned as an error type whose name is "<any>"
if ("<any>".equals(errorType.toString())) {
throw new CodeGenerationIncompleteException(
"Type reported as <any> is likely a not-yet generated parameterized type.");
}
result.append(errorType.toString());
return null;
}
@Override
protected Void defaultAction(TypeMirror typeMirror, Void v) {
result.append("void");
return null;
}
}, null);
}
解决方法:
对于一般情况,我会说“这是一个接口,所以只需创建一个模拟”.对于这种情况,我认为您正在测试错误的接缝.
在这种情况下,要测试的真实单元是您匿名声明的SimpleTypeVisitor实现.考虑到要真正测试整个typeToString方法,您必须验证已实现的所有六个方法均与它们的预期行为一致.因此,您应该将匿名实现提升为具体的实现并测试这些方法.他们每个人都应该至少有一个测试,在这些测试中,您可以模拟出typeToString来处理您的递归调用.
内容总结
以上是互联网集市为您收集整理的java-如何实例化TypeMirror全部内容,希望文章能够帮你解决java-如何实例化TypeMirror所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。