| 1 |
/* |
| 2 |
* Copyright (c) 2016-present, RxJava Contributors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 |
* the License for the specific language governing permissions and limitations under the License. |
| 12 |
*/ |
| 13 |
|
| 14 |
package io.reactivex.rxjava3.validators; |
| 15 |
|
| 16 |
import java.io.*; |
| 17 |
import java.net.URL; |
| 18 |
import java.util.*; |
| 19 |
|
| 20 |
import org.junit.Test; |
| 21 |
|
| 22 |
public class NoAnonymousInnerClassesTest { |
| 23 |
|
| 24 |
@Test |
| 25 |
public void verify() throws Exception { |
| 26 |
URL u = NoAnonymousInnerClassesTest.class.getResource("/"); |
| 27 |
File f = new File(u.toURI()); |
| 28 |
|
| 29 |
String fs = f.toString().toLowerCase().replace("\\", "/"); |
| 30 |
|
| 31 |
System.out.println("Found " + fs); |
| 32 |
|
| 33 |
// running this particular test from IntelliJ will have the wrong class directory |
| 34 |
// gradle will generate test classes into a separate directory too |
| 35 |
int idx = fs.indexOf("/test"); |
| 36 |
if (idx >= 0) { |
| 37 |
f = new File(fs.substring(0, idx)); |
| 38 |
} |
| 39 |
|
| 40 |
StringBuilder b = new StringBuilder("Anonymous inner classes found:"); |
| 41 |
|
| 42 |
Queue<File> queue = new ArrayDeque<>(); |
| 43 |
|
| 44 |
queue.offer(f); |
| 45 |
|
| 46 |
String prefix = f.getAbsolutePath(); |
| 47 |
int count = 0; |
| 48 |
while (!queue.isEmpty()) { |
| 49 |
|
| 50 |
f = queue.poll(); |
| 51 |
|
| 52 |
if (f.isDirectory()) { |
| 53 |
File[] dir = f.listFiles(); |
| 54 |
if (dir != null && dir.length != 0) { |
| 55 |
for (File g : dir) { |
| 56 |
queue.offer(g); |
| 57 |
} |
| 58 |
} |
| 59 |
} else { |
| 60 |
String name = f.getName(); |
| 61 |
if (name.endsWith(".class") && name.contains("$") |
| 62 |
&& !name.contains("Perf") && !name.contains("Test") |
| 63 |
&& !name.startsWith("Test")) { |
| 64 |
String baseName = name.substring(0, name.length() - 6); |
| 65 |
String[] parts = name.split("\\$"); |
| 66 |
for (String s : parts) { |
| 67 |
if (Character.isDigit(s.charAt(0))) { |
| 68 |
String n = f.getAbsolutePath().substring(prefix.length()).replace('\\', '.').replace('/', '.'); |
| 69 |
if (n.startsWith(".")) { |
| 70 |
n = n.substring(1); |
| 71 |
} |
| 72 |
|
| 73 |
// javac generates switch-map anonymous classes with the same $x.class pattern |
| 74 |
// we have to look into the file and search for $SwitchMap$ |
| 75 |
|
| 76 |
boolean found = false; |
| 77 |
|
| 78 |
FileInputStream fin = new FileInputStream(f); |
| 79 |
try { |
| 80 |
byte[] data = new byte[fin.available()]; |
| 81 |
fin.read(data); |
| 82 |
|
| 83 |
String content = new String(data, "ISO-8859-1"); |
| 84 |
|
| 85 |
if (content.contains("$SwitchMap$")) { |
| 86 |
// the parent class can reference these synthetic inner classes |
| 87 |
// and thus they also have $SwitchMap$ |
| 88 |
// but the synthetic inner classes should not have further inner classes |
| 89 |
|
| 90 |
File[] filesInTheSameDir = f.getParentFile().listFiles(); |
| 91 |
|
| 92 |
for (File fsame : filesInTheSameDir) { |
| 93 |
String fsameName = fsame.getName(); |
| 94 |
if (fsameName.endsWith(".class")) { |
| 95 |
fsameName = fsameName.substring(0, fsameName.length() - 6); |
| 96 |
|
| 97 |
if (fsameName.startsWith(baseName) |
| 98 |
&& fsameName.length() > baseName.length() + 1 |
| 99 |
&& fsameName.charAt(baseName.length()) == '$' |
| 100 |
&& Character.isDigit(fsameName.charAt(baseName.length() + 1))) { |
| 101 |
found = true; |
| 102 |
break; |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
} else { |
| 107 |
found = true; |
| 108 |
} |
| 109 |
} finally { |
| 110 |
fin.close(); |
| 111 |
} |
| 112 |
|
| 113 |
if (found) { |
| 114 |
b.append("\r\n").append(n); |
| 115 |
count++; |
| 116 |
break; |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
if (count != 0) { |
| 125 |
throw new AssertionError(b.toString()); |
| 126 |
} |
| 127 |
} |
| 128 |
} |