JdtMessageAnalyse extends JdtAbstractModuleAnalyse { @Override public ModuleInfo analyse() { MessageInfo messageInfo = new MessageInfo(); initModuleInfo(messageInfo); messageInfo.addTypeParameters(jdtInfo.getTypeParameters()); FieldDeclaration[] fields = jdtInfo.getType().getFields(); for (FieldDeclaration field : fields) { List fragments = field.fragments(); if (fragments.size() != 1) { throw new AnalyseException("不能处理的情况"); } VariableDeclarationFragment nameFragment = (VariableDeclarationFragment) fragments.get(0); String fieldName = nameFragment.getName().getFullyQualifiedName(); TypeInfo typeInfo = jdtInfo.analyseType(field.getType()); if (typeInfo == null) { throw new AnalyseException("类型解析失败!错误的字段:" + field.getType()); } PropertyInfo propertyInfo = new PropertyInfo(fieldName, typeInfo); propertyInfo.setComment(transform(field.getJavadoc())); transformAnnotations(propertyInfo, field.modifiers()); messageInfo.add(propertyInfo); } return messageInfo; } JdtMessageAnalyse(JdtInfo jdtInfo); @Override ModuleInfo analyse();  }
@Test public void testUser() throws IOException { String code = readCode("api/model/User.java"); Analyse analyse = createAnalyse(); MessageInfo message = (MessageInfo) analyse.analyse(code, "model"); assertEquals("User", message.getName()); assertEquals("model", message.getPackageName()); ImportsInfo imports = message.getImports(); assertEquals(4, imports.getImports().size()); assertImport(imports.get("Message"), Message.class.getName()); assertImport(imports.get("Length"), Length.class.getName()); assertImport(imports.get("Min"), Min.class.getName()); JavadocInfo comment = message.getComment(); Map.Entry<String, Collection<List<String>>> entry = comment.getTags(0); assertJavadocInfo(message.getComment(), null, Collections.singletonList("用户信息")); assertEquals(0, message.getTypeParameters().size()); testProperty(message.getProperty("id"), "id", TypeInfo.Type.LONG); testProperty( message.getProperty("name"), "name", TypeInfo.Type.STRING, Length.class.getName(), Collections.singletonList("max=255") ); assertJavadocInfo(message.getProperty("name").getComment(), null, Collections.singletonList("名称允许重复")); testProperty( message.getProperty("age"), "age", TypeInfo.Type.INT, Min.class.getName(), Collections.singletonList("14") ); }
@Test public void testTestObject() throws IOException { String code = readCode("api/model/TestObject.java"); Analyse analyse = createAnalyse(); MessageInfo message = (MessageInfo) analyse.analyse(code, "model"); assertEquals("TestObject", message.getName()); assertEquals("model", message.getPackageName()); assertEquals(message.getTypeParameters(), Collections.singletonList("T")); PropertyInfo generic = message.getProperty("generic"); assertEquals(generic.getTypeInfo().getFullName(), "T"); PropertyInfo generics = message.getProperty("generics"); assertEquals(generics.getTypeInfo().getFullName(), "T"); assertTrue(generics.getTypeInfo().isArray()); assertTrue(generics.getTypeInfo().isGeneric()); PropertyInfo genericObjs = message.getProperty("genericObjs"); assertTrue(generics.getTypeInfo().isArray()); TypeInfo typeInfo = genericObjs.getTypeInfo().getTypeArguments().get(0); assertEquals(typeInfo.getFullName(), "T"); assertFalse(typeInfo.isArray()); assertTrue(typeInfo.isGeneric()); }