1 /* 2 * Copyright (c) 1997, 2012, 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.xjc.addon.at_generated; 27 28 import java.text.SimpleDateFormat; 29 import java.util.Date; 30 31 import com.sun.codemodel.internal.JAnnotatable; 32 import com.sun.codemodel.internal.JClass; 33 import com.sun.codemodel.internal.JFieldVar; 34 import com.sun.codemodel.internal.JMethod; 35 import com.sun.tools.internal.xjc.Driver; 36 import com.sun.tools.internal.xjc.Options; 37 import com.sun.tools.internal.xjc.Plugin; 38 import com.sun.tools.internal.xjc.outline.ClassOutline; 39 import com.sun.tools.internal.xjc.outline.EnumOutline; 40 import com.sun.tools.internal.xjc.outline.Outline; 41 42 import org.xml.sax.ErrorHandler; 43 44 /** 45 * {@link Plugin} that marks the generated code by using JSR-250's '@Generated'. 46 * 47 * @author Kohsuke Kawaguchi 48 */ 49 public class PluginImpl extends Plugin { 50 51 public String getOptionName() { 52 return "mark-generated"; 53 } 54 55 public String getUsage() { 56 return " -mark-generated : mark the generated code as @javax.annotation.Generated"; 57 } 58 59 private JClass annotation; 60 61 public boolean run( Outline model, Options opt, ErrorHandler errorHandler ) { 62 // we want this to work without requiring JSR-250 jar. 63 annotation = model.getCodeModel().ref("javax.annotation.Generated"); 64 65 for( ClassOutline co : model.getClasses() ) 66 augument(co); 67 for( EnumOutline eo : model.getEnums() ) 68 augument(eo); 69 70 //TODO: process generated ObjectFactory classes? 71 72 return true; 73 } 74 75 private void augument(EnumOutline eo) { 76 annotate(eo.clazz); 77 } 78 79 /** 80 * Adds "@Generated" to the classes, methods, and fields. 81 */ 82 private void augument(ClassOutline co) { 83 annotate(co.implClass); 84 for (JMethod m : co.implClass.methods()) 85 annotate(m); 86 for (JFieldVar f : co.implClass.fields().values()) 87 annotate(f); 88 } 89 90 private void annotate(JAnnotatable m) { 91 m.annotate(annotation) 92 .param("value",Driver.class.getName()) 93 .param("date", getISO8601Date()) 94 .param("comments", "JAXB RI v" + Options.getBuildID()); 95 } 96 97 // cache the timestamp so that all the @Generated annotations match 98 private String date = null; 99 100 /** 101 * calculate the date value in ISO8601 format for the @Generated annotation 102 * @return the date value 103 */ 104 private String getISO8601Date() { 105 if(date==null) { 106 StringBuffer tstamp = new StringBuffer(); 107 tstamp.append((new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ")).format(new Date())); 108 // hack to get ISO 8601 style timezone - is there a better way that doesn't require 109 // a bunch of timezone offset calculations? 110 tstamp.insert(tstamp.length()-2, ':'); 111 date = tstamp.toString(); 112 } 113 return date; 114 } 115 }