Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Java 8+ in JavadocParanamer #39

Open
mjustin opened this issue Aug 25, 2021 · 3 comments
Open

Support Java 8+ in JavadocParanamer #39

mjustin opened this issue Aug 25, 2021 · 3 comments

Comments

@mjustin
Copy link
Contributor

mjustin commented Aug 25, 2021

I'm attempting to use JavadocParanamer with the Java 8 Javadocs, since I don't want to try to specially compile a custom version of Java SE just to get the parameter names.

Path javadocPath = Paths.get("/path/to/jdk-8u301-docs-all.zip");
Paranamer paranamer = new CachingParanamer(
        new JavadocParanamer(javadocPath.toFile()));
Method method = LocalDate.class
        .getMethod("of", int.class, int.class, int.class);
String[] parameterNames = paranamer.lookupParameterNames(method);

However, when I do this, an exception is thrown, as the Javadoc HTML format appears to have changed since Java 7:

Exception in thread "main" com.thoughtworks.paranamer.ParameterNamesNotFoundException: public static java.time.LocalDate java.time.LocalDate.of(int,int,int), >\Qof\E</A></(?:B|strong)>\(,?\s*(?:<A[^>]+>)?[\w.]*\Qint\E(?:</A>)?(?:&lt;[^&]+&gt;)?&nbsp;([^),\s]+),?\s*(?:<A[^>]+>)?[\w.]*\Qint\E(?:</A>)?(?:&lt;[^&]+&gt;)?&nbsp;([^),\s]+),?\s*(?:<A[^>]+>)?[\w.]*\Qint\E(?:</A>)?(?:&lt;[^&]+&gt;)?&nbsp;([^),\s]+)\)</CODE>
	at com.thoughtworks.paranamer.JavadocParanamer.getParameterNames(JavadocParanamer.java:167)
	at com.thoughtworks.paranamer.JavadocParanamer.getMethodParameterNames(JavadocParanamer.java:121)
	at com.thoughtworks.paranamer.JavadocParanamer.lookupParameterNames(JavadocParanamer.java:102)
	at com.thoughtworks.paranamer.CachingParanamer.lookupParameterNames(CachingParanamer.java:90)
	at com.thoughtworks.paranamer.CachingParanamer.lookupParameterNames(CachingParanamer.java:83)
	at so.reflect.GenerateMethodSignature.main(GenerateMethodSignature.java:24)

I realize this library is mostly targeted for pre-Java 8, but it seems like this is something Paranamer should support. It looks like it's probably just a matter of fixing a regex to support the new HTML format.

private String[] getParameterNames(AccessibleObject a, String name, Class<?>[] types, String raw) {
if (types.length == 0)
return new String[0];
StringBuilder regex = new StringBuilder();
regex.append(format(">\\Q%s\\E</A></(?:B|strong)>\\(", name));
for (Class klass : types) {
regex.append(format(
",?\\s*(?:<A[^>]+>)?[\\w.]*\\Q%s\\E(?:</A>)?(?:&lt;[^&]+&gt;)?&nbsp;([^),\\s]+)",
klass.getSimpleName()
));
}
regex.append(format("\\)</CODE>"));
Pattern pattern = Pattern.compile(regex.toString(), Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(raw);
if (!matcher.find())
throw new ParameterNamesNotFoundException(a + ", " + regex);

@paul-hammant
Copy link
Owner

Can you give it a go at fixing it with a unit test? I'm happy to merge + release soon after

@mjustin
Copy link
Contributor Author

mjustin commented Aug 25, 2021

@paul-hammant Yep, I'll see if I can carve some free time out to give it a go.

@paul-hammant
Copy link
Owner

paul-hammant commented Aug 27, 2021

Take another look - the Qdox upgrade meant using a different method for FS Target names of classes. All builds now and is ready for your work.

See Issue39TestCase.java in the source

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants