开发者

Java custom annotation to restricted access to method

I am building an application on two layer. Web layer and business layer.

Inside the business layer I have some public method that can be called within the business layer or from the we开发者_开发知识库b layer.

I only want some of these methods being called from the web layer (the safe one).

I was wondering if I can create a annotation in my business layer, for example @Public which means I can call this method from the web layer, and @Private so I should not use this method from the web layer.

And when I try to call a @private method from the web layer (in eclipse) it gives me a warning?

As well: Can I have a way to list automatically all this method private and public?


AFAIK you can't make Eclipse use annotation to determine whether you can access a method from a certain file. For this to be possible Eclipse would have to know whether the file is part of the web layer or the business layer.

In order to list all methods having a certain annotation, you could use reflection at runtime. In Eclipse there might be filters, but I don't know of any annotation based filters.

Maybe you should choose another approach, I'll shortly describe how we do that:

We have two interfaces that our services may implement:

  • one public interface that contains all the methods the web layer may see
  • one private interface that contains all the methods internal to the business logic

We split those interfaces into two eclipse projects - one public api project and one implementation project that contains the services and internal api - and just allow access to the public api project from the web layer.

Since our services (EJB 3.0) need an interface, we have to add the internal one, if we have internal methods. However, with other technology (like EJB 3.1) you might also just provide the public interface.

Another approach might be to split the interfaces into two packages, e.g. myproject.api.pub (public is a keyword) and myproject.api.internal, and then use package based filters in Eclipse.


The first thing that comes up in my mind is that this would only be needed in a bad-designed two-layer app.

You can use access control to make sure that the web gui can only access the safe methods, and keep other methods to the business layer.

It is probably possible to just make those "public" methods that you don't want to be used by the web interface private; that way you can use them in the public, safe, methods in the business logic.

Though without knowing how your project is set up, giving concrete examples is kind of impossible.

But say; you can have:
com.somecompany.gui > contains all web-gui stuff
com.somecompany.logic > contains business logic.

In the logic package, you create classes that have public methods to be used from gui, and private or - if needed by other logic components - package private (protected) methods that cannot be accessed from the gui package. That way you can separate your logic from the interface without having a need for the annotation you want to make.


In general I'd say: yes, it could work. At least to produce compiler warnings.

We have the @Override annotation for methods. This annotation is used for a similar reason: verify at compile time, that certain conditions are met. In this case: the annotated methods overrides a method from a superclass or it implements an interface method or an abstract method. If the verifier finds out, the this is not the case, then the compiler will produce a compile time error.

So it should be possible here too. We could think of an annotations like

 @Layer("servicelayer")                // class annotation

 @Private(layer="servicelayer")        // method annotation

And now we could verify at compile time, that annotated methods can only be called from classes that have the same layer annotation. If the condition is not met, the compiler could produce a warning (iaw: the compiler could detect, if we accidentally call an internal service layer method from a web layer class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜