- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个 AnnotationProcessor,它读取 spring webmvc 注释并根据它找到的内容生成代码。
代码运行良好,但我需要弄清楚如何对采用 javax.lang.model.type.TypeMirror
的方法进行单元测试作为参数并返回它的 String 类型表示,包括泛型(例如 java.util.Map<java.lang.String, java.util.List<java.lang.String>>
是一个示例,如果我将表示 TypeMirror
的 Map<String, List<String>>
作为参数传递,它将返回什么。
所以为了对这个方法进行单元测试(我的代码基于这个答案: Getting the qualified class name of generic type with Java 6 annotation processor ) 我想在单元测试期间模拟或创建一个 TypeMirror。
在我的实际代码中,我得到了 TypeMirror
我需要使用 VariableElement.asType()
,但是运行调试器让我发现 VariableElement 的实际实现是一个核心 Java 类,而不是 api 的一部分: http://www.docjar.com/docs/api/com/sun/tools/javac/code/Symbol $VarSymbol.html
TypeMirror impl 是这样的,同样埋藏在 jdk 的非公开部分: http://www.docjar.com/docs/api/com/sun/tools/javac/code/Type $ClassType.html
我不想实例化内部 Java 类型 - 实例化 TypeMirror
的“正确”方法是什么? (或 VariableElement
)?有没有人 mock 过一个可以给我举个例子的地方?
这是我要单元测试的方法:
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);
}
最佳答案
对于一般情况,我会说“它是一个接口(interface),所以只需创建一个模拟”。对于这种情况,我认为您正在测试错误的接缝。
在这种情况下,真正的被测单元是您匿名声明的 SimpleTypeVisitor
实现。考虑到要真正测试整个 typeToString
方法,您必须验证您已实现的所有六个方法是否与其预期行为一致。因此,您应该将匿名实现提升为具体实现并测试这些方法。他们每个人都应该至少有一个测试,在这些测试中你可以模拟 typeToString
来处理你的递归调用。
关于java - 如何实例化 TypeMirror,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27554658/
我有一个 AnnotationProcessor,它读取 spring webmvc 注释并根据它找到的内容生成代码。 代码运行良好,但我需要弄清楚如何对采用 javax.lang.model.typ
我正在使用镜像并想确定一个 ClassMirror 是否是另一个的子类型。有点像: ClassMirror type = me.getField(someSymbol).type; // obviou
我有一个注释,根据 this idiom 接收“动态”参数,即接口(interface)类型的参数。简而言之: public interface MyInterface {} public @inte
我正在使用 Java 的注释处理机制为我的一些类创建序列化程序。 我想检查当前处理类的每个字段并确保它是 MyBaseSerializableClass 的子类。 我的问题是我只知道如何找到字段类型的
我正在做 Java APT 并想解析一个方法(ExecutableElement)。现在我需要方法的返回类型。如果它是一个数组,我不知道如何获取类型。 示例:String[] foobar() 我想获
我刚刚在这里开始使用 javax AnnotationProcessing,遇到了一个丑陋的案例。我将在描述我的学习过程的一系列伪代码行中对其进行说明: MyAnnotation ann = elem
相应于'dart:mirror'上的bool isSubtypeOf(TypeMirror other)文档 /** * Checks the subtype relationship, d
我有一个注释处理器,它接受一个带注释的类并尝试创建它的一个子类: package test; import com.squareup.javapoet.ClassName; import com.sq
我正在开发一个带有注释处理的代码生成器,并将逻辑分为 2 个步骤: 分析带注释的元素并创建数据模型,以及 使用数据模型使用 JavaPoet 生成代码。 在大多数情况下,注释处理 API 为我们提供了
在实用程序界面的文档中 Types ,其中一个实例必须由编译器提供给 Java SE 6 或 7 的注释处理器,对于我正在处理的代码片段,有两种方法让我感兴趣。我需要检查字段的类型是否是从特定抽象类继
我不明白如何在注释处理器中检索枚举值。 我的注释是自定义 Java Bean Validation 注释: @StringEnumeration(enumClass = UserCivility.
我正在使用 Java 注释处理器。我的注释 @foo 用于标记可以在运行时读取到文件或从文件中读取的字段变量。但是,我想在编译时检查变量类型是否实现了 Serializable,这样如果该字段不可序列
有什么方法可以检查 javax.lang.model.type.TypeMirror 是否是原始类型?我尝试过 instanceof PrimitiveType 但这总是产生 true,因为 Prim
我正在将 Java 7 项目迁移到 Java 8。有一种方法正在做这样的事情: if(type instanceof PrimitiveType){ PrimiriveType type = (
我正在编写注释处理器,我需要检查是否有特定的 TypeMirror实现特定接口(interface)。问题Java Annotations Processor: Check if TypeMirror
我有一个 DeclaredType一个字段,我想获得该字段的完全限定类型(原始类型?)。例如,如果字段是: public static Optional foo; 我想得到 java.util.Opt
我的 Xamarin.Forms 有问题不想再加载,它一直给我错误消息 Unable to cast object of type 'System.RuntimeType' to type 'Mono
我是一名优秀的程序员,十分优秀!