Fri, 24 Oct 2014 15:02:28 +0200
8054367: More references for endpoints
Summary: fix also reviewed by Iaroslav.Savytskyi@oracle.com, Alexander.Fomin@oracle.com
Reviewed-by: mullan, skoivu
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.xml.internal.bind.v2.runtime; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.lang.annotation.Annotation; |
aoqi@0 | 29 | import java.util.ArrayList; |
aoqi@0 | 30 | import java.util.Collections; |
aoqi@0 | 31 | import java.util.List; |
aoqi@0 | 32 | |
aoqi@0 | 33 | import javax.xml.bind.JAXBContext; |
aoqi@0 | 34 | import javax.xml.bind.JAXBException; |
aoqi@0 | 35 | |
aoqi@0 | 36 | import com.sun.xml.internal.bind.v2.model.annotation.Locatable; |
aoqi@0 | 37 | |
aoqi@0 | 38 | /** |
aoqi@0 | 39 | * Signals an incorrect use of JAXB annotations. |
aoqi@0 | 40 | * |
aoqi@0 | 41 | * @author Kohsuke Kawaguchi (kk@kohsuke.org) |
aoqi@0 | 42 | * @since JAXB 2.0 EA1 |
aoqi@0 | 43 | */ |
aoqi@0 | 44 | public class IllegalAnnotationException extends JAXBException { |
aoqi@0 | 45 | |
aoqi@0 | 46 | /** |
aoqi@0 | 47 | * Read-only list of {@link Location}s. |
aoqi@0 | 48 | */ |
aoqi@0 | 49 | private final List<List<Location>> pos; |
aoqi@0 | 50 | |
aoqi@0 | 51 | private static final long serialVersionUID = 1L; |
aoqi@0 | 52 | |
aoqi@0 | 53 | public IllegalAnnotationException(String message, Locatable src) { |
aoqi@0 | 54 | super(message); |
aoqi@0 | 55 | pos = build(src); |
aoqi@0 | 56 | } |
aoqi@0 | 57 | |
aoqi@0 | 58 | public IllegalAnnotationException(String message, Annotation src) { |
aoqi@0 | 59 | this(message,cast(src)); |
aoqi@0 | 60 | } |
aoqi@0 | 61 | |
aoqi@0 | 62 | public IllegalAnnotationException(String message, Locatable src1, Locatable src2) { |
aoqi@0 | 63 | super(message); |
aoqi@0 | 64 | pos = build(src1,src2); |
aoqi@0 | 65 | } |
aoqi@0 | 66 | |
aoqi@0 | 67 | public IllegalAnnotationException(String message, Annotation src1, Annotation src2) { |
aoqi@0 | 68 | this(message,cast(src1),cast(src2)); |
aoqi@0 | 69 | } |
aoqi@0 | 70 | |
aoqi@0 | 71 | public IllegalAnnotationException(String message, Annotation src1, Locatable src2) { |
aoqi@0 | 72 | this(message,cast(src1),src2); |
aoqi@0 | 73 | } |
aoqi@0 | 74 | |
aoqi@0 | 75 | public IllegalAnnotationException(String message, Throwable cause, Locatable src) { |
aoqi@0 | 76 | super(message, cause); |
aoqi@0 | 77 | pos = build(src); |
aoqi@0 | 78 | } |
aoqi@0 | 79 | |
aoqi@0 | 80 | private static Locatable cast(Annotation a) { |
aoqi@0 | 81 | if(a instanceof Locatable) |
aoqi@0 | 82 | return (Locatable)a; |
aoqi@0 | 83 | else |
aoqi@0 | 84 | return null; |
aoqi@0 | 85 | } |
aoqi@0 | 86 | |
aoqi@0 | 87 | private List<List<Location>> build(Locatable... srcs) { |
aoqi@0 | 88 | List<List<Location>> r = new ArrayList<List<Location>>(); |
aoqi@0 | 89 | for( Locatable l : srcs ) { |
aoqi@0 | 90 | if(l!=null) { |
aoqi@0 | 91 | List<Location> ll = convert(l); |
aoqi@0 | 92 | if(ll!=null && !ll.isEmpty()) |
aoqi@0 | 93 | r.add(ll); |
aoqi@0 | 94 | } |
aoqi@0 | 95 | } |
aoqi@0 | 96 | return Collections.unmodifiableList(r); |
aoqi@0 | 97 | } |
aoqi@0 | 98 | |
aoqi@0 | 99 | /** |
aoqi@0 | 100 | * Builds a list of {@link Location}s out of a {@link Locatable}. |
aoqi@0 | 101 | */ |
aoqi@0 | 102 | private List<Location> convert(Locatable src) { |
aoqi@0 | 103 | if(src==null) return null; |
aoqi@0 | 104 | |
aoqi@0 | 105 | List<Location> r = new ArrayList<Location>(); |
aoqi@0 | 106 | for( ; src!=null; src=src.getUpstream()) |
aoqi@0 | 107 | r.add(src.getLocation()); |
aoqi@0 | 108 | return Collections.unmodifiableList(r); |
aoqi@0 | 109 | } |
aoqi@0 | 110 | |
aoqi@0 | 111 | |
aoqi@0 | 112 | |
aoqi@0 | 113 | /** |
aoqi@0 | 114 | * Returns a read-only list of {@link Location} that indicates |
aoqi@0 | 115 | * where in the source code the problem has happened. |
aoqi@0 | 116 | * |
aoqi@0 | 117 | * <p> |
aoqi@0 | 118 | * Normally, an annotation error happens on one particular |
aoqi@0 | 119 | * annotation, in which case this method returns a list that |
aoqi@0 | 120 | * contains another list, which in turn contains the location |
aoqi@0 | 121 | * information that leads to the error location |
aoqi@0 | 122 | * (IOW, <tt>[ [pos1,pos2,...,posN] ]</tt>) |
aoqi@0 | 123 | * |
aoqi@0 | 124 | * <p> |
aoqi@0 | 125 | * Sometimes, an error could occur because of two or more conflicting |
aoqi@0 | 126 | * annotations, in which case this method returns a list |
aoqi@0 | 127 | * that contains many lists, where each list contains |
aoqi@0 | 128 | * the location information that leads to each of the conflicting |
aoqi@0 | 129 | * annotations |
aoqi@0 | 130 | * (IOW, <tt>[ [pos11,pos12,...,pos1N],[pos21,pos22,...,pos2M], ... ]</tt>) |
aoqi@0 | 131 | * |
aoqi@0 | 132 | * <p> |
aoqi@0 | 133 | * Yet some other time, the runtime can fail to provide any |
aoqi@0 | 134 | * error location, in which case this method returns an empty list. |
aoqi@0 | 135 | * (IOW, <tt>[]</tt>). We do try hard to make sure this won't happen, |
aoqi@0 | 136 | * so please <a href="http://jaxb.dev.java.net/">let us know</a> |
aoqi@0 | 137 | * if you see this behavior. |
aoqi@0 | 138 | * |
aoqi@0 | 139 | * |
aoqi@0 | 140 | * <h3>List of {@link Location}</h3> |
aoqi@0 | 141 | * <p> |
aoqi@0 | 142 | * Each error location is identified not just by one {@link Location} |
aoqi@0 | 143 | * object, but by a sequence of {@link Location}s that shows why |
aoqi@0 | 144 | * the runtime is led to the place of the error. |
aoqi@0 | 145 | * This list is sorted such that the most specific {@link Location} comes |
aoqi@0 | 146 | * to the first in the list, sort of like a stack trace. |
aoqi@0 | 147 | * |
aoqi@0 | 148 | * <p> |
aoqi@0 | 149 | * For example, suppose you specify class <tt>Foo</tt> to {@link JAXBContext}, |
aoqi@0 | 150 | * <tt>Foo</tt> derives from <tt>Bar</tt>, <tt>Bar</tt> has a field <tt>pea</tt> |
aoqi@0 | 151 | * that points to <tt>Zot</tt>, <tt>Zot</tt> contains a <tt>gum</tt> |
aoqi@0 | 152 | * property, and this property has an errornous annotation. |
aoqi@0 | 153 | * Then when this exception is thrown, the list of {@link Location}s |
aoqi@0 | 154 | * will look something like |
aoqi@0 | 155 | * <tt>[ "gum property", "Zot class", "pea property", "Bar class", "Foo class" ]</tt> |
aoqi@0 | 156 | * |
aoqi@0 | 157 | * |
aoqi@0 | 158 | * @return |
aoqi@0 | 159 | * can be empty when no source position is available, |
aoqi@0 | 160 | * but never null. The returned list will never contain |
aoqi@0 | 161 | * null nor length-0 {@link List}. |
aoqi@0 | 162 | */ |
aoqi@0 | 163 | public List<List<Location>> getSourcePos() { |
aoqi@0 | 164 | return pos; |
aoqi@0 | 165 | } |
aoqi@0 | 166 | |
aoqi@0 | 167 | /** |
aoqi@0 | 168 | * Returns the exception name, message, and related information |
aoqi@0 | 169 | * together in one string. |
aoqi@0 | 170 | * |
aoqi@0 | 171 | * <p> |
aoqi@0 | 172 | * Overriding this method (instead of {@link #printStackTrace} allows |
aoqi@0 | 173 | * this crucial detail to show up even when this exception is nested |
aoqi@0 | 174 | * inside other exceptions. |
aoqi@0 | 175 | */ |
aoqi@0 | 176 | public String toString() { |
aoqi@0 | 177 | StringBuilder sb = new StringBuilder(getMessage()); |
aoqi@0 | 178 | |
aoqi@0 | 179 | for( List<Location> locs : pos ) { |
aoqi@0 | 180 | sb.append("\n\tthis problem is related to the following location:"); |
aoqi@0 | 181 | for( Location loc : locs ) |
aoqi@0 | 182 | sb.append("\n\t\tat ").append(loc.toString()); |
aoqi@0 | 183 | } |
aoqi@0 | 184 | |
aoqi@0 | 185 | return sb.toString(); |
aoqi@0 | 186 | } |
aoqi@0 | 187 | } |