use of com.thoughtworks.qdox.model.JavaClass in project maven-plugins by apache.
the class AbstractFixJavadocMojoTest method testReplaceLinkTags_spacesAfterMethod.
public void testReplaceLinkTags_spacesAfterMethod() throws Throwable {
String comment = "/** {@link ConnectException#getMessage() } */";
String source = "import java.net.ConnectException;\n" + comment + "\n" + "public class SpacesAfterMethod {}";
JavaClass clazz = getJavaSource(source).getClassByName("SpacesAfterMethod");
String newComment = AbstractFixJavadocMojo.replaceLinkTags(comment, clazz);
assertEquals("/** {@link java.net.ConnectException#getMessage()} */", newComment);
}
use of com.thoughtworks.qdox.model.JavaClass in project maven-plugins by apache.
the class AbstractFixJavadocMojoTest method testReplaceLinkTags_oneLinkTag.
public void testReplaceLinkTags_oneLinkTag() throws Throwable {
String comment = "/** {@link ConnectException} */";
String source = "import java.net.ConnectException;\n" + comment + "\n" + "public class OneLinkTag {}";
JavaClass clazz = getJavaSource(source).getClassByName("OneLinkTag");
String newComment = AbstractFixJavadocMojo.replaceLinkTags(comment, clazz);
assertEquals("/** {@link java.net.ConnectException} */", newComment);
}
use of com.thoughtworks.qdox.model.JavaClass in project maven-plugins by apache.
the class AbstractFixJavadocMojo method replaceLinkTags.
static String replaceLinkTags(String comment, JavaAnnotatedElement entity) {
StringBuilder resolvedComment = new StringBuilder();
// scan comment for {@link someClassName} and try to resolve this
Matcher linktagMatcher = Pattern.compile("\\{@link\\s").matcher(comment);
int startIndex = 0;
while (linktagMatcher.find()) {
int startName = linktagMatcher.end();
resolvedComment.append(comment.substring(startIndex, startName));
int endName = comment.indexOf("}", startName);
if (endName >= 0) {
String name;
String link = comment.substring(startName, endName);
int hashIndex = link.indexOf('#');
if (hashIndex >= 0) {
name = link.substring(0, hashIndex);
} else {
name = link;
}
if (StringUtils.isNotBlank(name)) {
String typeName;
if (entity instanceof JavaClass) {
JavaClass clazz = (JavaClass) entity;
typeName = TypeResolver.byClassName(clazz.getBinaryName(), clazz.getJavaClassLibrary(), clazz.getSource().getImports()).resolveType(name.trim());
} else if (entity instanceof JavaMember) {
JavaClass clazz = ((JavaMember) entity).getDeclaringClass();
typeName = TypeResolver.byClassName(clazz.getBinaryName(), clazz.getJavaClassLibrary(), clazz.getSource().getImports()).resolveType(name.trim());
} else {
typeName = null;
}
if (typeName == null) {
typeName = name.trim();
} else {
typeName = typeName.replaceAll("\\$", ".");
}
// adjust name for inner classes
resolvedComment.append(typeName);
}
if (hashIndex >= 0) {
resolvedComment.append(link.substring(hashIndex).trim());
}
startIndex = endName;
} else {
startIndex = startName;
}
}
resolvedComment.append(comment.substring(startIndex));
return resolvedComment.toString();
}
Aggregations