我有一个无法找到答案的 Java 问题:我有一个类 A,其中包含一个方法 a(),该方法只能从扩展类 B 的类中调用。目前,我能找到的最佳解决方案如下:
Class callerClass = Reflection.getCallerClass(2);
if (!B.class.isAssignableFrom(callerClass))
throw new InvalidMethodCallException("A.a", B.class.getName(), callerClass.getName());
InvalidMethodCallException
是我自己的一个异常(exception)。
但是,我对我的解决方案并不完全满意,因为调用是在运行时进行的。相反,我想在 a()
中声明,只有 B
应该有权访问它,但要在编译时检查它,因为反射是一个代价高昂的过程。
你知道正确的方法吗?
感谢您的帮助。
编辑:附加信息
一个更简单的例子是,我不希望用户调用 Edge
类中的 connect(port1, port2)
方法,而是调用 Graph
类中的 addEdge(edge, port1, port2)
方法,因为图表还必须记住内部有哪些边。
这是我的Graph
类代码:
public void addEdge (Edge edge, Port firstPort, Port secondPort)
throws DuplicateEdgeInGraphException, AttachedNodeNotInGraphException, GraphDescriptionModelException
{
// Must not appear in the graph
if (this.edges.contains(edge))
throw new DuplicateEdgeInGraphException(edge);
// The nodes attached to the ports must be in the graph
if (!this.nodes.contains(firstPort.getNode()))
throw new AttachedNodeNotInGraphException(firstPort);
if (!this.nodes.contains(secondPort.getNode()))
throw new AttachedNodeNotInGraphException(secondPort);
// We connect the edge and add it
edge.connect(firstPort, secondPort);
this.edges.add(edge);
}
来自Edge
类:
public void connect (Port portOne, Port portTwo)
throws InvalidMethodCallException, WrongPortsDirectionsException, WrongPortsSizesException, GraphDescriptionModelException
{
// We check the reserved method call
Class callerClass = Reflection.getCallerClass(2);
if (!Graph.class.isAssignableFrom(callerClass))
throw new InvalidMethodCallException("Edge.connect", Graph.class.getName(), callerClass.getName());
// We check the directions of the ports
if ((portOne.isInputPort() && !portOne.isInputOutputPort() && !portTwo.isOutputPort())
|| (portTwo.isInputPort() && !portTwo.isInputOutputPort() && !portOne.isOutputPort())
|| (portOne.isOutputPort() && !portOne.isInputOutputPort() && !portTwo.isInputPort())
|| (portTwo.isOutputPort() && !portTwo.isInputOutputPort() && !portOne.isInputPort()))
throw new WrongPortsDirectionsException(portOne, portTwo);
// We check the sizes of the ports
if ((portOne.isInputPort() && !portOne.isInputOutputPort() && portOne.getSize() < portTwo.getSize())
|| (portTwo.isInputPort() && !portTwo.isInputOutputPort() && portTwo.getSize() < portOne.getSize())
|| (portOne.isOutputPort() && !portOne.isInputOutputPort() && portOne.getSize() > portTwo.getSize())
|| (portTwo.isOutputPort() && !portTwo.isInputOutputPort() && portTwo.getSize() > portOne.getSize())
|| (portOne.isInputOutputPort() && portTwo.isInputOutputPort() && portOne.getSize() != portTwo.getSize()))
throw new WrongPortsSizesException(portOne, portTwo);
// We avoid similar edges
for (Edge portOneEdge : portOne.getConnectedEdges())
if (portOneEdge.getOppositePort(portOne).equals(portTwo))
throw new SimilarEdgeAlreadyExistsException(portOneEdge);
// Ports' attributes
portOne.getConnectedEdges().add(this);
portTwo.getConnectedEdges().add(this);
// Attributes
this.portOne = portOne;
this.portTwo = portTwo;
}
希望这有助于理解问题。
编辑:谢谢大家!
最佳答案
您可以查看Annotation Processing Tool 。它允许您进行编译时注释并在编译代码时生成警告/错误。
关于Java:如何在编译时找到方法的调用者?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16711140/