Skip to content

Commit

Permalink
Merge pull request #8 from 4thline/javadoc-1.8
Browse files Browse the repository at this point in the history
com.sun.tools.javadoc.JavadocTool API changed in Java1.8
  • Loading branch information
Christian Bauer committed Apr 9, 2015
2 parents 97bba55 + f96db73 commit 812d85e
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 42 deletions.
12 changes: 0 additions & 12 deletions javadoc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,6 @@
<artifactId>seamless-javadoc</artifactId>
<packaging>jar</packaging>

<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>com.sun</groupId>
Expand Down
115 changes: 85 additions & 30 deletions javadoc/src/main/java/org/seamless/javadoc/EasyDoclet.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
/*
* Copyright (C) 2012 4th Line GmbH, Switzerland
*
* The contents of this file are subject to the terms of either the GNU
* Lesser General Public License Version 2 or later ("LGPL") or the
* Common Development and Distribution License Version 1 or later
* ("CDDL") (collectively, the "License"). You may not use this file
* except in compliance with the License. See LICENSE.txt for more
* information.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
/*
* Copyright (C) 2012 4th Line GmbH, Switzerland
*
* The contents of this file are subject to the terms of either the GNU
* Lesser General Public License Version 2 or later ("LGPL") or the
* Common Development and Distribution License Version 1 or later
* ("CDDL") (collectively, the "License"). You may not use this file
* except in compliance with the License. See LICENSE.txt for more
* information.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
package org.seamless.javadoc;

import com.sun.javadoc.RootDoc;
import com.sun.javadoc.ClassDoc;
import com.sun.javadoc.RootDoc;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Options;
import com.sun.tools.javadoc.JavadocTool;
import com.sun.tools.javadoc.ModifierFilter;
import com.sun.tools.javadoc.PublicMessager;

import javax.tools.JavaFileObject;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.logging.Level;
Expand Down Expand Up @@ -78,7 +81,7 @@ public EasyDoclet(File sourceDirectory, String[] packageNames, File[] fileNames)
}

public EasyDoclet(String locale, File sourceDirectory, String[] packageNames, File[] fileNames) {
this(locale, new File[] {sourceDirectory}, packageNames, fileNames);
this(locale, new File[]{sourceDirectory}, packageNames, fileNames);
}

public EasyDoclet(String locale, File[] sourceDirectories, String[] packageNames, File[] fileNames) {
Expand All @@ -94,7 +97,7 @@ public EasyDoclet(String locale, File[] sourceDirectories, String[] packageNames
sb.append(File.pathSeparatorChar);
}
if (sb.length() > 0)
sb.deleteCharAt(sb.length()-1);
sb.deleteCharAt(sb.length() - 1);

log.fine("Using source path: " + sb.toString());
compOpts.put("-sourcepath", sb.toString());
Expand Down Expand Up @@ -122,26 +125,75 @@ public EasyDoclet(String locale, File[] sourceDirectories, String[] packageNames
JavadocTool javadocTool = JavadocTool.make0(context);

try {
rootDoc = javadocTool.getRootDocImpl(
rootDoc = getDynamicRootDoc(javadocTool, locale, new ModifierFilter(ModifierFilter.ALL_ACCESS),
javaNames.toList(), subPackages.toList());
} catch (Exception ex) {
throw new RuntimeException(ex);
}

if (log.isLoggable(Level.FINEST)) {
for (ClassDoc classDoc : getRootDoc().classes()) {
log.finest("Parsed Javadoc class source: " + classDoc.position() + " with inline tags: " + classDoc.inlineTags().length);
}
}
}

private RootDoc getDynamicRootDoc(final JavadocTool javadocTool, final String locale, final ModifierFilter modifierFilter, final List<String> javaNames, final List<String> subPackages) throws Exception {
String version = Runtime.class.getPackage().getImplementationVersion();
if (version.startsWith("1.8")) {
Method m = javadocTool.getClass().getMethod("getRootDocImpl",
String.class,
String.class,
com.sun.tools.javadoc.ModifierFilter.class,
com.sun.tools.javac.util.List.class,
com.sun.tools.javac.util.List.class,
java.lang.Iterable.class,
boolean.class,
com.sun.tools.javac.util.List.class,
com.sun.tools.javac.util.List.class,
boolean.class,
boolean.class,
boolean.class);

return (RootDoc) m.invoke(javadocTool,
locale,
null,
new ModifierFilter(ModifierFilter.ALL_ACCESS),
javaNames.toList(),
javaNames,
new ListBuffer<String[]>().toList(),
new ListBuffer<JavaFileObject>().toList(),
false,
subPackages.toList(),
subPackages,
new ListBuffer<String>().toList(),
false,
false,
false);
} else {
Method m = javadocTool.getClass().getMethod("getRootDocImpl",
String.class,
String.class,
ModifierFilter.class,
com.sun.tools.javac.util.List.class,
com.sun.tools.javac.util.List.class,
boolean.class,
com.sun.tools.javac.util.List.class,
com.sun.tools.javac.util.List.class,
boolean.class,
boolean.class,
boolean.class);

return (RootDoc) m.invoke(javadocTool,
locale,
null,
new ModifierFilter(ModifierFilter.ALL_ACCESS),
javaNames,
new ListBuffer<String[]>().toList(),
false,
subPackages,
new ListBuffer<String>().toList(),
false,
false,
false);
} catch (Exception ex) {
throw new RuntimeException(ex);
}

if (log.isLoggable(Level.FINEST)) {
for (ClassDoc classDoc : getRootDoc().classes()) {
log.finest("Parsed Javadoc class source: " + classDoc.position() + " with inline tags: " + classDoc.inlineTags().length );
}
}
}

Expand Down Expand Up @@ -172,8 +224,11 @@ public void write(char[] chars, int offset, int length) throws IOException {
log.log(level, s);
}

public void flush() throws IOException {}
public void close() throws IOException {}
public void flush() throws IOException {
}

public void close() throws IOException {
}
}

protected String getApplicationName() {
Expand Down
9 changes: 9 additions & 0 deletions javadoc/src/test/AllTests.tng.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="AllTests - Javadoc">
<test name="Javadoc">
<packages>
<package name="org.seamless.test.javadoc"/>
</packages>
</test>
</suite>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.seamless.test.javadoc;

import org.seamless.javadoc.EasyDoclet;
import org.testng.annotations.Test;

import java.io.File;

/**
* @author Sebastian Roth
*/
public class EasyDocletTest {

@Test
public void invokeDoclet() {
// Should not raise any exception.
final com.sun.javadoc.RootDoc rootDoc = new EasyDoclet(
"en_US",
new File[]{new File("src/test/java")},
new String[]{"org.seamless.test.javadoc"},
new File[0]
).getRootDoc();
}
}

0 comments on commit 812d85e

Please sign in to comment.