/** * Prints all the methods declared in the given class. */ public void print(CtClass clazz) { CtMethod[] methods = clazz.getDeclaredMethods(); for (int i = 0; i < methods.length; i++) { print(methods[i]); } }
/** * Prints all the methods declared in the given class. */ public void print(CtClass clazz) { CtMethod[] methods = clazz.getDeclaredMethods(); for (int i = 0; i < methods.length; i++) { print(methods[i]); } }
private static String getIsSupportMethodBody(CtClass patchClass, String modifiedClassName) throws NotFoundException { StringBuilder isSupportBuilder = new StringBuilder(); StringBuilder methodsIdBuilder = new StringBuilder(); if (Constants.isLogging) { isSupportBuilder.append(" android.util.Log.d(\"robust\",\"arrivied in isSupport \"+methodName+\" paramArrayOfObject \" +paramArrayOfObject);"); } isSupportBuilder.append("String methodNo=$1.split(\":\")[3];\n"); if (Constants.isLogging) { isSupportBuilder.append(" android.util.Log.d(\"robust\",\"in isSupport assemble method number is \" + methodNo);"); } for (CtMethod method : patchClass.getDeclaredMethods()) { String methodSignure = JavaUtils.getJavaMethodSignure(method).replaceAll(patchClass.getName(), modifiedClassName); String methodLongName = modifiedClassName + "." + methodSignure; Integer methodNumber = Config.methodMap.get(methodLongName); //just Forward methods with methodNumber if (methodNumber != null) { // 一前一后的冒号作为匹配锚点,只有一边有的话可能会有多重匹配的bug methodsIdBuilder.append(":" + methodNumber + ":"); } } if (Constants.isLogging) { isSupportBuilder.append(" android.util.Log.d(\"robust\",\"arrivied in isSupport \"+methodName+\" paramArrayOfObject \" +paramArrayOfObject+\" isSupport result is \"+\"" + methodsIdBuilder.toString() + "\".contains(\":\" + methodNo + \":\"));"); } isSupportBuilder.append("return \"" + methodsIdBuilder.toString() + "\".contains(\":\" + methodNo + \":\");"); return isSupportBuilder.toString(); }
private static CtMethod getterOrNull(CtClass containerClass, String propertyName) { for ( CtMethod method : containerClass.getDeclaredMethods() ) { try {
private CtMethod findOriginal(CtMethod m, boolean dontSearch) throws NotFoundException { if (dontSearch) return m; String name = m.getName(); CtMethod[] ms = m.getDeclaringClass().getDeclaredMethods(); for (int i = 0; i < ms.length; ++i) { String orgName = ms[i].getName(); if (orgName.endsWith(name) && orgName.startsWith(ClassMetaobject.methodPrefix) && ms[i].getSignature().equals(m.getSignature())) return ms[i]; } return m; }
private void ensureNewInstance(CtClass cc) throws NotFoundException, CannotCompileException { CtMethod ccms[] = cc.getDeclaredMethods(); if( !javassist.Modifier.isAbstract(cc.getModifiers()) && !hasExisting("newInstance", "()Lwater/Freezable;", ccms) ) { cc.addMethod(CtNewMethod.make( "public water.Freezable newInstance() {\n" + " return new " +cc.getName()+"();\n" + "}", cc)); } }
@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(); }
private CtMethod findOriginal(CtMethod m, boolean dontSearch) throws NotFoundException { if (dontSearch) return m; String name = m.getName(); CtMethod[] ms = m.getDeclaringClass().getDeclaredMethods(); for (int i = 0; i < ms.length; ++i) { String orgName = ms[i].getName(); if (orgName.endsWith(name) && orgName.startsWith(ClassMetaobject.methodPrefix) && ms[i].getSignature().equals(m.getSignature())) return ms[i]; } return m; }
private void ensureType(CtClass cc) throws NotFoundException, CannotCompileException { CtMethod ccms[] = cc.getDeclaredMethods(); if( !javassist.Modifier.isAbstract(cc.getModifiers()) && !hasExisting("frozenType", "()I", ccms) ) { // Build a simple field & method returning the type token cc.addField(new CtField(CtClass.intType, "_frozen$type", cc)); cc.addMethod(CtNewMethod.make("public int frozenType() {" + " return _frozen$type == 0 ? (_frozen$type=water.TypeMap.onIce(\""+cc.getName()+"\")) : _frozen$type;" + "}",cc)); } }
for (CtMethod method : patchClass.getDeclaredMethods()) { CtClass[] parametertypes = method.getParameterTypes(); String methodSignure = JavaUtils.getJavaMethodSignure(method).replaceAll(patchClass.getName(), modifiedClassName);
/** * 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)); } }
out.writeUTF(javaName); CtMethod[] methods = clazz.getDeclaredMethods();
/** * 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())); }
public static List<Method> getDeclaredMethods(Class<?> clazz) { ClassPool cp = new ClassPool(); cp.insertClassPath(new ClassClassPath(clazz)); CtClass cc; try { cc = cp.get(clazz.getName()); } catch (NotFoundException e) { throw U.rte("Cannot find the target class!", e); } List<Method> methods = U.list(); for (CtMethod m : cc.getDeclaredMethods()) { try { methods.add(getMethod(clazz, m.getName(), ctTypes(m.getParameterTypes()))); } catch (Exception e) { throw U.rte(e); } } return methods; }
if (ctClass.isInterface() || ctClass.getDeclaredMethods().length < 1) {
CtMethod ccms[] = cc.getDeclaredMethods(); boolean w = hasExisting("write", "(Lwater/AutoBuffer;)Lwater/AutoBuffer;", ccms); boolean r = hasExisting("read" , "(Lwater/AutoBuffer;)Lwater/Freezable;" , ccms);
@Override public void scan(Object cls) { try { CtClass ctClass = getClassPool().get(getMetadataAdapter().getClassName(cls)); for (CtBehavior member : ctClass.getDeclaredConstructors()) { scanMember(member); } for (CtBehavior member : ctClass.getDeclaredMethods()) { scanMember(member); } ctClass.detach(); } catch (Exception e) { throw new ReflectionsException("Could not scan method usage for " + getMetadataAdapter().getClassName(cls), e); } }
@Override public void scan(Object cls) { try { CtClass ctClass = getClassPool().get(getMetadataAdapter().getClassName(cls)); for (CtBehavior member : ctClass.getDeclaredConstructors()) { scanMember(member); } for (CtBehavior member : ctClass.getDeclaredMethods()) { scanMember(member); } ctClass.detach(); } catch (Exception e) { throw new ReflectionsException("Could not scan method usage for " + getMetadataAdapter().getClassName(cls), e); } }
CtMethod[] ctMethods = ctClass.getDeclaredMethods(); for (CtMethod ctMethod : ctMethods) { boolean enableDurationProfiling = durationProfilingFilter.matchMethod(ctClass.getName(), ctMethod.getName());
private void ensureVersion(CtClass cc) throws NotFoundException, CannotCompileException, BadBytecode { CtMethod ccms[] = cc.getDeclaredMethods(); if (!javassist.Modifier.isAbstract(cc.getModifiers())) { String gsig = cc.getGenericSignature(); ClassSignature csig = SignatureAttribute.toClassSignature(gsig); // Warning: this is not doing proper parent (superclass/interfaces) traversal TypeArgument ta = getTypeArg(csig.getSuperClass().getTypeArguments(), "Lwater/api/rest/Version"); if (ta!=null && !hasExisting("getVersion", "()"+ta.getType().encode(), ccms) ) { String typeName = ta.toString(); String valueName = getValueFromType(typeName); //cc.addMethod(CtNewMethod.make("public "+typeName+" getVersion() {" + cc.addMethod(CtNewMethod.make("public water.api.rest.Version getVersion() {" + " return "+valueName+";" + "}",cc)); } } } private String getValueFromType(String typeName) {