JavaParser: How to retrieve all ParentNode names from MethodDeclaration? - java

I am using JavaParser library (https://github.com/javaparser/javaparser) for parsing Java method declarations. I want to identify different method declarations from different packages, classes, scopes, etc. so that I can precisely identify every method declarations.
For example:
Here is a Java file called MainClass.java:
package com.company.packA.packB;
public class MainClass {
void doTask(int x, int y) {
//...
}
private class InnerClass {
void doTask(int x, int y) {
//...
}
}
}
class AnotherClassSameFile {
void doTask(int x, int y) {
//...
}
}
Notice that the above example contains three void doTask(int x, int y) methods:
com.company.packA.packB→MainClass→doTask(int x, int y)
com.company.packA.packB→MainClass→InnerClass→doTask(int x, int y)
com.company.packA.packB→AnotherClassSameFile→doTask(int x, int y)
To identify different method declarations with the same method signatures, I need to traverse all parent nodes until the root node.
So far I have tried this code (simplified) using JavaParser library:
class MethodStruct { // the second example will be:
String parentNodes; // com.company.packA.packB#MainClass#InnerClass
String returnType; // void
String methodName; // doTask
String parameters; // int,int
}
class JavaParserTest {
// this is the method to be called from outside
static List<MethodStruct> getMethodStructs(Reader reader) {
CompilationUnit cu = JavaParser.parse(reader);
List<MethodStruct> methodStructs = new LinkedList<>();
cu.accept(new MethodVisitor(), methodStructs);
return methodStructs;
}
static class MethodVisitor extends VoidVisitorAdapter<List<MethodStruct>> {
#Override
public void visit(MethodDeclaration methodDeclaration, List<MethodStruct> methodStructs) {
super.visit(methodDeclaration, methodStructs);
// adding individual methodStruct into the list
methodStructs.add(getMethodStruct(methodDeclaration));
}
static MethodStruct getMethodStruct(MethodDeclaration methodDeclaration) {
return new MethodStruct(
getParents(methodDeclaration),
methodDeclaration.getTypeAsString(),
methodDeclaration.getNameAsString(),
getParameterAsString(methodDeclaration.getParameters()));
}
// it is the method to be concerned for my question
static String getParents(MethodDeclaration methodDeclaration) {
StringBuilder parents = new StringBuilder();
Node currentNode = methodDeclaration;
while (currentNode.getParentNode().isPresent()) {
// goto parent node
currentNode = currentNode.getParentNode().get();
//TODO: I'm stuck here. Help me please!
//TODO: How to identify each node whether
// it is a package, innerClass, etc?
}
// convert StringBuilder into String and return the String
return parents.toString();
}
static String getParameterAsString(NodeList<Parameter> parameters) {
// easy task! convert parameter string list
// into a single string (comma separated)
}
}
}
I am facing difficulties in defining my getParents(MethodDeclaration methodDeclaration) method. How can I solve it (i.e., identify every parent nodes)? I can't find any useful method of Node class for my goal. I might have missed something in JavaParser library.

You should try to use walk method to find all scopes of concrete method declaration:
static String getParents(fina MethodDeclaration methodDeclaration) {
final StringBuilder parents = new StringBuilder();
methodDeclaration.walk(Node.TreeTraversal.PARENTS, node -> {
if (node instanceof ClassOrInterfaceDeclaration) {
path.insert(0, ((ClassOrInterfaceDeclaration) node).getNameAsString());
path.insert(0, '$');
}
if (node instanceof ObjectCreationExpr) {
path.insert(0, ((ObjectCreationExpr) node).getType().getNameAsString());
path.insert(0, '$');
}
if (node instanceof MethodDeclaration) {
path.insert(0, ((MethodDeclaration) node).getNameAsString());
path.insert(0, '#');
}
if (node instanceof CompilationUnit) {
final Optional<PackageDeclaration> pkg = ((CompilationUnit) node).getPackageDeclaration();
if (pkg.isPresent()) {
path.replace(0, 1, ".");
path.insert(0, pkg.get().getNameAsString());
}
}
});
// convert StringBuilder into String and return the String
return parents.toString();
}

Related

Converting tree representing math expression to a string without redundant parentheses

I want to convert a tree that represents a math expression into the actual math expression (a string like "a+b/c")
The tree representation is the simplest you could imagine:
A+B/C would be this tree:
OperationNode(+, A, OperationNode(/, B, C))
And (A+B)/C would be this tree:
OperationNode(/, OperationNode(+, A, B), C)
In order to convert the tree into the string, I'm using the Visitor pattern. The problem comes with parentheses.
My current Visitor implementation ALWAYS adds parentheses to the nodes, so every tree I generate turns into a string like this:
(((A+B)+C)+D)
Notice the redundant parentheses.
So the question is: how could I make my Visitor generate the string with no redundant parentheses?
As NelFeal wrote while walking the tree you just need to check if precedence of suboperation less than a precedence of the current operation.
I implemented visitor pattern for you, hope it helps.
enum Operation
{
Add,
Multiply,
Power,
UnaryMinus,
None,
}
static class OperationExtensions
{
public static string ToFriendlyString(this Operation me)
{
switch (me)
{
case Operation.None:
return "";
case Operation.Add:
return "+";
case Operation.Multiply:
return "*";
case Operation.Power:
return "^";
case Operation.UnaryMinus:
return "-";
default:
throw new ArgumentException();
}
}
}
class OperationNode
{
public Operation Op;
public OperationNode(Operation op)
{
Op = op;
}
}
interface IVisitor
{
void Visit(OperationNodeLeaf node);
void Visit(OperationNode1 node);
void Visit(OperationNode2 node);
}
sealed class Visitor : IVisitor
{
public string Text { get; set; }
private void Enclose(OperationNode subNode, Operation op)
{
if (subNode.Op < op)
{
Text = Text + "(";
Visit((dynamic)subNode);
Text = Text + ")";
}
else
{
Visit((dynamic)subNode);
}
}
public void Visit(OperationNodeLeaf node)
{
Text = Text + node.Op.ToFriendlyString();
Text = Text + node.Value.ToString();
}
public void Visit(OperationNode1 node)
{
Text = Text + node.Op.ToFriendlyString();
Enclose(node.SubNode, node.Op);
}
public void Visit(OperationNode2 node)
{
Enclose(node.LeftSubNode, node.Op);
Text = Text + node.Op.ToFriendlyString();
Enclose(node.RightSubNode, node.Op);
}
}
class OperationNodeLeaf : OperationNode
{
public int Value;
public OperationNodeLeaf(int v, Operation op = Operation.None) : base(op)
{
Value = v;
}
void Accept(IVisitor v)
{
v.Visit(this);
}
}
class OperationNode1 : OperationNode
{
public OperationNode SubNode;
public OperationNode1(OperationNode sn, Operation op) : base(op)
{
SubNode = sn;
}
void Accept(IVisitor v)
{
v.Visit(this);
}
}
class OperationNode2 : OperationNode
{
public OperationNode LeftSubNode;
public OperationNode RightSubNode;
public OperationNode2(OperationNode lsn, OperationNode rsn, Operation op) : base(op)
{
LeftSubNode = lsn;
RightSubNode = rsn;
}
void Accept(IVisitor v)
{
v.Visit(this);
}
}
class Program
{
static void Main(string[] args)
{
var tree =
new OperationNode2(
new OperationNode2(
new OperationNode2(new OperationNodeLeaf(5), new OperationNodeLeaf(6), Operation.Add),
new OperationNode2(new OperationNodeLeaf(5), new OperationNodeLeaf(6), Operation.Multiply),
Operation.Power
),
new OperationNode2(
new OperationNode2(new OperationNodeLeaf(1), new OperationNodeLeaf(2), Operation.Multiply),
new OperationNode1(new OperationNodeLeaf(7, Operation.None), Operation.UnaryMinus),
Operation.Add
),
Operation.Multiply
);
var visitor = new Visitor();
visitor.Visit(tree);
System.Diagnostics.Debug.WriteLine(visitor.Text);
}
}
(5+6)^(5*6)*(1*2+-7)
You need a operator precedence table. Simply assign precedence values to every operator you support (and maybe to the topmost no-op that gives you the outermost pair of parentheses). Then, for each operation node, if its operation has a higher precedence than the parent node operation, you don't need parentheses.

Java Binary Trees/Visitor Pattern

So I can really get this I was wondering if anyone could help me out, the object of this program is to use the visitor pattern to generate a list of strings from the name of people in a given binary tree, if not really sure how to go about using an append function to do so. How would I append the person with its parents?
import tester.Tester;
//Representation for an ancestor tree
interface IAT {
<R> R accept(IATVisitor<R> visitor);
//Append two lists
IList<String> append(IList<String> l);
}
//-------------------------------------------------------------------------------------------------
//Unknown person
class Unknown implements IAT {
Unknown() {
}
public <R> R accept(IATVisitor<R> visitor) {
return visitor.visitUnknown(this);
}
//append two an unknown
public IList<String> append(IList<String> l) {
return l;
}
}
//-------------------------------------------------------------------------------------------------
//Representation for a person
class Person implements IAT {
String name;
int yob;
boolean isMale;
IAT mom;
IAT dad;
//Constructor
Person(String name, int yob, boolean isMale, IAT mom, IAT dad) {
this.name = name;
this.yob = yob;
this.isMale = isMale;
this.mom = mom;
this.dad = dad;
}
public <R> R accept(IATVisitor<R> visitor) {
return visitor.visitPerson(this);
}
//append parent and children of tree
public IList<String> append(IList<String> l) {
//
}
}
//-------------------------------------------------------------------------------------------------
interface IATVisitor<R> {
R visitUnknown(Unknown u);
R visitPerson(Person p);
}
//-------------------------------------------------------------------------------------------------
//IAT Visitor that returns a list of the names of all people
class IATVisitGetNames implements IATVisitor<IList<String>> {
public IList<String> visitUnknown(Unknown u) {
return new MT<String>();
}
public IList<String> visitPerson(Person p) {
return new Cons<String>(p.name, new MT<String>());
}
}
//Examples
class ExamplesIATV {
//persons
Unknown a = new Unknown();
Person ralph = new Person("Ralph", 1995, true, a, a);
Person kevin = new Person("Kevin", 1994, true, a , a);
Person julia = new Person("Julia", 1991, false, ralph, a);
Person lily = new Person("Lily", 1990, false, kevin, julia);
Person who = new Person("?", 1738, false, lily, a);
//Visitor
IATVisitor<IList<String>> byName = new IATVisitGetNames();
//test Vistior
boolean testGetNames(Tester t) {
return
t.checkExpect(who.accept(byName), new MT<String>());
}
}
First - you want collect all names from tree. You need traversal function, for example:
public void traverse(Node root) {
//do somesing with node
System.out.println(root.value);
if (root.left != null) {
traverse(root.left);
}
if (root.right != null) {
traverse(root.right);
}
}
Second - you want to use Visitor pattern. How wikipedia says:
Visitor - is a way of separating an algorithm from an object structure
on which it operates.
So visitor is not suitable for travers/itereate logic. With visitor we can only incapsulate some logic on node:
public void traverseWithVisitor(Node root, IVisitor v) {
root.accept(v);
if (root.left != null) {
traverseWithVisitor(root.left, v);
}
if (root.right != null) {
traverseWithVisitor(root.right, v);
}
}
Now incapsulate our collecting names logic in visitor:
class AggregateNamesVisitor implements IVisitor {
public List<Integer> names = new ArrayList<>();
#Override
public void visit(Node node) {
names.add(node.value);
}
}
We can use it like this:
AggregateNamesVisitor aggregateVisitor = new AggregateNamesVisitor();
traverseWithVisitor(root, aggregateVisitor);
aggregateVisitor.names.forEach(name -> System.out.print(" " + name));

Object oriented design, designing a tree

I'm creating a (atypical)tree in Java that will be composed of three classes: node, branch and leaf
Each node stores the branches it is connected to in a HashSet. The branch is supposed to lead to a descendent node or a leaf, but I'm not sure how to code that. Would I just have two separate variables, one Node and one Leaf in the branch class, along with two sets of getters and setters, even though I will never use both? Is there a best practice in this regard?
I was thinking maybe make node and leaf subclasses of the same superclass, but they have absolutely nothing in common in terms of code(i.e. different variable types, functions, etc.).
EDIT:
Node references branches and
each Branch references a Node or a Leaf
I'd probably go with something like this:
interface BranchDestination {
boolean isLeaf();
}
class Node implements BranchDestination {
private Set branches;
public boolean isLeaf() {
return false;
}
...
}
class Leaf implements BranchDestination {
public boolean isLeaf() {
return true;
}
...
}
class Branch {
BranchDestination destination;
...
}
I do like the idea of defining an interface for the leaf / node classes, and implement that interface in each. I would define a simple function in that interface (syntax might be wrong below, but it's pseduo-ish code):
interface BranchItem {
public object[] GetVals();
}
public class Branch
{
public BranchItem item;
}
public class Leaf implements BranchItem
{
private object myVal = <your data here>;
public object[] GetVals() {
return new object[] { myVal };
}
}
public class Node implements BranchItem
{
private myBranches[] = <List of Branches>;
public object[] GetVals() {
object[] myArray = new object[];
foreach (BranchItem b in myBranches)
{
myArray.addTo(b.item.GetVals());
}
return myArray;
}
}
When traversing your node, simply iterate over the Branches and call GetVals().
The Leaf class will simply returns it's stored value.
The Node Class will recursively loop over it's branches, calling GetVals() on each and add it to it's own returned array.
This is but a simple implementation. If you want sort order, handle collisions or duplicate data, or anything of that nature it could get more complicated.
Make the Leaf class with the basic information.
Make the Branch class which holds references to Leafs.
Make the Node class which holds references to Brahces.
Then try look up Recursion and how to use it to make such constructs :)
Here is my go at it. Though not very elegant, it gets the job done.
Here is the Leaf class:
public class Leaf {
private String text;
public Leaf(String text) {
this.text = text;
}
public String getText() {
return text;
}
public void setString(String newString) {
text = newString;
}
#Override
public String toString() {
return text;
}
}
And here is the Branch class:
public class Branch<T> {
private String text;
private HashSet<T> list;
public Branch(String text) {
this.text = text;
list = new HashSet<>();
}
public String getText() {
return text;
}
public void setText(String newText) {
text = newText;
}
public HashSet<T> getHashSet() {
return list;
}
public void setHashSet(HashSet<T> newList) {
list = newList;
}
public String getAllLeaves() {
StringBuilder sb = new StringBuilder();
sb.append(text).append("\n");
for(T t : list) {
sb.append("\t\t");
sb.append(t.toString()).append("\n");
}
return sb.toString();
}
#Override
public String toString() {
return text;
}
}
Lastly the Node class:
public class Node<T> {
private String text;
private HashSet<T> list;
public Node(String text) {
this.text = text;
list = new HashSet<>();
}
public String getText() {
return text;
}
public void setText(String newText) {
text = newText;
}
public HashSet<T> getHashSet() {
return list;
}
public void setHashSet(HashSet<T> newList) {
list = newList;
}
}
Little test program to try it out:
public class TreeConstruct {
public static void main(String[] args) {
Leaf l1 = new Leaf("Leaf 1");
Leaf l2 = new Leaf("Leaf 2");
Leaf l3 = new Leaf("Leaf 3");
Leaf l4 = new Leaf("Leaf 4");
Branch<Leaf> b1 = new Branch("Branch 1");
Branch<Leaf> b2 = new Branch("Branch 2");
Node<Branch> n1 = new Node("Node 1");
b1.getHashSet().add(l1);
b1.getHashSet().add(l2);
b1.getHashSet().add(l3);
b2.getHashSet().add(l4);
n1.getHashSet().add(b1);
n1.getHashSet().add(b2);
System.out.println(printNodeTree(n1));
}
public static String printNodeTree(Node<Branch> n) {
StringBuilder sb = new StringBuilder();
sb.append(n.getText()).append("\n");
for(Branch b : n.getHashSet()) {
sb.append("\t");
sb.append(b.getAllLeaves());
}
return sb.toString();
}
}
The output will be:
Node 1
Branch 1
Leaf 1
Leaf 3
Leaf 2
Branch 2
Leaf 4
Hope this helps!

Construct a tree structure from list of string paths

I have a collection of string paths like ["x1/x2/x3","x1/x2/x4","x1/x5"] in a list.
I need to construct a tree-like structure from this list which can be iterated to get a pretty printed tree.
like this
x1
/ \
x5 x2
/ \
x3 x4
Any ideas/suggestions?
I believe that the problem can be attacked first by processing the list of strings EDIT: The correct answer chosen was an elegant implementation, other suggestions were good too.
Follow an implementation of naive implementation of a visitable tree:
class Tree<T> implements Visitable<T> {
// NB: LinkedHashSet preserves insertion order
private final Set<Tree> children = new LinkedHashSet<Tree>();
private final T data;
Tree(T data) {
this.data = data;
}
void accept(Visitor<T> visitor) {
visitor.visitData(this, data);
for (Tree child : children) {
Visitor<T> childVisitor = visitor.visitTree(child);
child.accept(childVisitor);
}
}
Tree child(T data) {
for (Tree child: children ) {
if (child.data.equals(data)) {
return child;
}
}
return child(new Tree(data));
}
Tree child(Tree<T> child) {
children.add(child);
return child;
}
}
interfaces for Visitor Pattern:
interface Visitor<T> {
Visitor<T> visitTree(Tree<T> tree);
void visitData(Tree<T> parent, T data);
}
interface Visitable<T> {
void accept(Visitor<T> visitor);
}
sample implementation for Visitor Pattern:
class PrintIndentedVisitor implements Visitor<String> {
private final int indent;
PrintIndentedVisitor(int indent) {
this.indent = indent;
}
Visitor<String> visitTree(Tree<String> tree) {
return new IndentVisitor(indent + 2);
}
void visitData(Tree<String> parent, String data) {
for (int i = 0; i < indent; i++) { // TODO: naive implementation
System.out.print(" ");
}
System.out.println(data);
}
}
and finally (!!!) a simple test case:
Tree<String> forest = new Tree<String>("forest");
Tree<String> current = forest;
for (String tree : Arrays.asList("x1/x2/x3", "x1/x2/x4", "x1/x5")) {
Tree<String> root = current;
for (String data : tree.split("/")) {
current = current.child(data);
}
current = root;
}
forest.accept(new PrintIndentedVisitor(0));
output:
forest
x1
x2
x3
x4
x5
Just split each path by its delimiter and then add them to a tree structure one by one.
i.e. if 'x1' does not exist create this node, if it does exist go to it and check if there is a child 'x2' and so on...
I'd make the tree one string at a time.
Make an empty tree (which has a root node - I assume there could be a path like "x7/x8/x9").
Take the first string, add x1 to the root node, then x2 to x1, then x3 to x2.
Take the second string, see that x1 and x2 are already there, add x4 to x2.
Do this for every path you have.
Create an Object Node which contains a parent (Node) and a List of children (Node).
First split the string using ",". For every splitted string you split the string using "/".
Search for the first node identifier (e.g x1) in the root list.
If you can find it, use the node to find the next node identifier (e.g. x2).
If you can not find a node, add the node to the last node you was able to find in the existing lists.
After you have created the list structure, you can print the list to the screen. I would make it recursive.
NOT TESTED, just an animation
public void print(List nodes, int deep) {
if (nodes == null || nodes.isEmpty()) {
return;
}
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < deep; i++) {
buffer.append("---");
}
for (Iterator iterator = nodes.iterator(); iterator.hasNext();) {
Node node = (Node)iterator.next();
System.out.println(buffer.toString() + " " + node.getIdentifier());
print(node.getChildren(), deep + 1);
}
}
public class Menu {
private String path;
private List<Menu> children;
public Menu(String path) {
this.path = path;
children = new ArrayList<>();
}
public void addChild(Menu child) {
children.add(child);
}
public List<Menu> getChildren() {
return children;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public Menu getChild(String data) {
for (Menu n : children)
if (n.path.equals(data)) {return n;}
return null;
}
}
Tree builder class:
public class MenuTree {
private Menu root;
public MenuTree() {
root = new Menu("");
}
public void add(String str) {
Menu current = root;
StringTokenizer s = new StringTokenizer(str, "/");
while (s.hasMoreElements()) {
str = (String) s.nextElement();
Menu child = current.getChild(str);
if (child == null) {
current.addChild(new Menu(str));
child = current.getChild(str);
}
current = child;
}
}
public JSONObject toJSON() {
try {
return new JSONObject(new ObjectMapper().writeValueAsString(this.root));
} catch (JsonProcessingException e) {
return null;
}
}
}
Usage:
String slist[] = new String[]{
"mnt/sdcard/folder1/a/b/file1.file",
"mnt/sdcard/folder1/a/b/file2.file",
"D/a/b/c.file",
};
MenuTree t = new MenuTree();
for (String s : slist) {
t.add(s);
}
System.out.println(t.toJSON().toString());
JSONObject result:
{"path":"","children":[{"path":"mnt","children":[{"path":"sdcard","children":[{"path":"folder1","children":[{"path":"a","children":[{"path":"b","children":[{"path":"file1.file","children":[]},{"path":"file2.file","children":[]}]}]}]}]}]},{"path":"D","children":[{"path":"a","children":[{"path":"b","children":[{"path":"c.file","children":[]}]}]}]}]}
Make your tree for every string in array.
Just split path for '/' , check whether the node exists in your tree or not, if it exists then move on... otherwise create a new node and add this node in childrens of parent node.
Iterate using recursion.
Following is model for tree's node.
Class Node{
string name;
List<Node> childrens;
Node(string name){
this.name = name;
this.childrens = new List<Node>();
}
}
This is way how I am doing tree from path (folders) structure. Maybe should help someone with basic logic.
Node:
public class Node {
private String path;
private List<Node> children;
public Node(String path) {
this.path = path;
children = new ArrayList<>();
}
public String getName() {
return getName(path);
}
private String getName(String path) {
String[] split = path.split("\\\\");
return split[split.length - 1];
}
public void addChild(Node child) {
children.add(child);
}
public List<Node> getChildren() {
return children;
}
public String getPath() {
return path;
}
}
FilesTree:
public class FilesTree {
private static final Logger log = Logger.getLogger(FilesTree.class.getName());
private FilesTree() {}
private static void createTree(Node root, List<String> paths) {
for (String path : paths) {
addNode(root, Arrays.asList(path.split("\\\\")), "");
}
}
private static void addNode(Node node, List<String> path, String nodePath) {
if (!path.isEmpty()) {
nodePath = nodePath.equals("") ? path.get(0) : String.format("%s\\%s", nodePath, path.get(0));
}
if (node.getChildren().isEmpty() && path.size() == 1) {
node.addChild(new Node(nodePath));
} else if (!node.getChildren().isEmpty()) {
for (Node actual : node.getChildren()) {
if (actual.getName().equals(path.get(0))) {
addNode(actual, path.subList(1, path.size()), nodePath);
return;
}
}
node.addChild(new Node(nodePath));
} else {
log.info("Without children but with size: " + path.size());
}
}
}

Java: Why does this method have side effects?

I have a method that is producing side effects, even though certain variables are marked final. Why is this? Perhaps I am confused about what final does.
#Test
public void testSubGraph() {
WeightedGraph<String, DefaultWeightedEdge> g = generateSimpleCaseGraph();
Graph<String, DefaultWeightedEdge> sub = ChooseRoot.subgraphInDirection(g, "alpha", "l");
assertEquals(g, generateSimpleCaseGraph()); //fails
}
public static <V, E extends DefaultEdge> Graph<V, E> subgraphInDirection(final Graph<V, E> g, final V start, final V sink) {
Graph<V, E> sub = removeEdges(g, start, sink);
return removeUnconnectedNodes(sub, start);
}
private static <Vertex, Edge extends DefaultEdge> Graph<Vertex, Edge> removeEdges(final Graph<Vertex, Edge> g, Vertex start, Vertex sink) {
final Set<Edge> outEdges = new HashSet<Edge>(g.edgesOf(start));
boolean removedEdge;
for (Edge e : outEdges) {
if (! (g.getEdgeTarget(e).equals(sink) || g.getEdgeSource(e).equals(sink))) {
removedEdge = g.removeEdge(e);
assert removedEdge;
}
}
return g;
}
private static <Vertex, Edge> Graph<Vertex, Edge> removeUnconnectedNodes(Graph<Vertex, Edge> g, Vertex start) {
ConnectivityInspector<Vertex, Edge> conn = new ConnectivityInspector<Vertex, Edge>((UndirectedGraph<Vertex, Edge>) g);
boolean removedVertex;
final Set<Vertex> nodes = new HashSet<Vertex>(g.vertexSet());
for (Vertex v : nodes) {
if (! conn.pathExists(start, v)) {
removedVertex = g.removeVertex(v);
assert removedVertex;
}
}
return g;
}
The final modifier only means that the reference cannot be reassigned. It does not prevent the object's state from being modified.
EDIT: Just for Tom:
public void doSomething1(Object arg)
{
arg = new Object(); // OK.
}
public void doSomething2(final Object arg)
{
arg = new Object(); // Compile error.
}
In both cases you can invoke methods on the object pointed to by arg, including methods that modify its state.
Dan has the right answer on final. What you are after is more like const in C++, which Java does not have. You can simulate it by doing this:
public class Foo
{
protected int x;
public Foo(final int val)
{
x = val;
}
public int getX()
{
return (x);
}
}
public class MutableFoo
extends Foo
{
public MutableFoo(final int val)
{
super(val);
}
public void setX(final int val)
{
x = val;
}
}
then do:
void bar(final Foo foo)
{
foo.setX(5); // will not compile
}
void bar(final MutableFoo foo)
{
foo.setX(5); // will compile
}
Not pretty, but it works. The trick is to make sure that none of the methods in the parent class (Foo) make any changes to the instance variables - only MutableFoo can have methods that allow the state to change.
Of course the best thing to do, as much as possible, is to write immutable classes (make all the variables final) and do not call methods on instance/class variables that have side effects, so that things cannot change

Categories