325N/A/*
325N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.tools.internal.xjc.util;
325N/A
325N/Aimport com.sun.codemodel.internal.ClassType;
325N/Aimport com.sun.codemodel.internal.JClassAlreadyExistsException;
325N/Aimport com.sun.codemodel.internal.JClassContainer;
325N/Aimport com.sun.codemodel.internal.JDefinedClass;
325N/Aimport com.sun.codemodel.internal.JJavaName;
325N/Aimport com.sun.codemodel.internal.JMod;
325N/Aimport com.sun.tools.internal.xjc.ErrorReceiver;
325N/A
325N/Aimport org.xml.sax.Locator;
325N/Aimport org.xml.sax.SAXParseException;
325N/A
325N/A/**
325N/A * Create new {@link JDefinedClass} and report class collision errors,
325N/A * if necessary.
325N/A *
325N/A * This is just a helper class that simplifies the class name collision
325N/A * detection. This object maintains no state, so it is OK to use
325N/A * multiple instances of this.
325N/A *
325N/A * @author
325N/A * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
325N/A */
325N/Apublic final class CodeModelClassFactory {
325N/A
325N/A /** errors are reported to this object. */
325N/A private ErrorReceiver errorReceiver;
325N/A
325N/A /** unique id generator. */
325N/A private int ticketMaster = 0;
325N/A
325N/A
325N/A public CodeModelClassFactory( ErrorReceiver _errorReceiver ) {
325N/A this.errorReceiver = _errorReceiver;
325N/A }
325N/A
325N/A public JDefinedClass createClass( JClassContainer parent, String name, Locator source ) {
325N/A return createClass( parent, JMod.PUBLIC, name, source );
325N/A }
325N/A public JDefinedClass createClass( JClassContainer parent, int mod, String name, Locator source ) {
325N/A return createClass(parent,mod,name,source,ClassType.CLASS);
325N/A }
325N/A
325N/A public JDefinedClass createInterface( JClassContainer parent, String name, Locator source ) {
325N/A return createInterface( parent, JMod.PUBLIC, name, source );
325N/A }
325N/A public JDefinedClass createInterface( JClassContainer parent, int mod, String name, Locator source ) {
325N/A return createClass(parent,mod,name,source,ClassType.INTERFACE);
325N/A }
325N/A public JDefinedClass createClass(
325N/A JClassContainer parent, String name, Locator source, ClassType kind ) {
325N/A return createClass(parent,JMod.PUBLIC,name,source,kind);
325N/A }
325N/A public JDefinedClass createClass(
325N/A JClassContainer parent, int mod, String name, Locator source, ClassType kind ) {
325N/A
325N/A if(!JJavaName.isJavaIdentifier(name)) {
325N/A // report the error
325N/A errorReceiver.error( new SAXParseException(
325N/A Messages.format( Messages.ERR_INVALID_CLASSNAME, name ), source ));
325N/A return createDummyClass(parent);
325N/A }
325N/A
325N/A
325N/A try {
325N/A if(parent.isClass() && kind==ClassType.CLASS)
325N/A mod |= JMod.STATIC;
325N/A
325N/A JDefinedClass r = parent._class(mod,name,kind);
325N/A // use the metadata field to store the source location,
325N/A // so that we can report class name collision errors.
325N/A r.metadata = source;
325N/A
325N/A return r;
325N/A } catch( JClassAlreadyExistsException e ) {
325N/A // class collision.
325N/A JDefinedClass cls = e.getExistingClass();
325N/A
325N/A // report the error
325N/A errorReceiver.error( new SAXParseException(
325N/A Messages.format( Messages.ERR_CLASSNAME_COLLISION, cls.fullName() ),
325N/A (Locator)cls.metadata ));
325N/A errorReceiver.error( new SAXParseException(
325N/A Messages.format( Messages.ERR_CLASSNAME_COLLISION_SOURCE, name ),
325N/A source ));
325N/A
325N/A if( !name.equals(cls.name()) ) {
325N/A // on Windows, FooBar and Foobar causes name collision
325N/A errorReceiver.error( new SAXParseException(
325N/A Messages.format( Messages.ERR_CASE_SENSITIVITY_COLLISION,
325N/A name, cls.name() ), null ) );
325N/A }
325N/A
325N/A if(Util.equals((Locator)cls.metadata,source)) {
325N/A errorReceiver.error( new SAXParseException(
325N/A Messages.format( Messages.ERR_CHAMELEON_SCHEMA_GONE_WILD ),
325N/A source ));
325N/A }
325N/A
325N/A return createDummyClass(parent);
325N/A }
325N/A }
325N/A
325N/A /**
325N/A * Create a dummy class to recover from an error.
325N/A *
325N/A * We won't generate the code, so the client will never see this class
325N/A * getting generated.
325N/A */
325N/A private JDefinedClass createDummyClass(JClassContainer parent) {
325N/A try {
325N/A return parent._class("$$$garbage$$$"+(ticketMaster++));
325N/A } catch( JClassAlreadyExistsException ee ) {
325N/A return ee.getExistingClass();
325N/A }
325N/A }
325N/A}