如何为某些类重写Class <! - ? - > .getName()?

时间:2013-06-26 15:41:59

标签: aspectj

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

它应该返回一个自定义值,我认为最好在

这样的属性中定义
@NameOverride("Custom.fully.qualified.class.name")
public class CustomClass {}

有没有办法做到这一点?

2 个答案:

答案 0 :(得分:1)

弗雷德的回答是可以的,但是他的方面可以更优雅,代码更少,特别是更少的反射调用。对不起,我更喜欢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

答案 1 :(得分:0)

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

首先是文件结构:

./src/aspectj:
GetNameOverrider.aj

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

NameOverride.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