5
\$\begingroup\$

I'm too lazy to write a JavaDoc comment myself, there's a lot of typing and it is mostly boilerplate. Write a program to generate a JavaDoc comment with $ as a placeholder given a method specification.

Example input:

Integer method(int arg1, String arg2) throws InvalidArgumentException, NullPointerException

Example output:

/**
    $
@param arg1 $
@param arg2 $
@return $
@throws InvalidArgumentException $
@throws NullPointerException $
*/

Some notes:

  • There won't be any static, private, or other qualifier on the method, just the return type.
  • There should be no @return tag if the return type is void.
  • It is possible that there won't be any throws clause.
  • You should accept the one-line method spec on stdin and write the result to stdout.

Shortest code wins.

\$\endgroup\$
11
  • \$\begingroup\$ The javadoc comments on the Wikipedia page show leading ` * ` on the second to penultimate lines. Could you either change the example output to match it, or clarify that no leading ` * ` should be printed? \$\endgroup\$
    – Joey Adams
    Jul 26, 2011 at 2:01
  • 1
    \$\begingroup\$ @Joey - the * you mention are optional in Javadoc comments - they are not needed for this question. \$\endgroup\$ Jul 26, 2011 at 2:11
  • \$\begingroup\$ What's about arrays and generic types as param/return type? They introduce new opportunity for blanks. Do we assume all delimiters being blanks, or may they be tabs? After a comma - is there always a blank (or a tab)? \$\endgroup\$ Jul 26, 2011 at 2:30
  • \$\begingroup\$ You may assume that all types will be space-free (but as you point out, they may have []<> characters in them). You may also assume that all blank spaces are composed of space characters only (i.e., no tabs). There may or may not be spaces surrounding (both before and after) a comma. \$\endgroup\$ Jul 26, 2011 at 2:52
  • \$\begingroup\$ the @return tag for that input shouldn't be @return Integer $? \$\endgroup\$
    – JBernardo
    Jul 26, 2011 at 3:09

4 Answers 4

2
\$\begingroup\$

Perl 5.14 - 192 bytes

#!perl -n
print'/**
    $
';@F=split/\(|\) throws +|\)/;print"\@param $_ \$
"for values{split/[, ]+/,$F[1]};print(($F[0]!~/^void /?'@return $
':''),(map{"\@throws $_ \$
"}$F[2]=~/\w+/g),"*/")
\$\endgroup\$
1
  • \$\begingroup\$ This fails when there is no space between the closing parenthesis and throws. \$\endgroup\$ Jul 29, 2011 at 17:19
2
\$\begingroup\$

PowerShell, 209 213

'/**
    $'
($i="$input")-replace'.*\( *| *\).*'-split','-ne''|%{"@param $((-split$_)[1]) $"}
,'@return $'*!($i-clike'void *')
if($i-match'\) *t'){$i-replace'.*\) *throws *'-split' *, *'|%{"@throws $_ $"}}'*/'

bash and PowerShell test scripts. but I'm probably missing a few cases still. But 22 tests are better than only one example, I guess :-)

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Wow, for an untrained eye, it looks as unreadable as the Perl script below... :-) \$\endgroup\$
    – PhiLho
    Jul 28, 2011 at 8:19
1
\$\begingroup\$

Ruby, 177 182 186

a,e=gets.split /\bthrows\b/
a=a.split /[\s(),]+/
puts'/**
    $',3.step(a.size,2).map{|i|"@param #{a[i]} $"},a[0]=='void'?[]:'@return $',e ?e.scan(/\w+/).map{|x|"@throws #{x} $"}:[],'*/'
\$\endgroup\$
1
  • \$\begingroup\$ Doesn't handle generic throws types correctly, but I'll take it. \$\endgroup\$ Sep 16, 2011 at 20:14
0
\$\begingroup\$

scala 579:

import scala.util.parsing.combinator._
object M extends JavaTokenParsers{def i=ident
type P=Parser[Any]
def r:P=n~"("~rep(p)~")"~opt("throws"~rep(t))^^(s=>"\n/**\n    $"+s._1._1._2.mkString("")+s._1._1._1+"\n2:"+(s._2.getOrElse(""))+"\n*/")
def n:P=i~i^^(s=>if(s._1=="void")""else"\nreturn $")
def p:P=i~i~opt(",")^^(s=>("\n@param "+s._1._2+" $"))
def t:P=i~opt(",")^^(s=>("\n@throws "+s._1+" $"))
def main (args:Array[String])={val i=parseAll(r,readLine())
println(i.toString().replaceAll(".:.*","").replaceAll("[~(),]","").replaceAll("^[\\[].*","").replaceAll ("\n\n","\n"))}}

I'm sorry, I'm not experienced with TokenParsers. I guess I'm doing it wrong, and there is much room for improvement.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.