From 3da5f7e4113fbb0076c8ee10e59499861dcac0e3 Mon Sep 17 00:00:00 2001 From: Mike Holler Date: Wed, 15 Jun 2016 12:00:04 -0500 Subject: [PATCH 1/2] Added support for @value tags --- .gitignore | 7 +- pom.xml | 7 +- .../java/org/stfm/texdoclet/TeXDoclet.java | 90 +++++++++++++++++++ 3 files changed, 102 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 1c5b2d2..6369c2a 100644 --- a/.gitignore +++ b/.gitignore @@ -28,4 +28,9 @@ .Trashes Icon? ehthumbs.db -Thumbs.db \ No newline at end of file +Thumbs.db + +# IntelliJ Artifacts +/target +*.iml +/.idea/ diff --git a/pom.xml b/pom.xml index 90c93d6..6e2713d 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.stfm texdoclet - 0.9-SNAPSHOT + 0.9.1-SNAPSHOT TexDoclet jar @@ -82,6 +82,11 @@ png-encoder 1.5 + + commons-lang + commons-lang + 2.6 + diff --git a/src/main/java/org/stfm/texdoclet/TeXDoclet.java b/src/main/java/org/stfm/texdoclet/TeXDoclet.java index c6e4407..7f781eb 100755 --- a/src/main/java/org/stfm/texdoclet/TeXDoclet.java +++ b/src/main/java/org/stfm/texdoclet/TeXDoclet.java @@ -12,6 +12,7 @@ import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.PrintWriter; +import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -32,11 +33,13 @@ import com.sun.javadoc.PackageDoc; import com.sun.javadoc.ParamTag; import com.sun.javadoc.Parameter; +import com.sun.javadoc.ProgramElementDoc; import com.sun.javadoc.RootDoc; import com.sun.javadoc.SeeTag; import com.sun.javadoc.Tag; import com.sun.javadoc.ThrowsTag; import com.sun.javadoc.Type; +import org.apache.commons.lang.StringUtils; /** * This class provides a Java javadoc Doclet which generates a "; + } else if ("@value".equals(tags[i].name())) { + final String value = getValue(tags[i]); + if (value != null) { + htmlstr += "" + value + ""; + } } else { htmlstr += tags[i].text(); } @@ -2087,6 +2096,87 @@ static void printTags(PackageDoc this_package, Tag[] tags) { os.print(HTMLtoLaTeXBackEnd.fixText(htmlstr)); } + static String getValue(final Tag tag) { + final String text = tag.text().trim(); + final String classpath; + final String member; + + if (text.isEmpty()) { + if (tag.holder() instanceof FieldDoc) { + final FieldDoc fieldDoc = ((FieldDoc) tag.holder()); + return fieldDoc.constantValueExpression(); + } else { + throw new RuntimeException( + "@value cannot be empty when not documenting a field. " + tag + ", position: " + tag.position()); + } + } else if (!text.contains("#")) { + return null; + } else { + final String[] parts = text.split("#"); + + if (parts.length != 2 || parts[1].isEmpty()) { + throw new RuntimeException( + "Invalid tag format: " + tag + ", position: " + tag.position()); + } + + member = parts[1]; + + if (parts[0].isEmpty()) { + if (tag.holder() instanceof ClassDoc) { + final ClassDoc classDoc = ((ClassDoc) tag.holder()); + classpath = classDoc.qualifiedName(); + } else if (tag.holder() instanceof ProgramElementDoc) { + final ProgramElementDoc elDoc = ((ProgramElementDoc) tag.holder()); + classpath = elDoc.qualifiedName().replaceAll("\\." + elDoc.name() + "$", ""); + } else { + throw new RuntimeException("Unable to find parent class."); + } + } else { + classpath = parts[0]; + } + } + + final Class clazz; + try { + clazz = getClassForNameBackoff(classpath); + } catch (final ClassNotFoundException e) { + throw new RuntimeException( + "Unable to find class in {@value " + text + "}. " + tag.position(), e); + } + + final Field field; + try { + field = clazz.getDeclaredField(member); + } catch (final NoSuchFieldException e) { + throw new RuntimeException( + "Field \"" + member + "\n on class \"" + clazz.getCanonicalName() + "\". " + + tag.position(), e); + } + + field.setAccessible(true); + try { + return field.get(null).toString(); + } catch (final IllegalAccessException e) { + throw new RuntimeException( + "Unable to access field: " + field + ". " + tag.position(), e); + } + } + + static Class getClassForNameBackoff(final String classpath) throws ClassNotFoundException { + final int dots = StringUtils.countMatches(classpath, "."); + for (int i = 0; i <= dots; i++) { + final String newClasspath = StringUtils.reverse( + StringUtils.replace(StringUtils.reverse(classpath), ".", "$", i)); + try { + return Class.forName(newClasspath); + } catch (final ClassNotFoundException e) { + System.out.println("Couldn't find: " + newClasspath); + } + } + + throw new ClassNotFoundException(classpath); + } + /** * Prints a reference to a package, class or member. */ From 6d2b849400773596138d8d8a218ef769f461ff9d Mon Sep 17 00:00:00 2001 From: Mike Holler Date: Wed, 15 Jun 2016 12:03:58 -0500 Subject: [PATCH 2/2] Cleanup --- src/main/java/org/stfm/texdoclet/TeXDoclet.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/org/stfm/texdoclet/TeXDoclet.java b/src/main/java/org/stfm/texdoclet/TeXDoclet.java index 7f781eb..044b883 100755 --- a/src/main/java/org/stfm/texdoclet/TeXDoclet.java +++ b/src/main/java/org/stfm/texdoclet/TeXDoclet.java @@ -541,8 +541,6 @@ static public boolean validOptions(String[][] args, DocErrorReporter err) { * the root of the starting document */ public static boolean start(RootDoc root) { -// System.exit(777); - theroot = root; init();