src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/IllegalAnnotationException.java

changeset 0
373ffda63c9a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/IllegalAnnotationException.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,187 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.bind.v2.runtime;
    1.30 +
    1.31 +import java.lang.annotation.Annotation;
    1.32 +import java.util.ArrayList;
    1.33 +import java.util.Collections;
    1.34 +import java.util.List;
    1.35 +
    1.36 +import javax.xml.bind.JAXBContext;
    1.37 +import javax.xml.bind.JAXBException;
    1.38 +
    1.39 +import com.sun.xml.internal.bind.v2.model.annotation.Locatable;
    1.40 +
    1.41 +/**
    1.42 + * Signals an incorrect use of JAXB annotations.
    1.43 + *
    1.44 + * @author Kohsuke Kawaguchi (kk@kohsuke.org)
    1.45 + * @since JAXB 2.0 EA1
    1.46 + */
    1.47 +public class IllegalAnnotationException extends JAXBException {
    1.48 +
    1.49 +    /**
    1.50 +     * Read-only list of {@link Location}s.
    1.51 +     */
    1.52 +    private final List<List<Location>> pos;
    1.53 +
    1.54 +    private static final long serialVersionUID = 1L;
    1.55 +
    1.56 +    public IllegalAnnotationException(String message, Locatable src) {
    1.57 +        super(message);
    1.58 +        pos = build(src);
    1.59 +    }
    1.60 +
    1.61 +    public IllegalAnnotationException(String message, Annotation src) {
    1.62 +        this(message,cast(src));
    1.63 +    }
    1.64 +
    1.65 +    public IllegalAnnotationException(String message, Locatable src1, Locatable src2) {
    1.66 +        super(message);
    1.67 +        pos = build(src1,src2);
    1.68 +    }
    1.69 +
    1.70 +    public IllegalAnnotationException(String message, Annotation src1, Annotation src2) {
    1.71 +        this(message,cast(src1),cast(src2));
    1.72 +    }
    1.73 +
    1.74 +    public IllegalAnnotationException(String message, Annotation src1, Locatable src2) {
    1.75 +        this(message,cast(src1),src2);
    1.76 +    }
    1.77 +
    1.78 +    public IllegalAnnotationException(String message, Throwable cause, Locatable src) {
    1.79 +        super(message, cause);
    1.80 +        pos = build(src);
    1.81 +    }
    1.82 +
    1.83 +    private static Locatable cast(Annotation a) {
    1.84 +        if(a instanceof Locatable)
    1.85 +            return (Locatable)a;
    1.86 +        else
    1.87 +            return null;
    1.88 +    }
    1.89 +
    1.90 +    private List<List<Location>> build(Locatable... srcs) {
    1.91 +        List<List<Location>> r = new ArrayList<List<Location>>();
    1.92 +        for( Locatable l : srcs ) {
    1.93 +            if(l!=null) {
    1.94 +                List<Location> ll = convert(l);
    1.95 +                if(ll!=null && !ll.isEmpty())
    1.96 +                    r.add(ll);
    1.97 +            }
    1.98 +        }
    1.99 +        return Collections.unmodifiableList(r);
   1.100 +    }
   1.101 +
   1.102 +    /**
   1.103 +     * Builds a list of {@link Location}s out of a {@link Locatable}.
   1.104 +     */
   1.105 +    private List<Location> convert(Locatable src) {
   1.106 +        if(src==null)   return null;
   1.107 +
   1.108 +        List<Location> r = new ArrayList<Location>();
   1.109 +        for( ; src!=null; src=src.getUpstream())
   1.110 +            r.add(src.getLocation());
   1.111 +        return Collections.unmodifiableList(r);
   1.112 +    }
   1.113 +
   1.114 +
   1.115 +
   1.116 +    /**
   1.117 +     * Returns a read-only list of {@link Location} that indicates
   1.118 +     * where in the source code the problem has happened.
   1.119 +     *
   1.120 +     * <p>
   1.121 +     * Normally, an annotation error happens on one particular
   1.122 +     * annotation, in which case this method returns a list that
   1.123 +     * contains another list, which in turn contains the location
   1.124 +     * information that leads to the error location
   1.125 +     * (IOW, <tt>[ [pos1,pos2,...,posN] ]</tt>)
   1.126 +     *
   1.127 +     * <p>
   1.128 +     * Sometimes, an error could occur because of two or more conflicting
   1.129 +     * annotations, in which case this method returns a list
   1.130 +     * that contains many lists, where each list contains
   1.131 +     * the location information that leads to each of the conflicting
   1.132 +     * annotations
   1.133 +     * (IOW, <tt>[ [pos11,pos12,...,pos1N],[pos21,pos22,...,pos2M], ... ]</tt>)
   1.134 +     *
   1.135 +     * <p>
   1.136 +     * Yet some other time, the runtime can fail to provide any
   1.137 +     * error location, in which case this method returns an empty list.
   1.138 +     * (IOW, <tt>[]</tt>). We do try hard to make sure this won't happen,
   1.139 +     * so please <a href="http://jaxb.dev.java.net/">let us know</a>
   1.140 +     * if you see this behavior.
   1.141 +     *
   1.142 +     *
   1.143 +     * <h3>List of {@link Location}</h3>
   1.144 +     * <p>
   1.145 +     * Each error location is identified not just by one {@link Location}
   1.146 +     * object, but by a sequence of {@link Location}s that shows why
   1.147 +     * the runtime is led to the place of the error.
   1.148 +     * This list is sorted such that the most specific {@link Location} comes
   1.149 +     * to the first in the list, sort of like a stack trace.
   1.150 +     *
   1.151 +     * <p>
   1.152 +     * For example, suppose you specify class <tt>Foo</tt> to {@link JAXBContext},
   1.153 +     * <tt>Foo</tt> derives from <tt>Bar</tt>, <tt>Bar</tt> has a field <tt>pea</tt>
   1.154 +     * that points to <tt>Zot</tt>, <tt>Zot</tt> contains a <tt>gum</tt>
   1.155 +     * property, and this property has an errornous annotation.
   1.156 +     * Then when this exception is thrown, the list of {@link Location}s
   1.157 +     * will look something like
   1.158 +     * <tt>[ "gum property", "Zot class", "pea property", "Bar class", "Foo class" ]</tt>
   1.159 +     *
   1.160 +     *
   1.161 +     * @return
   1.162 +     *      can be empty when no source position is available,
   1.163 +     *      but never null. The returned list will never contain
   1.164 +     *      null nor length-0 {@link List}.
   1.165 +     */
   1.166 +    public List<List<Location>> getSourcePos() {
   1.167 +        return pos;
   1.168 +    }
   1.169 +
   1.170 +    /**
   1.171 +     * Returns the exception name, message, and related information
   1.172 +     * together in one string.
   1.173 +     *
   1.174 +     * <p>
   1.175 +     * Overriding this method (instead of {@link #printStackTrace} allows
   1.176 +     * this crucial detail to show up even when this exception is nested
   1.177 +     * inside other exceptions.
   1.178 +     */
   1.179 +    public String toString() {
   1.180 +        StringBuilder sb = new StringBuilder(getMessage());
   1.181 +
   1.182 +        for( List<Location> locs : pos ) {
   1.183 +            sb.append("\n\tthis problem is related to the following location:");
   1.184 +            for( Location loc : locs )
   1.185 +                sb.append("\n\t\tat ").append(loc.toString());
   1.186 +        }
   1.187 +
   1.188 +        return sb.toString();
   1.189 +    }
   1.190 +}

mercurial