简体   繁体   English

用于实施静态变量或静态方法的Java注释?

[英]Java annotation for enforcing static variables or static methods?

I'm interested in doing something like this: 我有兴趣做这样的事情:

public interface Foo {

  public static "abstract" Bar SOME_BAR; // subclasses define one of these

}

and

public interface Foo {

  public static "abstract" Baz buildABas(...); // subclasses define this method 

}

without the statics, this is OOP 101, but it can't be done in standard oop java. 没有静态,这是OOP 101,但它不能在标准的oop java中完成。 I wonder if there's an annotation that would ensure this behavior? 我想知道是否有一个注释可以确保这种行为?

edit: 编辑:

i'm interested in specifying a set of options which define how to set things for "configurable" objects. 我有兴趣指定一组选项来定义如何为“可配置”对象设置内容。 this could be command-line flags, etc. 这可能是命令行标志等。

I'm guessing what you're wanting is to have a method like 我猜你想要的是有一个像这样的方法

public void callFoo(Class<?> clazz)

and you want to ensure that clazz has a method public static void foo() . 并且你想确保clazz有一个方法public static void foo()

I thought about this a while and none of the techniques that come to mind will get you there. 我想了一会儿,想到的技术都没有让你到那里。 You can use an AnnotationProcessor to ensure that any classes annotated with a certain annotation have a specific method or what have you (and generate a compile error if they don't) but there's no way to ensure (at compile time) that Class arguments passed to callFoo(Class<?> clazz) are annotated with your annotation. 您可以使用AnnotationProcessor来确保使用特定注释注释的任何类具有特定方法或具有哪些方法(如果没有,则生成编译错误)但是无法确保(在编译时)传递的Class参数callFoo(Class<?> clazz)用注释注释。

Here's an AnnotationProcessor that gets you halfway there: 这是一个AnnotationProcessor,可以让你到达那里:

import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Messager;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;


@SupportedSourceVersion(SourceVersion.RELEASE_6)
@SupportedAnnotationTypes("so.Foo")
public class FooAnnotationProcessor extends AbstractProcessor {

    @Override
    public boolean process(Set<? extends TypeElement> annotations,
            RoundEnvironment roundEnv) {

        TypeElement foo = processingEnv.getElementUtils().getTypeElement("so.Foo");
        Set<? extends Element> classes = roundEnv.getElementsAnnotatedWith(foo);
        Messager messenger = processingEnv.getMessager();
        for (Element e : classes) {
            boolean found = false;
            for (Element method : e.getEnclosedElements()) {
                messenger.printMessage(Diagnostic.Kind.ERROR, 
                        method.getSimpleName());
                if (method.getKind() == ElementKind.METHOD && method.getSimpleName().toString().equals("getInstance")) {
                    found = true;
                    break;
                }
            }
            if (!found)
            {
                messenger.printMessage(Diagnostic.Kind.ERROR, 
                    "The following class does not implement getInstance : " + e.getSimpleName(),e);
            }
        }
        return true;
    }

}

Ultimately, I would suggest you either allow it to be something enforced at runtime or your redesign your code so you don't need to use static methods. 最后,我建议您允许它在运行时强制执行或重新设计代码,这样您就不需要使用静态方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM