Skip to content

Commit 07dea75

Browse files
processing javadoc for interfaces, closes #43
1 parent 09f91e4 commit 07dea75

File tree

2 files changed

+64
-40
lines changed
  • typescript-generator-core/src

2 files changed

+64
-40
lines changed

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/parser/Javadoc.java

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import cz.habarta.typescript.generator.xmldoclet.Class;
55
import cz.habarta.typescript.generator.xmldoclet.Enum;
66
import cz.habarta.typescript.generator.xmldoclet.Field;
7+
import cz.habarta.typescript.generator.xmldoclet.Interface;
78
import cz.habarta.typescript.generator.xmldoclet.Method;
89
import cz.habarta.typescript.generator.xmldoclet.Package;
910
import cz.habarta.typescript.generator.xmldoclet.Root;
@@ -49,35 +50,48 @@ public Model enrichModel(Model model) {
4950
}
5051

5152
private BeanModel enrichBean(BeanModel bean) {
52-
final Class dClass = findJavadocClass(bean.getBeanClass(), dRoots);
53-
final List<String> beanComments = getClassComments(dClass);
53+
if (bean.getBeanClass().isInterface()) {
54+
final Interface dInterface = findJavadocInterface(bean.getBeanClass(), dRoots);
55+
if (dInterface != null) {
56+
return enrichBean(bean, dInterface.getComment(), dInterface.getField(), dInterface.getMethod());
57+
}
58+
} else {
59+
final Class dClass = findJavadocClass(bean.getBeanClass(), dRoots);
60+
if (dClass != null) {
61+
return enrichBean(bean, dClass.getComment(), dClass.getField(), dClass.getMethod());
62+
}
63+
}
64+
return bean;
65+
}
66+
67+
private BeanModel enrichBean(BeanModel bean, String beanComment, List<Field> dFields, List<Method> dMethods) {
5468
final List<PropertyModel> enrichedProperties = new ArrayList<>();
5569
for (PropertyModel property : bean.getProperties()) {
56-
final PropertyModel enrichedProperty = enrichProperty(property, dClass);
70+
final PropertyModel enrichedProperty = enrichProperty(property, dFields, dMethods);
5771
enrichedProperties.add(enrichedProperty);
5872
}
59-
return new BeanModel(bean.getBeanClass(), bean.getParent(), enrichedProperties, concat(beanComments, bean.getComments()));
73+
return new BeanModel(bean.getBeanClass(), bean.getParent(), enrichedProperties, concat(getComments(beanComment), bean.getComments()));
6074
}
6175

62-
private PropertyModel enrichProperty(PropertyModel property, Class dClass) {
63-
final List<String> propertyComments;
76+
private PropertyModel enrichProperty(PropertyModel property, List<Field> dFields, List<Method> dMethods) {
77+
final String propertyComment;
6478
if (property.getOriginalMember() instanceof java.lang.reflect.Method) {
65-
final Method dMethod = findJavadocMethod(property.getOriginalMember().getName(), dClass);
66-
propertyComments = getMethodComments(dMethod);
79+
final Method dMethod = findJavadocMethod(property.getOriginalMember().getName(), dMethods);
80+
propertyComment = dMethod != null ? dMethod.getComment() : null;
6781
} else if (property.getOriginalMember() instanceof java.lang.reflect.Field) {
68-
final Field dField = findJavadocField(property.getOriginalMember().getName(), dClass);
69-
propertyComments = getFieldComments(dField);
82+
final Field dField = findJavadocField(property.getOriginalMember().getName(), dFields);
83+
propertyComment = dField != null ? dField.getComment() : null;
7084
} else {
71-
final Field dField = findJavadocField(property.getName(), dClass);
72-
propertyComments = getFieldComments(dField);
85+
final Field dField = findJavadocField(property.getName(), dFields);
86+
propertyComment = dField != null ? dField.getComment() : null;
7387
}
74-
return property.comments(propertyComments);
88+
return property.comments(getComments(propertyComment));
7589
}
7690

7791
private EnumModel enrichEnum(EnumModel enumModel) {
7892
final Enum dEnum = findJavadocEnum(enumModel.getEnumClass(), dRoots);
79-
final List<String> enumComments = getEnumComments(dEnum);
80-
return new EnumModel(enumModel.getEnumClass(), enumModel.getValues(), concat(enumComments, enumModel.getComments()));
93+
final String enumComment = dEnum != null ? dEnum.getComment() : null;
94+
return new EnumModel(enumModel.getEnumClass(), enumModel.getValues(), concat(getComments(enumComment), enumModel.getComments()));
8195
}
8296

8397
// finders
@@ -96,9 +110,23 @@ private static Class findJavadocClass(java.lang.Class<?> cls, List<Root> dRoots)
96110
return null;
97111
}
98112

99-
private static Field findJavadocField(String name, Class dClass) {
100-
if (dClass != null) {
101-
for (Field dField : dClass.getField()) {
113+
private static Interface findJavadocInterface(java.lang.Class<?> cls, List<Root> dRoots) {
114+
final String name = cls.getName().replace('$', '.');
115+
for (Root dRoot : dRoots) {
116+
for (Package dPackage : dRoot.getPackage()) {
117+
for (Interface dInterface : dPackage.getInterface()) {
118+
if (dInterface.getQualified().equals(name)) {
119+
return dInterface;
120+
}
121+
}
122+
}
123+
}
124+
return null;
125+
}
126+
127+
private static Field findJavadocField(String name, List<Field> dFields) {
128+
if (dFields != null) {
129+
for (Field dField : dFields) {
102130
if (dField.getName().equals(name)) {
103131
return dField;
104132
}
@@ -107,9 +135,9 @@ private static Field findJavadocField(String name, Class dClass) {
107135
return null;
108136
}
109137

110-
private static Method findJavadocMethod(String name, Class dClass) {
111-
if (dClass != null) {
112-
for (Method dMethod : dClass.getMethod()) {
138+
private static Method findJavadocMethod(String name, List<Method> dMethods) {
139+
if (dMethods != null) {
140+
for (Method dMethod : dMethods) {
113141
if (dMethod.getName().equals(name)) {
114142
return dMethod;
115143
}
@@ -132,24 +160,6 @@ private static Enum findJavadocEnum(java.lang.Class<?> cls, List<Root> dRoots) {
132160
return null;
133161
}
134162

135-
// comment getters
136-
137-
private List<String> getClassComments(Class dClass) {
138-
return dClass != null ? getComments(dClass.getComment()) : null;
139-
}
140-
141-
private List<String> getFieldComments(Field dField) {
142-
return dField != null ? getComments(dField.getComment()) : null;
143-
}
144-
145-
private List<String> getMethodComments(Method dMethod) {
146-
return dMethod != null ? getComments(dMethod.getComment()) : null;
147-
}
148-
149-
private List<String> getEnumComments(Enum dEnum) {
150-
return dEnum != null ? getComments(dEnum.getComment()) : null;
151-
}
152-
153163
private List<String> getComments(String dComments) {
154164
if (dComments == null) {
155165
return null;

typescript-generator-core/src/test/java/cz/habarta/typescript/generator/JavadocTest.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void testJavadoc() {
4040
Assert.assertNull(property.getComments());
4141
}
4242
{
43-
final String generated = new TypeScriptGenerator(settings).generateTypeScript(Input.from(ClassWithJavadoc.class));
43+
final String generated = new TypeScriptGenerator(settings).generateTypeScript(Input.from(ClassWithJavadoc.class, InterfaceWithJavadoc.class));
4444
System.out.println(generated);
4545
Assert.assertTrue(generated.contains("Documentation for ClassWithJavadoc. First line."));
4646
Assert.assertTrue(generated.contains("Second line."));
@@ -49,6 +49,8 @@ public void testJavadoc() {
4949
Assert.assertTrue(generated.contains("Documentation for DummyEnum."));
5050
Assert.assertTrue(generated.contains("Documentation for getter property."));
5151
Assert.assertTrue(generated.contains("Documentation for renamed field."));
52+
Assert.assertTrue(generated.contains("Documentation for InterfaceWithJavadoc."));
53+
Assert.assertTrue(generated.contains("Documentation for interface getter property."));
5254
}
5355
}
5456

@@ -83,6 +85,18 @@ public String getGetterPropery() {
8385

8486
}
8587

88+
/**
89+
* Documentation for InterfaceWithJavadoc.
90+
*/
91+
public static interface InterfaceWithJavadoc {
92+
93+
/**
94+
* Documentation for interface getter property.
95+
*/
96+
public String getGetterPropery();
97+
98+
}
99+
86100
public static class ClassWithoutJavadoc {
87101

88102
public String undocumentedField;

0 commit comments

Comments
 (0)