author | mkos |
Sun, 30 Dec 2012 00:00:00 +0100 | |
changeset 22678 | ac1ea46be942 |
parent 12009 | 4abb694f273a |
permissions | -rw-r--r-- |
12009 | 1 |
/* |
22678
ac1ea46be942
8029237: Update copyright year to match last edit in jaxws repository for 2012
mkos
parents:
12009
diff
changeset
|
2 |
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
12009 | 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.writer; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.io.Writer; |
|
30 |
import java.util.Collection; |
|
31 |
import java.util.Comparator; |
|
32 |
import java.util.HashMap; |
|
33 |
import java.util.Iterator; |
|
34 |
import java.util.Map; |
|
35 |
import java.util.Set; |
|
36 |
import java.util.TreeSet; |
|
37 |
||
38 |
import com.sun.codemodel.internal.JClass; |
|
39 |
import com.sun.codemodel.internal.JClassContainer; |
|
40 |
import com.sun.codemodel.internal.JDefinedClass; |
|
41 |
import com.sun.codemodel.internal.JPackage; |
|
42 |
import com.sun.codemodel.internal.JType; |
|
43 |
import com.sun.tools.internal.xjc.outline.ClassOutline; |
|
44 |
import com.sun.tools.internal.xjc.outline.FieldOutline; |
|
45 |
import com.sun.tools.internal.xjc.outline.Outline; |
|
46 |
||
47 |
/** |
|
48 |
* Dumps an annotated grammar in a simple format that |
|
49 |
* makes signature check easy. |
|
50 |
* |
|
51 |
* @author |
|
52 |
* Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) |
|
53 |
*/ |
|
54 |
public class SignatureWriter { |
|
55 |
||
56 |
public static void write( Outline model, Writer out ) |
|
57 |
throws IOException { |
|
58 |
||
59 |
new SignatureWriter(model,out).dump(); |
|
60 |
} |
|
61 |
||
62 |
private SignatureWriter( Outline model, Writer out ) { |
|
63 |
this.out = out; |
|
64 |
this.classes = model.getClasses(); |
|
65 |
||
66 |
for( ClassOutline ci : classes ) |
|
67 |
classSet.put( ci.ref, ci ); |
|
68 |
} |
|
69 |
||
70 |
/** All the ClassItems in this grammar. */ |
|
71 |
private final Collection<? extends ClassOutline> classes; |
|
72 |
/** Map from content interfaces to ClassItem. */ |
|
73 |
private final Map<JDefinedClass,ClassOutline> classSet = new HashMap<JDefinedClass,ClassOutline>(); |
|
74 |
||
75 |
private final Writer out; |
|
76 |
private int indent=0; |
|
77 |
private void printIndent() throws IOException { |
|
78 |
for( int i=0; i<indent; i++ ) |
|
79 |
out.write(" "); |
|
80 |
} |
|
81 |
private void println(String s) throws IOException { |
|
82 |
printIndent(); |
|
83 |
out.write(s); |
|
84 |
out.write('\n'); |
|
85 |
} |
|
86 |
||
87 |
private void dump() throws IOException { |
|
88 |
||
89 |
// collect packages used in the class. |
|
90 |
Set<JPackage> packages = new TreeSet<JPackage>(new Comparator<JPackage>() { |
|
91 |
public int compare(JPackage lhs, JPackage rhs) { |
|
92 |
return lhs.name().compareTo(rhs.name()); |
|
93 |
} |
|
94 |
}); |
|
95 |
for( ClassOutline ci : classes ) |
|
96 |
packages.add(ci._package()._package()); |
|
97 |
||
98 |
for( JPackage pkg : packages ) |
|
99 |
dump( pkg ); |
|
100 |
||
101 |
out.flush(); |
|
102 |
} |
|
103 |
||
104 |
private void dump( JPackage pkg ) throws IOException { |
|
105 |
println("package "+pkg.name()+" {"); |
|
106 |
indent++; |
|
107 |
dumpChildren(pkg); |
|
108 |
indent--; |
|
109 |
println("}"); |
|
110 |
} |
|
111 |
||
112 |
private void dumpChildren( JClassContainer cont ) throws IOException { |
|
113 |
Iterator itr = cont.classes(); |
|
114 |
while(itr.hasNext()) { |
|
115 |
JDefinedClass cls = (JDefinedClass)itr.next(); |
|
116 |
ClassOutline ci = classSet.get(cls); |
|
117 |
if(ci!=null) |
|
118 |
dump(ci); |
|
119 |
} |
|
120 |
} |
|
121 |
||
122 |
private void dump( ClassOutline ci ) throws IOException { |
|
123 |
JDefinedClass cls = ci.implClass; |
|
124 |
||
125 |
StringBuilder buf = new StringBuilder(); |
|
126 |
buf.append("interface "); |
|
127 |
buf.append(cls.name()); |
|
128 |
||
129 |
boolean first=true; |
|
130 |
Iterator itr = cls._implements(); |
|
131 |
while(itr.hasNext()) { |
|
132 |
if(first) { |
|
133 |
buf.append(" extends "); |
|
134 |
first=false; |
|
135 |
} else { |
|
136 |
buf.append(", "); |
|
137 |
} |
|
138 |
buf.append( printName((JClass)itr.next()) ); |
|
139 |
} |
|
140 |
buf.append(" {"); |
|
141 |
println(buf.toString()); |
|
142 |
indent++; |
|
143 |
||
144 |
// dump the field |
|
145 |
for( FieldOutline fo : ci.getDeclaredFields() ) { |
|
146 |
String type = printName(fo.getRawType()); |
|
147 |
println(type+' '+fo.getPropertyInfo().getName(true)+';'); |
|
148 |
} |
|
149 |
||
150 |
dumpChildren(cls); |
|
151 |
||
152 |
indent--; |
|
153 |
println("}"); |
|
154 |
} |
|
155 |
||
156 |
/** Get the display name of a type. */ |
|
157 |
private String printName( JType t ) { |
|
158 |
String name = t.fullName(); |
|
159 |
if(name.startsWith("java.lang.")) |
|
160 |
name = name.substring(10); // chop off the package name |
|
161 |
return name; |
|
162 |
} |
|
163 |
} |