Java Code Examples for javassist.bytecode.AccessFlag#isPublic()

The following examples show how to use javassist.bytecode.AccessFlag#isPublic() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavassistAdapter.java    From panda with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isPublic(@Nullable Object o) {
    if (o == null) {
        return false;
    }

    if (o instanceof ClassFile) {
        return AccessFlag.isPublic(((ClassFile) o).getAccessFlags());
    }

    if (o instanceof MethodInfo) {
        return AccessFlag.isPublic(((MethodInfo) o).getAccessFlags());
    }

    if (o instanceof FieldInfo) {
        return AccessFlag.isPublic(((FieldInfo) o).getAccessFlags());
    }

    if (o instanceof Class) {
        return Modifier.isPublic(((Class) o).getModifiers());
    }

    return false;
}
 
Example 2
Source File: ClassFileJavassist.java    From jbse with GNU General Public License v3.0 6 votes vote down vote up
@Override
public int getModifiers() {
    //this code reimplements CtClassType.getModifiers() to circumvent a bug
    int acc = this.cf.getAccessFlags();
    acc = clear(acc, SUPER);
    int inner = this.cf.getInnerAccessFlags();
    if (inner != -1) {
        if ((inner & STATIC) != 0) {
            acc |= STATIC;
        }
        if (AccessFlag.isPublic(inner)) {
            //seems that public nested classes already have the PUBLIC modifier set
            //but we are paranoid and we set it again
            acc = setPublic(acc);
        } else if (AccessFlag.isProtected(inner)) {
            acc = setProtected(acc);
        } else if (AccessFlag.isPrivate(inner)) {
            acc = setPrivate(acc);
        } else { //package visibility
            acc = setPackage(acc); //clear the PUBLIC modifier in case it is set
        }
    }
    return AccessFlag.toModifier(acc);
}
 
Example 3
Source File: LogLifeCycleProcessor.java    From loglifecycle with Apache License 2.0 5 votes vote down vote up
private void debugLifeCycleMethods(CtClass classToTransform, CtMethod[] methods)
    throws CannotCompileException, AfterBurnerImpossibleException, NotFoundException {
  for (CtMethod lifeCycleHook : methods) {
    String methodName = lifeCycleHook.getName();
    String className = classToTransform.getName();

    int accessFlags = lifeCycleHook.getMethodInfo().getAccessFlags();
    boolean isFinal = (accessFlags & AccessFlag.FINAL) == AccessFlag.FINAL;
    boolean canOverride = !isFinal && (AccessFlag.isPublic(accessFlags)
        || AccessFlag.isProtected(accessFlags)
        || AccessFlag.isPackage(accessFlags));

    if (canOverride && methodName.startsWith("on")) {
      log.info("Overriding " + methodName);
      try {

        String body = "android.util.Log.d(\"LogLifeCycle\", \""
            + className
            + " [\" + System.identityHashCode(this) + \"] \u27F3 "
            + methodName
            + "\");";
        afterBurner.afterOverrideMethod(classToTransform, methodName, body);
        log.info("Override successful " + methodName);
      } catch (Exception e) {
        logMoreIfDebug("Override didn't work ", e);
      }
    } else {
      log.info(
          "Skipping " + methodName + ". Either it is final, private or doesn't start by 'on...'");
    }
  }
}
 
Example 4
Source File: JavassistDynamicProxyClass.java    From code with Apache License 2.0 4 votes vote down vote up
/**
 * 动态代理对象构建
 */
public void newProxyInstance(ClassLoader loader, Class<?> proxyTarget,
                             InvocationHandler invocationHandler) throws NotFoundException,
        CannotCompileException, IllegalAccessException,
        InstantiationException, NoSuchMethodException,
        InvocationTargetException {
    ClassPool pool = new ClassPool();
    pool.insertClassPath(new LoaderClassPath(proxyTarget.getClassLoader()));
    CtClass targetClass = pool.get(proxyTarget.getName());
    // 构建代理类,父类是targetClass
    CtClass proxyClass = pool.makeClass(proxyTarget.getName() + "$proxy",
            targetClass);
    // 构建代理类的成员变量,添加到代理类
    CtField handlerField = new CtField(pool.get(InvocationHandler.class.getName()), "h", proxyClass);
    proxyClass.addField(handlerField);

    int methodIndex = 0;
    for (CtMethod ctMethod : targetClass.getDeclaredMethods()) {
        // 满足非public,native、static、final直接跳过
        if (!AccessFlag.isPublic(ctMethod.getModifiers())) {
            continue;
        } else if ((ctMethod.getModifiers() & AccessFlag.NATIVE) != 0) {
            continue;
        } else if ((ctMethod.getModifiers() & AccessFlag.STATIC) != 0) {
            continue;
        } else if ((ctMethod.getModifiers() & AccessFlag.FINAL) != 0) {
            continue;
        }
        String methodName = ctMethod.getName() + methodIndex;
        CtField methodField = new CtField(pool.get(Method.class.getName()),
                methodName, proxyClass);
        StringBuilder paramTypeSrc = new StringBuilder("new Class[]{");
        for (int i = 0; i < ctMethod.getParameterTypes().length; i++) {
            if (i != 0) {
                paramTypeSrc.append(",");
            }
            paramTypeSrc.append(ctMethod.getParameterTypes()[i].getName()).append(".class");
        }
        paramTypeSrc.append("}");
        String d = proxyTarget.getName() + ".class";
        String initSrc = getClass().getName() + ".getMethod(" + d + ",\""
                + ctMethod.getName() + "\"," + paramTypeSrc.toString() + ")";

        proxyClass.addField(methodField, initSrc);

        CtMethod copyMethod = CtNewMethod.copy(ctMethod, proxyClass, null);

        String bodySrc = "{";
        bodySrc += "Object result=h.invoke($0," + methodName + ",$args);";

        if (!"void".equals(copyMethod.getReturnType().getName())) {
            bodySrc += "return ($r)result;";
        }
        bodySrc += "}";

        copyMethod.setBody(bodySrc);
        proxyClass.addMethod(copyMethod);
        methodIndex++;
    }
    // 构建代理类的构造器
    CtConstructor constructor = new CtConstructor(
            new CtClass[]{pool.get(InvocationHandler.class.getName())},
            proxyClass);
    constructor.setBody("h=$1;");
    proxyClass.addConstructor(constructor);
    // 生成代理类字节码文件
    proxyClass.debugWriteFile("D:\\code\\GitRepository\\Apm\\apm\\javassist\\target");
    Class clazz = proxyClass.toClass();

    UserServiceImpl userService = (UserServiceImpl) clazz.getConstructor(
            InvocationHandler.class).newInstance(invocationHandler);
    userService.getName("11");
}
 
Example 5
Source File: JType.java    From jadira with Apache License 2.0 4 votes vote down vote up
public boolean isPublic() {
    return AccessFlag.isPublic(classFile.getAccessFlags());
}
 
Example 6
Source File: JMethod.java    From jadira with Apache License 2.0 4 votes vote down vote up
public boolean isPublic() {
    return AccessFlag.isPublic(getMethodInfo().getAccessFlags());
}
 
Example 7
Source File: JConstructor.java    From jadira with Apache License 2.0 4 votes vote down vote up
public boolean isPublic() {
    return AccessFlag.isPublic(getMethodInfo().getAccessFlags());
}