-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Java method extraction (signature + body) #16825
-
Suppose I have the following method that I want to extract (declaration + body):
111 public static <V, WE, G extends Graph<V, WE>>
112 SpanningWeightedEdgeMapperBuilder<V, WE> minimumSpanningTree(G graph) {
113 graph = checkNotNull(graph, "Minimum spanning tree can not be calculated on null graph");
114 return new DefaultSpanningWeightedEdgeMapperBuilder<V, WE>(graph);
115 }
I don't know how to write a query to return the location between 111 - 115. My current query returns two locations:
Method.getLocation(): this returns the location of minimumSpanningTree in line 112 (I consider this as my start line).
Method.getBody().getLocation(): this returns the location of the method body. I then consider the end line here as the end of method.
this assumption is mostly correct because the method name is rarely written in the second line of the method. thats why it fails for the example above because the start line misses public static <V, WE, G extends Graph<V, WE>>. Any solutions?
Beta Was this translation helpful? Give feedback.
All reactions
I attempted to do this a few days back. However, there are a lot of patterns to consider a stopping criteria:
- Stop search backwards when there is a
}for end of an earlier method- Stop search backwards where this is a
/*for comments
In my limited understanding of Java there should just be three cases: (1) you encounter a {, a }, or a ;. Comments you probably want to handle in a special way, because they can occur in awkward places, for example:
public /* ... */ static /* ... */ <V, WE, G extends Graph<V, WE>> SpanningWeightedEdgeMapperBuilder<V, WE> minimumSpanningTree(G graph) {
I was wondering if you can help with a query to generate text of a method from AST stored in DBs.
Replies: 1 comment 10 replies
-
Hi @alibrahimzada,
It's not possible to write a query like you want, because we do not track the locations of keywords like public and static.
Beta Was this translation helpful? Give feedback.
All reactions
-
Yes.
Beta Was this translation helpful? Give feedback.
All reactions
-
Thanks. What I would do is:
- Write a query that outputs the start location of the method and the end location of its body
- Retrieve the source file (I assume you have those somewhere, otherwise the source is included in the database in a file called
src.zip) - Open the source file and from the starting position you got from the query search backward to find the start of the method declaration
- Output everything between the position the found and the end of the body (as output by the query)
Beta Was this translation helpful? Give feedback.
All reactions
-
I attempted to do this a few days back. However, there are a lot of patterns to consider a stopping criteria:
- Stop search backwards when there is a
}for end of an earlier method - Stop search backwards where this is a
/*for comments
This search would not work if there is a field declaration right before a method. I am still thinking to generalize the stopping criterias in my search. Thank you.
I was wondering if you can help with a query to generate text of a method from AST stored in DBs.
Beta Was this translation helpful? Give feedback.
All reactions
-
I attempted to do this a few days back. However, there are a lot of patterns to consider a stopping criteria:
- Stop search backwards when there is a
}for end of an earlier method- Stop search backwards where this is a
/*for comments
In my limited understanding of Java there should just be three cases: (1) you encounter a {, a }, or a ;. Comments you probably want to handle in a special way, because they can occur in awkward places, for example:
public /* ... */ static /* ... */ <V, WE, G extends Graph<V, WE>> SpanningWeightedEdgeMapperBuilder<V, WE> minimumSpanningTree(G graph) {
I was wondering if you can help with a query to generate text of a method from AST stored in DBs.
I theory you can do this by recusing through the AST, yielding a string for each element, and combining the strings. However, I would strongly recommend against this, as it'll be a lot of work.
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
Thank you so much, @jketema.
Beta Was this translation helpful? Give feedback.