1 /*
   2  * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.tools.internal.ws.wscompile.plugin.at_generated;
  27 
  28 import com.sun.codemodel.internal.*;
  29 import com.sun.tools.internal.ws.ToolVersion;
  30 import com.sun.tools.internal.ws.processor.model.Model;
  31 import com.sun.tools.internal.ws.wscompile.ErrorReceiver;
  32 import com.sun.tools.internal.ws.wscompile.Plugin;
  33 import com.sun.tools.internal.ws.wscompile.WsimportOptions;
  34 import com.sun.tools.internal.ws.wscompile.WsimportTool;
  35 import java.text.SimpleDateFormat;
  36 import java.util.Date;
  37 import java.util.Iterator;
  38 import org.xml.sax.SAXException;
  39 
  40 /**
  41  * {@link Plugin} that marks the generated code by using JSR-250's '@Generated'.
  42  * It is based on a similar plugin in JAXB RI.
  43  *
  44  * @author Lukas Jungmann
  45  * @since 2.2.6
  46  */
  47 public final class PluginImpl extends Plugin {
  48 
  49     private JClass annotation;
  50 
  51     // cache the timestamp so that all the @Generated annotations match
  52     private String date = null;
  53 
  54     @Override
  55     public String getOptionName() {
  56         return "mark-generated";
  57     }
  58 
  59     @Override
  60     public String getUsage() {
  61         return "  -mark-generated    :  mark the generated code as @javax.annotation.Generated";
  62     }
  63 
  64     @Override
  65     public boolean run(Model model, WsimportOptions wo, ErrorReceiver er) throws SAXException {
  66         JCodeModel cm = wo.getCodeModel();
  67         // we want this to work without requiring JSR-250 jar.
  68         annotation = cm.ref("javax.annotation.Generated");
  69 
  70         for (Iterator<JPackage> i = cm.packages(); i.hasNext();) {
  71             for (Iterator<JDefinedClass> j = i.next().classes(); j.hasNext();) {
  72                 annotate(j.next());
  73             }
  74         }
  75 
  76         return true;
  77     }
  78 
  79     private void annotate(JAnnotatable m) {
  80         m.annotate(annotation)
  81                 .param("value", WsimportTool.class.getName())
  82                 .param("date", getISO8601Date())
  83                 .param("comments", ToolVersion.VERSION.BUILD_VERSION);
  84     }
  85 
  86     /**
  87      * calculate the date value in ISO8601 format for the @Generated annotation
  88      * @return the date value
  89      */
  90     private String getISO8601Date() {
  91         if(date==null) {
  92             StringBuilder tstamp = new StringBuilder();
  93             tstamp.append((new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ")).format(new Date()));
  94             // hack to get ISO 8601 style timezone - is there a better way that doesn't require
  95             // a bunch of timezone offset calculations?
  96             tstamp.insert(tstamp.length()-2, ':');
  97             date = tstamp.toString();
  98         }
  99         return date;
 100     }
 101 }