01: /*
02: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
03: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
04: *
05: * This code is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU General Public License version 2 only, as
07: * published by the Free Software Foundation. Sun designates this
08: * particular file as subject to the "Classpath" exception as provided
09: * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22: * CA 95054 USA or visit www.sun.com if you need additional information or
23: * have any questions.
24: */
25:
26: package com.sun.tools.internal.xjc.reader;
27:
28: import java.util.List;
29:
30: import com.sun.tools.internal.xjc.ErrorReceiver;
31: import com.sun.tools.internal.xjc.model.CClassInfo;
32: import com.sun.tools.internal.xjc.model.CPropertyInfo;
33: import com.sun.tools.internal.xjc.model.Model;
34:
35: /**
36: * Checks errors on model classes.
37: *
38: * <p>
39: * This should be used as a {@link Ring} component.
40: *
41: * @author Kohsuke Kawaguchi
42: */
43: public final class ModelChecker {
44: private final Model model = Ring.get(Model.class);
45: private final ErrorReceiver errorReceiver = Ring
46: .get(ErrorReceiver.class);
47:
48: public ModelChecker() {
49: }
50:
51: public void check() {
52: for (CClassInfo ci : model.beans().values())
53: check(ci);
54: }
55:
56: private void check(CClassInfo ci) {
57: List<CPropertyInfo> props = ci.getProperties();
58:
59: OUTER: for (int i = 0; i < props.size(); i++) {
60: CPropertyInfo p1 = props.get(i);
61:
62: if (p1.getName(true).equals("Class")) {
63: errorReceiver.error(p1.locator,
64: Messages.PROPERTY_CLASS_IS_RESERVED.format());
65: continue;
66: }
67:
68: for (int j = i + 1; j < props.size(); j++) {
69: if (checkPropertyCollision(p1, props.get(j)))
70: continue OUTER;
71: }
72: for (CClassInfo c = ci.getBaseClass(); c != null; c = c
73: .getBaseClass()) {
74: for (CPropertyInfo p2 : c.getProperties())
75: if (checkPropertyCollision(p1, p2))
76: continue OUTER;
77: }
78: }
79: }
80:
81: private boolean checkPropertyCollision(CPropertyInfo p1,
82: CPropertyInfo p2) {
83: if (!p1.getName(true).equals(p2.getName(true)))
84: return false;
85: errorReceiver.error(p1.locator, Messages.DUPLICATE_PROPERTY
86: .format(p1.getName(true)));
87: errorReceiver.error(p2.locator, Messages.ERR_RELEVANT_LOCATION
88: .format());
89: return true;
90: }
91: }
|