1 /* 2 * Copyright (C) 2021-2025 LSPosed 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 org.lsposed.hiddenapibypass; 18 19 import java.lang.invoke.MethodType; 20 import java.util.HashSet; 21 import java.util.Set; 22 23 @SuppressWarnings("unused") 24 public class Helper { 25 static final Set<String> signaturePrefixes = new HashSet<>(); 26 checkArgsForInvokeMethod(java.lang.Class<?>[] params, Object[] args)27 static boolean checkArgsForInvokeMethod(java.lang.Class<?>[] params, Object[] args) { 28 if (params.length != args.length) return false; 29 for (int i = 0; i < params.length; ++i) { 30 if (params[i].isPrimitive()) { 31 if (params[i] == int.class && !(args[i] instanceof Integer)) return false; 32 else if (params[i] == byte.class && !(args[i] instanceof Byte)) return false; 33 else if (params[i] == char.class && !(args[i] instanceof Character)) return false; 34 else if (params[i] == boolean.class && !(args[i] instanceof Boolean)) return false; 35 else if (params[i] == double.class && !(args[i] instanceof Double)) return false; 36 else if (params[i] == float.class && !(args[i] instanceof Float)) return false; 37 else if (params[i] == long.class && !(args[i] instanceof Long)) return false; 38 else if (params[i] == short.class && !(args[i] instanceof Short)) return false; 39 } else if (args[i] != null && !params[i].isInstance(args[i])) return false; 40 } 41 return true; 42 } 43 44 static public class MethodHandle { 45 private final MethodType type = null; 46 private MethodType nominalType; 47 private MethodHandle cachedSpreadInvoker; 48 protected final int handleKind = 0; 49 50 // The ArtMethod* or ArtField* associated with this method handle (used by the runtime). 51 protected final long artFieldOrMethod = 0; 52 } 53 54 static final public class Class { 55 private transient ClassLoader classLoader; 56 private transient java.lang.Class<?> componentType; 57 private transient Object dexCache; 58 private transient Object extData; 59 private transient Object[] ifTable; 60 private transient String name; 61 private transient java.lang.Class<?> superClass; 62 private transient Object vtable; 63 private transient long iFields; 64 private transient long methods; 65 private transient long sFields; 66 private transient int accessFlags; 67 private transient int classFlags; 68 private transient int classSize; 69 private transient int clinitThreadId; 70 private transient int dexClassDefIndex; 71 private transient volatile int dexTypeIndex; 72 private transient int numReferenceInstanceFields; 73 private transient int numReferenceStaticFields; 74 private transient int objectSize; 75 private transient int objectSizeAllocFastPath; 76 private transient int primitiveType; 77 private transient int referenceInstanceOffsets; 78 private transient int status; 79 private transient short copiedMethodsOffset; 80 private transient short virtualMethodsOffset; 81 } 82 83 static public class AccessibleObject { 84 private boolean override; 85 } 86 87 static final public class Executable extends AccessibleObject { 88 private Class declaringClass; 89 private Class declaringClassOfOverriddenMethod; 90 private Object[] parameters; 91 private long artMethod; 92 private int accessFlags; 93 } 94 95 @SuppressWarnings("EmptyMethod") 96 public static class NeverCall { a()97 private static void a() { 98 } 99 b()100 private static void b() { 101 } 102 103 private static int s; 104 private static int t; 105 private int i; 106 private int j; 107 } 108 109 public static class InvokeStub { invoke(Object... args)110 private static Object invoke(Object... args) { 111 throw new IllegalStateException("Failed to invoke the method"); 112 } 113 InvokeStub(Object... args)114 private InvokeStub(Object... args) { 115 throw new IllegalStateException("Failed to new a instance"); 116 } 117 } 118 } 119