325N/A/*
325N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.codemodel.internal;
325N/A
325N/Aimport java.util.HashMap;
325N/Aimport java.util.Map;
325N/A
325N/A/**
325N/A * JavaDoc comment.
325N/A *
325N/A * <p>
325N/A * A javadoc comment consists of multiple parts. There's the main part (that comes the first in
325N/A * in the comment section), then the parameter parts (@param), the return part (@return),
325N/A * and the throws parts (@throws).
325N/A *
325N/A * TODO: it would be nice if we have JComment class and we can derive this class from there.
325N/A */
325N/Apublic class JDocComment extends JCommentPart implements JGenerable {
325N/A
325N/A private static final long serialVersionUID = 1L;
325N/A
325N/A /** list of @param tags */
325N/A private final Map<String,JCommentPart> atParams = new HashMap<String,JCommentPart>();
325N/A
325N/A /** list of xdoclets */
325N/A private final Map<String,Map<String,String>> atXdoclets = new HashMap<String,Map<String,String>>();
325N/A
325N/A /** list of @throws tags */
325N/A private final Map<JClass,JCommentPart> atThrows = new HashMap<JClass,JCommentPart>();
325N/A
325N/A /**
325N/A * The @return tag part.
325N/A */
325N/A private JCommentPart atReturn = null;
325N/A
325N/A /** The @deprecated tag */
325N/A private JCommentPart atDeprecated = null;
325N/A
325N/A private final JCodeModel owner;
325N/A
325N/A
325N/A public JDocComment(JCodeModel owner) {
325N/A this.owner = owner;
325N/A }
325N/A
325N/A public JDocComment append(Object o) {
325N/A add(o);
325N/A return this;
325N/A }
325N/A
325N/A /**
325N/A * Append a text to a @param tag to the javadoc
325N/A */
325N/A public JCommentPart addParam( String param ) {
325N/A JCommentPart p = atParams.get(param);
325N/A if(p==null)
325N/A atParams.put(param,p=new JCommentPart());
325N/A return p;
325N/A }
325N/A
325N/A /**
325N/A * Append a text to an @param tag.
325N/A */
325N/A public JCommentPart addParam( JVar param ) {
325N/A return addParam( param.name() );
325N/A }
325N/A
325N/A
325N/A /**
325N/A * add an @throws tag to the javadoc
325N/A */
325N/A public JCommentPart addThrows( Class<? extends Throwable> exception ) {
325N/A return addThrows( owner.ref(exception) );
325N/A }
325N/A
325N/A /**
325N/A * add an @throws tag to the javadoc
325N/A */
325N/A public JCommentPart addThrows( JClass exception ) {
325N/A JCommentPart p = atThrows.get(exception);
325N/A if(p==null)
325N/A atThrows.put(exception,p=new JCommentPart());
325N/A return p;
325N/A }
325N/A
325N/A /**
325N/A * Appends a text to @return tag.
325N/A */
325N/A public JCommentPart addReturn() {
325N/A if(atReturn==null)
325N/A atReturn = new JCommentPart();
325N/A return atReturn;
325N/A }
325N/A
325N/A /**
325N/A * add an @deprecated tag to the javadoc, with the associated message.
325N/A */
325N/A public JCommentPart addDeprecated() {
325N/A if(atDeprecated==null)
325N/A atDeprecated = new JCommentPart();
325N/A return atDeprecated;
325N/A }
325N/A
325N/A /**
325N/A * add an xdoclet.
325N/A */
325N/A public Map<String,String> addXdoclet(String name) {
325N/A Map<String,String> p = atXdoclets.get(name);
325N/A if(p==null)
325N/A atXdoclets.put(name,p=new HashMap<String,String>());
325N/A return p;
325N/A }
325N/A
325N/A /**
325N/A * add an xdoclet.
325N/A */
325N/A public Map<String,String> addXdoclet(String name, Map<String,String> attributes) {
325N/A Map<String,String> p = atXdoclets.get(name);
325N/A if(p==null)
325N/A atXdoclets.put(name,p=new HashMap<String,String>());
325N/A p.putAll(attributes);
325N/A return p;
325N/A }
325N/A
325N/A /**
325N/A * add an xdoclet.
325N/A */
325N/A public Map<String,String> addXdoclet(String name, String attribute, String value) {
325N/A Map<String,String> p = atXdoclets.get(name);
325N/A if(p==null)
325N/A atXdoclets.put(name,p=new HashMap<String,String>());
325N/A p.put(attribute, value);
325N/A return p;
325N/A }
325N/A
325N/A public void generate(JFormatter f) {
325N/A // I realized that we can't use StringTokenizer because
325N/A // this will recognize multiple \n as one token.
325N/A
325N/A f.p("/**").nl();
325N/A
325N/A format(f," * ");
325N/A
325N/A f.p(" * ").nl();
325N/A for (Map.Entry<String,JCommentPart> e : atParams.entrySet()) {
325N/A f.p(" * @param ").p(e.getKey()).nl();
325N/A e.getValue().format(f,INDENT);
325N/A }
325N/A if( atReturn != null ) {
325N/A f.p(" * @return").nl();
325N/A atReturn.format(f,INDENT);
325N/A }
325N/A for (Map.Entry<JClass,JCommentPart> e : atThrows.entrySet()) {
325N/A f.p(" * @throws ").t(e.getKey()).nl();
325N/A e.getValue().format(f,INDENT);
325N/A }
325N/A if( atDeprecated != null ) {
325N/A f.p(" * @deprecated").nl();
325N/A atDeprecated.format(f,INDENT);
325N/A }
325N/A for (Map.Entry<String,Map<String,String>> e : atXdoclets.entrySet()) {
325N/A f.p(" * @").p(e.getKey());
325N/A if (e.getValue() != null) {
325N/A for (Map.Entry<String,String> a : e.getValue().entrySet()) {
325N/A f.p(" ").p(a.getKey()).p("= \"").p(a.getValue()).p("\"");
325N/A }
325N/A }
325N/A f.nl();
325N/A }
325N/A f.p(" */").nl();
325N/A }
325N/A
325N/A private static final String INDENT = " * ";
325N/A}