|
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.locator; |
|
27 |
|
28 import java.io.IOException; |
|
29 |
|
30 import javax.xml.bind.annotation.XmlTransient; |
|
31 |
|
32 import com.sun.codemodel.internal.JDefinedClass; |
|
33 import com.sun.codemodel.internal.JMod; |
|
34 import com.sun.codemodel.internal.JVar; |
|
35 import com.sun.codemodel.internal.JMethod; |
|
36 import com.sun.tools.internal.xjc.BadCommandLineException; |
|
37 import com.sun.tools.internal.xjc.Options; |
|
38 import com.sun.tools.internal.xjc.Plugin; |
|
39 import com.sun.tools.internal.xjc.outline.ClassOutline; |
|
40 import com.sun.tools.internal.xjc.outline.Outline; |
|
41 import com.sun.xml.internal.bind.Locatable; |
|
42 import com.sun.xml.internal.bind.annotation.XmlLocation; |
|
43 |
|
44 import org.xml.sax.ErrorHandler; |
|
45 import org.xml.sax.Locator; |
|
46 |
|
47 /** |
|
48 * Generates JAXB objects that implement {@link Locatable}. |
|
49 * |
|
50 * @author |
|
51 * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) |
|
52 */ |
|
53 public class SourceLocationAddOn extends Plugin { |
|
54 |
|
55 public String getOptionName() { |
|
56 return "Xlocator"; |
|
57 } |
|
58 |
|
59 public String getUsage() { |
|
60 return " -Xlocator : enable source location support for generated code"; |
|
61 } |
|
62 |
|
63 public int parseArgument(Options opt, String[] args, int i) throws BadCommandLineException, IOException { |
|
64 return 0; // no option recognized |
|
65 } |
|
66 |
|
67 private static final String fieldName = "locator"; |
|
68 |
|
69 public boolean run( |
|
70 Outline outline, |
|
71 Options opt, |
|
72 ErrorHandler errorHandler ) { |
|
73 |
|
74 for( ClassOutline ci : outline.getClasses() ) { |
|
75 JDefinedClass impl = ci.implClass; |
|
76 if (ci.getSuperClass() == null) { |
|
77 JVar $loc = impl.field(JMod.PROTECTED, Locator.class, fieldName); |
|
78 $loc.annotate(XmlLocation.class); |
|
79 $loc.annotate(XmlTransient.class); |
|
80 |
|
81 impl._implements(Locatable.class); |
|
82 |
|
83 impl.method(JMod.PUBLIC, Locator.class, "sourceLocation").body()._return($loc); |
|
84 |
|
85 JMethod setter = impl.method(JMod.PUBLIC, Void.TYPE, "setSourceLocation"); |
|
86 JVar $newLoc = setter.param(Locator.class, "newLocator"); |
|
87 setter.body().assign($loc, $newLoc); |
|
88 } |
|
89 } |
|
90 |
|
91 return true; |
|
92 } |
|
93 } |