|
| 1 | +#!/usr/bin/env bash |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Copyright 2023-2025 The Enola <https://enola.dev> Authors |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | + |
| 18 | +# set -euo pipefail |
| 19 | + |
| 20 | +# This script detects any usages of java.net.URL in **/*.java. |
| 21 | +# See class dev.enola.common.io.iri.URL for background about why. |
| 22 | +# |
| 23 | +# It technically misses "implicit" usages of java.net.URL, such as: |
| 24 | +# Resources.getResource("test.txt").toURI(); but that's OK, for now. |
| 25 | +# TODO Write a better JVM level test; e.g. with ErrorProne or SpotBugs. |
| 26 | + |
| 27 | +set -euo pipefail |
| 28 | + |
| 29 | +allow_list=("java/dev/enola/common/io/resource/UrlResource.java" |
| 30 | + "java/dev/enola/common/io/resource/ClasspathResource.java") |
| 31 | + |
| 32 | +found_files=$(find . -name "*.java" -print0 | xargs -0 grep -lE "(^|[^a-zA-Z0-9_.])java\.net\.URL($|[^a-zA-Z0-9_#}])" | while IFS= read -r file; do |
| 33 | + file_name=$(basename "$file") |
| 34 | + allow_path="${file//.\//}" |
| 35 | +
|
| 36 | + allowed=false |
| 37 | + for allowed_file in "${allow_list[@]}"; do |
| 38 | + if [[ "$file_name" == "$allowed_file" ]] || [[ "$allow_path" == "$allowed_file" ]]; then |
| 39 | + allowed=true |
| 40 | + break |
| 41 | + fi |
| 42 | + done |
| 43 | +
|
| 44 | + if "$allowed"; then |
| 45 | + continue |
| 46 | + fi |
| 47 | +
|
| 48 | + echo "$file" |
| 49 | +done) |
| 50 | +
|
| 51 | +if [[ -n "$found_files" ]]; then |
| 52 | + echo "Found use of forbidden bad java.net.URL in the following files, please fix:" |
| 53 | + echo "$found_files" |
| 54 | + exit 1 |
| 55 | +else |
| 56 | + exit 0 |
| 57 | +fi |
0 commit comments