aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.bind.v2.model.impl; aoqi@0: aoqi@0: import java.util.LinkedHashSet; aoqi@0: import java.util.Set; aoqi@0: aoqi@0: import javax.xml.bind.annotation.XmlElementDecl; aoqi@0: aoqi@0: import com.sun.xml.internal.bind.v2.model.annotation.Locatable; aoqi@0: import com.sun.xml.internal.bind.v2.model.annotation.MethodLocatable; aoqi@0: import com.sun.xml.internal.bind.v2.model.core.RegistryInfo; aoqi@0: import com.sun.xml.internal.bind.v2.model.core.TypeInfo; aoqi@0: import com.sun.xml.internal.bind.v2.model.nav.Navigator; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationException; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.Location; aoqi@0: import com.sun.xml.internal.bind.v2.ContextFactory; aoqi@0: aoqi@0: /** aoqi@0: * Implementation of {@link RegistryInfo}. aoqi@0: * aoqi@0: * @author Kohsuke Kawaguchi aoqi@0: */ aoqi@0: // experimenting with shorter type parameters for quadruple. aoqi@0: // the idea is that they show so often that you'd understand the meaning aoqi@0: // without relying on the whole name. aoqi@0: final class RegistryInfoImpl implements Locatable, RegistryInfo { aoqi@0: aoqi@0: final C registryClass; aoqi@0: private final Locatable upstream; aoqi@0: private final Navigator nav; aoqi@0: aoqi@0: /** aoqi@0: * Types that are referenced from this registry. aoqi@0: */ aoqi@0: private final Set> references = new LinkedHashSet>(); aoqi@0: aoqi@0: /** aoqi@0: * Picks up references in this registry to other types. aoqi@0: */ aoqi@0: RegistryInfoImpl(ModelBuilder builder, Locatable upstream, C registryClass) { aoqi@0: this.nav = builder.nav; aoqi@0: this.registryClass = registryClass; aoqi@0: this.upstream = upstream; aoqi@0: builder.registries.put(getPackageName(),this); aoqi@0: aoqi@0: if(nav.getDeclaredField(registryClass,ContextFactory.USE_JAXB_PROPERTIES)!=null) { aoqi@0: // the user is trying to use ObjectFactory that we generate for interfaces, aoqi@0: // that means he's missing jaxb.properties aoqi@0: builder.reportError(new IllegalAnnotationException( aoqi@0: Messages.MISSING_JAXB_PROPERTIES.format(getPackageName()), aoqi@0: this aoqi@0: )); aoqi@0: // looking at members will only add more errors, so just abort now aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: for( M m : nav.getDeclaredMethods(registryClass) ) { aoqi@0: XmlElementDecl em = builder.reader.getMethodAnnotation( aoqi@0: XmlElementDecl.class, m, this ); aoqi@0: aoqi@0: if(em==null) { aoqi@0: if(nav.getMethodName(m).startsWith("create")) { aoqi@0: // this is a factory method. visit this class aoqi@0: references.add( aoqi@0: builder.getTypeInfo(nav.getReturnType(m), aoqi@0: new MethodLocatable(this,m,nav))); aoqi@0: } aoqi@0: aoqi@0: continue; aoqi@0: } aoqi@0: aoqi@0: ElementInfoImpl ei; aoqi@0: try { aoqi@0: ei = builder.createElementInfo(this,m); aoqi@0: } catch (IllegalAnnotationException e) { aoqi@0: builder.reportError(e); aoqi@0: continue; // recover by ignoring this element aoqi@0: } aoqi@0: aoqi@0: // register this mapping aoqi@0: // TODO: any chance this could cause a stack overflow (by recursively visiting classes)? aoqi@0: builder.typeInfoSet.add(ei,builder); aoqi@0: references.add(ei); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public Locatable getUpstream() { aoqi@0: return upstream; aoqi@0: } aoqi@0: aoqi@0: public Location getLocation() { aoqi@0: return nav.getClassLocation(registryClass); aoqi@0: } aoqi@0: aoqi@0: public Set> getReferences() { aoqi@0: return references; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Gets the name of the package that this registry governs. aoqi@0: */ aoqi@0: public String getPackageName() { aoqi@0: return nav.getPackageName(registryClass); aoqi@0: } aoqi@0: aoqi@0: public C getClazz() { aoqi@0: return registryClass; aoqi@0: } aoqi@0: }