1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package libcore.tools.analyzer.openjdk;
18 
19 import org.objectweb.asm.ClassReader;
20 import org.objectweb.asm.tree.ClassNode;
21 import org.objectweb.asm.tree.InnerClassNode;
22 
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.Locale;
26 import java.util.zip.ZipEntry;
27 import java.util.zip.ZipFile;
28 
29 /**
30  * Util functions for parsing .jar / .jmod / .class file.
31  */
32 public class ClassFileUtil {
parseClass(InputStream in)33     static ClassNode parseClass(InputStream in) throws IOException {
34         ClassReader classReader = new ClassReader(in);
35         ClassNode node = new ClassNode();
36         classReader.accept(node, 0);
37         return node;
38     }
39 
isAnonymousClass(ClassNode classNode)40     static boolean isAnonymousClass(ClassNode classNode) {
41         if (classNode.outerClass == null) {
42             return false;
43         }
44         for (InnerClassNode node : classNode.innerClasses) {
45             if (classNode.name.equals(node.name)
46                     && node.outerName == null && node.innerName == null) {
47                 return true;
48             }
49         }
50 
51         return false;
52     }
53 
getEntryFromClassName(ZipFile zipFile, String className, boolean useSlash)54     static ZipEntry getEntryFromClassName(ZipFile zipFile, String className,
55             boolean useSlash) {
56         String entryName = useSlash ? className : className.replaceAll("\\.", "/");
57         entryName += ".class";
58         ZipEntry e = zipFile.getEntry(entryName);
59         if (e == null) {
60             String secondName = "classes/" + entryName;
61             e = zipFile.getEntry(secondName);
62         }
63         return e;
64     }
65 
getEntryFromClassNameOrThrow(ZipFile zipFile, String className)66     static ZipEntry getEntryFromClassNameOrThrow(ZipFile zipFile,
67             String className) throws IllegalArgumentException {
68         ZipEntry e =  getEntryFromClassName(zipFile, className, false);
69         if (e == null) {
70             String entryName = className + ".class";
71             throw new IllegalArgumentException(String.format(Locale.US,
72                     "Neither %s nor %s is found in %s", entryName, "classes/" + entryName,
73                     zipFile.getName()));
74         }
75         return e;
76     }
77 
78 }
79