public static Location getLocation()

in core/src/main/java/org/apache/struts2/util/location/LocationUtils.java [217:306]


    public static Location getLocation(Object obj, String description) {
        if (obj instanceof Location) {
            return (Location) obj;
        }

        if (obj instanceof Locatable) {
            return ((Locatable)obj).getLocation();
        }

        // Check some well-known locatable exceptions
        if (obj instanceof SAXParseException spe) {
            if (spe.getSystemId() != null) {
                return new LocationImpl(description, spe.getSystemId(), spe.getLineNumber(), spe.getColumnNumber());
            } else {
                return Location.UNKNOWN;
            }
        }

        if (obj instanceof TransformerException ex) {
            SourceLocator locator = ex.getLocator();
            if (locator != null && locator.getSystemId() != null) {
                return new LocationImpl(description, locator.getSystemId(), locator.getLineNumber(), locator.getColumnNumber());
            } else {
                return Location.UNKNOWN;
            }
        }

        if (obj instanceof Locator locator) {
            if (locator.getSystemId() != null) {
                return new LocationImpl(description, locator.getSystemId(), locator.getLineNumber(), locator.getColumnNumber());
            } else {
                return Location.UNKNOWN;
            }
        }

        if (obj instanceof Element) {
            return LocationAttributes.getLocation((Element)obj);
        }

        List<WeakReference<LocationFinder>> currentFinders = finders; // Keep the current list
        for (WeakReference<LocationFinder> ref : currentFinders) {
            LocationFinder finder = ref.get();
            if (finder == null) {
                // This finder was garbage collected: update finders
                synchronized (LocationFinder.class) {
                    // Update a clone of the current list to avoid breaking current iterations
                    List<WeakReference<LocationFinder>> newFinders = new ArrayList<>(finders);
                    newFinders.remove(ref);
                    finders = newFinders;
                }
            } else {
                Location result = finder.getLocation(obj, description);
                if (result != null) {
                    return result;
                }
            }
        }

        if (obj instanceof Throwable t) {
            StackTraceElement[] stack = t.getStackTrace();
            if (stack != null && stack.length > 0) {
                StackTraceElement trace = stack[0];
                if (trace.getLineNumber() >= 0) {
                    String uri = trace.getClassName();
                    if (trace.getFileName() != null) {
                        uri = uri.replace('.', '/');
                        uri = uri.substring(0, uri.lastIndexOf('/') + 1);
                        uri = uri + trace.getFileName();
                        URL url = ClassLoaderUtil.getResource(uri, LocationUtils.class);
                        if (url != null) {
                            uri = url.toString();
                        }
                    }
                    if (description == null) {
                        description = "Class: " + trace.getClassName() + "\n" +
                                "File: " + trace.getFileName() + "\n" +
                                "Method: " + trace.getMethodName() + "\n" +
                                "Line: " + trace.getLineNumber();
                    }
                    return new LocationImpl(description, uri, trace.getLineNumber(), -1);
                }
            }
        }

        if (obj instanceof StrutsJavaConfiguration) {
            return new LocationImpl(description, obj.toString());
        }

        return Location.UNKNOWN;
    }