1 /*
   2  * Copyright (c) 1997, 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.xjc.addon.accessors;
  27 
  28 import com.sun.codemodel.internal.JAnnotationUse;
  29 import com.sun.codemodel.internal.JClass;
  30 import java.io.IOException;
  31 import com.sun.tools.internal.xjc.BadCommandLineException;
  32 import com.sun.tools.internal.xjc.Options;
  33 import com.sun.tools.internal.xjc.Plugin;
  34 import com.sun.tools.internal.xjc.outline.ClassOutline;
  35 import com.sun.tools.internal.xjc.outline.Outline;
  36 import java.lang.reflect.Field;
  37 import java.util.Iterator;
  38 import java.util.logging.Level;
  39 import java.util.logging.Logger;
  40 import javax.xml.bind.annotation.XmlAccessType;
  41 import javax.xml.bind.annotation.XmlAccessorType;
  42 import org.xml.sax.ErrorHandler;
  43 
  44 /**
  45  * Generates synchronized methods.
  46  *
  47  * @author
  48  *     Martin Grebac (martin.grebac@sun.com)
  49  */
  50 public class PluginImpl extends Plugin {
  51 
  52     public String getOptionName() {
  53         return "Xpropertyaccessors";
  54     }
  55 
  56     public String getUsage() {
  57         return "  -Xpropertyaccessors :  Use XmlAccessType PROPERTY instead of FIELD for generated classes";
  58     }
  59 
  60     @Override
  61     public int parseArgument(Options opt, String[] args, int i) throws BadCommandLineException, IOException {
  62         return 0;   // no option recognized
  63     }
  64 
  65     public boolean run( Outline model, Options opt, ErrorHandler errorHandler ) {
  66 
  67         for( ClassOutline co : model.getClasses() ) {
  68             Iterator<JAnnotationUse> ann = co.ref.annotations().iterator();
  69             while (ann.hasNext()) {
  70                try {
  71                     JAnnotationUse a = ann.next();
  72                     Field clazzField = a.getClass().getDeclaredField("clazz");
  73                     clazzField.setAccessible(true);
  74                     JClass cl = (JClass) clazzField.get(a);
  75                     if (cl.equals(model.getCodeModel()._ref(XmlAccessorType.class))) {
  76                         a.param("value", XmlAccessType.PROPERTY);
  77                         break;
  78                     }
  79                 } catch (IllegalArgumentException ex) {
  80                     Logger.getLogger(PluginImpl.class.getName()).log(Level.SEVERE, null, ex);
  81                 } catch (IllegalAccessException ex) {
  82                     Logger.getLogger(PluginImpl.class.getName()).log(Level.SEVERE, null, ex);
  83                 } catch (NoSuchFieldException ex) {
  84                     Logger.getLogger(PluginImpl.class.getName()).log(Level.SEVERE, null, ex);
  85                 } catch (SecurityException ex) {
  86                     Logger.getLogger(PluginImpl.class.getName()).log(Level.SEVERE, null, ex);
  87                 }
  88             }
  89         }
  90         return true;
  91     }
  92 
  93 }