Map getDeclaredInterfaces(CtClass clazz, Map map) { if (map == null) map = new HashMap(); if (clazz.isInterface()) map.put(clazz.getName(), clazz); CtClass[] interfaces; try { interfaces = clazz.getInterfaces(); } catch (NotFoundException e) { throw new RuntimeException(e); } for (int i = 0; i < interfaces.length; i++) { CtClass intf = interfaces[i]; map.put(intf.getName(), intf); getDeclaredInterfaces(intf, map); } return map; }
Map getAllInterfaces(CtClass clazz, Map map) { if (map == null) map = new HashMap(); if (clazz.isInterface()) map.put(clazz.getName(), clazz); do { try { CtClass[] interfaces = clazz.getInterfaces(); for (int i = 0; i < interfaces.length; i++) { CtClass intf = interfaces[i]; map.put(intf.getName(), intf); getAllInterfaces(intf, map); } clazz = clazz.getSuperclass(); } catch (NotFoundException e) { throw new RuntimeException(e); } } while (clazz != null); return map; }
/** * Appends INVOKESPECIAL. * * @param clazz the target class. * @param name the method name * @param desc the descriptor of the method signature. * * @see Descriptor#ofMethod(CtClass,CtClass[]) * @see Descriptor#ofConstructor(CtClass[]) */ public void addInvokespecial(CtClass clazz, String name, String desc) { boolean isInterface = clazz == null ? false : clazz.isInterface(); addInvokespecial(isInterface, constPool.addClassInfo(clazz), name, desc); }
Map<String,CtClass> getDeclaredInterfaces(CtClass clazz, Map<String,CtClass> map) { if (map == null) map = new HashMap<String,CtClass>(); if (clazz.isInterface()) map.put(clazz.getName(), clazz); CtClass[] interfaces; try { interfaces = clazz.getInterfaces(); } catch (NotFoundException e) { throw new RuntimeException(e); } for (CtClass intf:interfaces) { map.put(intf.getName(), intf); getDeclaredInterfaces(intf, map); } return map; }
/** * Appends INVOKESTATIC. * * @param clazz the target class. * @param name the method name * @param desc the descriptor of the method signature. * * @see Descriptor#ofMethod(CtClass,CtClass[]) */ public void addInvokestatic(CtClass clazz, String name, String desc) { boolean isInterface; if (clazz == THIS) isInterface = false; else isInterface = clazz.isInterface(); addInvokestatic(constPool.addClassInfo(clazz), name, desc, isInterface); }
Map<String,CtClass> getAllInterfaces(CtClass clazz, Map<String,CtClass> map) { if (map == null) map = new HashMap<String,CtClass>(); if (clazz.isInterface()) map.put(clazz.getName(), clazz); do { try { CtClass[] interfaces = clazz.getInterfaces(); for (CtClass intf:interfaces) { map.put(intf.getName(), intf); getAllInterfaces(intf, map); } clazz = clazz.getSuperclass(); } catch (NotFoundException e) { throw new RuntimeException(e); } } while (clazz != null); return map; }
/** * Appends INVOKESPECIAL. * * @param clazz the target class. * @param name the method name * @param desc the descriptor of the method signature. * * @see Descriptor#ofMethod(CtClass,CtClass[]) * @see Descriptor#ofConstructor(CtClass[]) */ public void addInvokespecial(CtClass clazz, String name, String desc) { boolean isInterface = clazz == null ? false : clazz.isInterface(); addInvokespecial(isInterface, constPool.addClassInfo(clazz), name, desc); }
/** * Appends INVOKESTATIC. * * @param clazz the target class. * @param name the method name * @param desc the descriptor of the method signature. * * @see Descriptor#ofMethod(CtClass,CtClass[]) */ public void addInvokestatic(CtClass clazz, String name, String desc) { boolean isInterface; if (clazz == THIS) isInterface = false; else isInterface = clazz.isInterface(); addInvokestatic(constPool.addClassInfo(clazz), name, desc, isInterface); }
@Override protected void insertCode(List<CtClass> box, File jarFile) throws IOException, CannotCompileException { ZipOutputStream outStream = new JarOutputStream(new FileOutputStream(jarFile)); //get every class in the box ,ready to insert code for (CtClass ctClass : box) { //change modifier to public ,so all the class in the apk will be public ,you will be able to access it in the patch ctClass.setModifiers(AccessFlag.setPublic(ctClass.getModifiers())); if (isNeedInsertClass(ctClass.getName()) && !(ctClass.isInterface() || ctClass.getDeclaredMethods().length < 1)) { //only insert code into specific classes zipFile(transformCode(ctClass.toBytecode(), ctClass.getName().replaceAll("\\.", "/")), outStream, ctClass.getName().replaceAll("\\.", "/") + ".class"); } else { zipFile(ctClass.toBytecode(), outStream, ctClass.getName().replaceAll("\\.", "/") + ".class"); } } outStream.close(); }
NotFoundException if (clazz.isInterface()) throw new CannotReflectException( "Cannot reflect an interface: " + clazz.getName());
/** * Constructs an annotation that can be accessed through the interface * represented by <code>clazz</code>. The values of the members are * not specified. * * @param cp the constant pool table. * @param clazz the interface. * @throws NotFoundException when the clazz is not found */ public Annotation(ConstPool cp, CtClass clazz) throws NotFoundException { // todo Enums are not supported right now. this(cp.addUtf8Info(Descriptor.of(clazz.getName())), cp); if (!clazz.isInterface()) throw new RuntimeException( "Only interfaces are allowed for Annotation creation."); CtMethod methods[] = clazz.getDeclaredMethods(); if (methods.length > 0) { members = new LinkedHashMap(); } for (int i = 0; i < methods.length; i++) { CtClass returnType = methods[i].getReturnType(); addMemberValue(methods[i].getName(), createMemberValue(cp, returnType)); } }
NotFoundException if (clazz.isInterface()) throw new CannotReflectException( "Cannot reflect an interface: " + clazz.getName());
/** * Constructs an annotation that can be accessed through the interface * represented by <code>clazz</code>. The values of the members are * not specified. * * @param cp the constant pool table. * @param clazz the interface. * @throws NotFoundException when the clazz is not found */ public Annotation(ConstPool cp, CtClass clazz) throws NotFoundException { // todo Enums are not supported right now. this(cp.addUtf8Info(Descriptor.of(clazz.getName())), cp); if (!clazz.isInterface()) throw new RuntimeException( "Only interfaces are allowed for Annotation creation."); CtMethod[] methods = clazz.getDeclaredMethods(); if (methods.length > 0) members = new LinkedHashMap<String,Pair>(); for (CtMethod m:methods) addMemberValue(m.getName(), createMemberValue(cp, m.getReturnType())); }
if (ctClass.isInterface() || ctClass.getDeclaredMethods().length < 1) {
else { if (!Modifier.isPublic(declClass.getModifiers()) || declClass.isInterface() != targetClass.isInterface()) declClass = targetClass; if (declClass.isInterface()) { int nargs = Descriptor.paramSize(desc) + 1; bytecode.addInvokeinterface(declClass, mname, desc, nargs);
private boolean enhance(CtClass managedCtClass) { if ( managedCtClass.isInterface() ) { log.debugf( "Skipping enhancement of [%s]: it's an interface!", managedCtClass.getName() ); return false;
return new ArrayMemberValue(member, cp); else if (type.isInterface()) { Annotation info = new Annotation(cp, type); return new AnnotationMemberValue(info, cp);
if (Modifier.isStatic(mod1) != Modifier.isStatic(mod2) || (Modifier.isPrivate(mod1) && !Modifier.isPrivate(mod2)) || origMethod.getDeclaringClass().isInterface() != substMethod.getDeclaringClass().isInterface()) throw new CannotCompileException("invoke-type mismatch " + substMethod.getLongName());
return new ArrayMemberValue(member, cp); else if (type.isInterface()) { Annotation info = new Annotation(cp, type); return new AnnotationMemberValue(info, cp);
if (Modifier.isStatic(mod1) != Modifier.isStatic(mod2) || (Modifier.isPrivate(mod1) && !Modifier.isPrivate(mod2)) || origMethod.getDeclaringClass().isInterface() != substMethod.getDeclaringClass().isInterface()) throw new CannotCompileException("invoke-type mismatch " + substMethod.getLongName());