src/share/jaxws_classes/com/sun/tools/internal/xjc/addon/at_generated/PluginImpl.java

changeset 0
373ffda63c9a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/xjc/addon/at_generated/PluginImpl.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,115 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.internal.xjc.addon.at_generated;
    1.30 +
    1.31 +import java.text.SimpleDateFormat;
    1.32 +import java.util.Date;
    1.33 +
    1.34 +import com.sun.codemodel.internal.JAnnotatable;
    1.35 +import com.sun.codemodel.internal.JClass;
    1.36 +import com.sun.codemodel.internal.JFieldVar;
    1.37 +import com.sun.codemodel.internal.JMethod;
    1.38 +import com.sun.tools.internal.xjc.Driver;
    1.39 +import com.sun.tools.internal.xjc.Options;
    1.40 +import com.sun.tools.internal.xjc.Plugin;
    1.41 +import com.sun.tools.internal.xjc.outline.ClassOutline;
    1.42 +import com.sun.tools.internal.xjc.outline.EnumOutline;
    1.43 +import com.sun.tools.internal.xjc.outline.Outline;
    1.44 +
    1.45 +import org.xml.sax.ErrorHandler;
    1.46 +
    1.47 +/**
    1.48 + * {@link Plugin} that marks the generated code by using JSR-250's '@Generated'.
    1.49 + *
    1.50 + * @author Kohsuke Kawaguchi
    1.51 + */
    1.52 +public class PluginImpl extends Plugin {
    1.53 +
    1.54 +    public String getOptionName() {
    1.55 +        return "mark-generated";
    1.56 +    }
    1.57 +
    1.58 +    public String getUsage() {
    1.59 +        return "  -mark-generated    :  mark the generated code as @javax.annotation.Generated";
    1.60 +    }
    1.61 +
    1.62 +    private JClass annotation;
    1.63 +
    1.64 +    public boolean run( Outline model, Options opt, ErrorHandler errorHandler ) {
    1.65 +        // we want this to work without requiring JSR-250 jar.
    1.66 +        annotation = model.getCodeModel().ref("javax.annotation.Generated");
    1.67 +
    1.68 +        for( ClassOutline co : model.getClasses() )
    1.69 +            augument(co);
    1.70 +        for( EnumOutline eo : model.getEnums() )
    1.71 +            augument(eo);
    1.72 +
    1.73 +        //TODO: process generated ObjectFactory classes?
    1.74 +
    1.75 +        return true;
    1.76 +    }
    1.77 +
    1.78 +    private void augument(EnumOutline eo) {
    1.79 +        annotate(eo.clazz);
    1.80 +    }
    1.81 +
    1.82 +    /**
    1.83 +     * Adds "@Generated" to the classes, methods, and fields.
    1.84 +     */
    1.85 +    private void augument(ClassOutline co) {
    1.86 +        annotate(co.implClass);
    1.87 +        for (JMethod m : co.implClass.methods())
    1.88 +            annotate(m);
    1.89 +        for (JFieldVar f : co.implClass.fields().values())
    1.90 +            annotate(f);
    1.91 +    }
    1.92 +
    1.93 +    private void annotate(JAnnotatable m) {
    1.94 +        m.annotate(annotation)
    1.95 +                .param("value",Driver.class.getName())
    1.96 +                .param("date", getISO8601Date())
    1.97 +                .param("comments", "JAXB RI v" + Options.getBuildID());
    1.98 +    }
    1.99 +
   1.100 +    // cache the timestamp so that all the @Generated annotations match
   1.101 +    private String date = null;
   1.102 +
   1.103 +    /**
   1.104 +     * calculate the date value in ISO8601 format for the @Generated annotation
   1.105 +     * @return the date value
   1.106 +     */
   1.107 +    private String getISO8601Date() {
   1.108 +        if(date==null) {
   1.109 +            StringBuffer tstamp = new StringBuffer();
   1.110 +            tstamp.append((new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ")).format(new Date()));
   1.111 +            // hack to get ISO 8601 style timezone - is there a better way that doesn't require
   1.112 +            // a bunch of timezone offset calculations?
   1.113 +            tstamp.insert(tstamp.length()-2, ':');
   1.114 +            date = tstamp.toString();
   1.115 +        }
   1.116 +        return date;
   1.117 +    }
   1.118 +}

mercurial