3

我的目标是能够覆盖从 CustomClass.class.getName() 和 CustomClass.getClass().getName() 返回的内容

它应该返回一个自定义值,我认为最好在属性中定义,例如

@NameOverride("Custom.fully.qualified.class.name")
public class CustomClass {}

有没有办法做到这一点?

4

2 回答 2

1

Fred 的回答还可以,但他的方面可能会更优雅,代码更少,尤其是反射调用更少。抱歉,我更喜欢 AspectJ 原生代码风格,但 @AspectJ 注释风格不会太长:

String around(Class clazz) : call(public String Class.getName()) && target(clazz) {
    NameOverride nameOverride = (NameOverride) clazz.getAnnotation(NameOverride.class);
    return nameOverride == null ? proceed(clazz) : nameOverride.value();
}

这是完整的源代码。我添加了一个没有注释的类来显示不同的行为以及对类定义的限制@Target(ElementType.TYPE)- 到注释类。我还显示包名称和导入:

package test;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface NameOverride {
    String value();
}
package test;

public class NormalClass {}
package test;

@NameOverride("Custom.fully.qualified.class.name")
public class CustomClass {}
package test;

public class Main {
    public static void main(String[] args) {
        System.out.println(NormalClass.class.getName());
        System.out.println(CustomClass.class.getName());
        System.out.println(new NormalClass().getClass().getName());
        System.out.println(new CustomClass().getClass().getName());
    }
}
package aspectj;

import test.NameOverride;

public aspect GetNameOverrider {
    @SuppressWarnings({ "unchecked", "rawtypes" })
    String around(Class clazz) : call(public String Class.getName()) && target(clazz) {
        NameOverride nameOverride = (NameOverride) clazz.getAnnotation(NameOverride.class);
        return nameOverride == null ? proceed(clazz) : nameOverride.value();
    }
}

输出:

test.NormalClass
Custom.fully.qualified.class.name
test.NormalClass
Custom.fully.qualified.class.name
于 2013-07-08T23:17:25.910 回答
0

这肯定不是最好/最快的解决方案,但可能是 POC ......

首先是文件结构:

./src/aspectj:
GetNameOverrider.aj

./src/test:
CustomClass.java    Main.java       NameOverride.java

名称覆盖.java:

@Retention(RetentionPolicy.RUNTIME)
public @interface NameOverride {
    String value();
}

GetNameOverrider.aj:

@Aspect
public class GetNameOverrider {
    @Around("call(String getName()) && !within(aspectj..*)")
    public String advice(ProceedingJoinPoint pjp) throws Throwable {

        String ret = (String) pjp.proceed();

        String className = "" + pjp.getTarget();
        className = className.replace("class ", "");
        try {
            test.NameOverride anno = Class.forName(className).getAnnotation(
                    test.NameOverride.class);

            if (anno != null) {
                return anno.value();
            }
        } catch (ClassNotFoundException e) {
            return ret;
        }

        return ret;
    }
}

给我 Main.java:

public class Main {
    public static void main(String[] args) {
        System.out.println(CustomClass.class.getName());
        System.out.println(new CustomClass().getClass().getName());
    }
}

输出:

Custom.fully.qualified.class.name
Custom.fully.qualified.class.name
于 2013-07-03T15:11:50.857 回答