Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Java method extraction (signature + body) #16825

Answered by jketema
alibrahimzada asked this question in Q&A
Discussion options

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?

You must be logged in to vote

I attempted to do this a few days back. However, there are a lot of patterns to consider a stopping criteria:

  1. Stop search backwards when there is a } for end of an earlier method
  2. 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

Comment options

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.

You must be logged in to vote
10 replies
Comment options

Yes.

Comment options

Thanks. What I would do is:

  1. Write a query that outputs the start location of the method and the end location of its body
  2. Retrieve the source file (I assume you have those somewhere, otherwise the source is included in the database in a file called src.zip)
  3. Open the source file and from the starting position you got from the query search backward to find the start of the method declaration
  4. Output everything between the position the found and the end of the body (as output by the query)
Comment options

I attempted to do this a few days back. However, there are a lot of patterns to consider a stopping criteria:

  1. Stop search backwards when there is a } for end of an earlier method
  2. 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.

Comment options

I attempted to do this a few days back. However, there are a lot of patterns to consider a stopping criteria:

  1. Stop search backwards when there is a } for end of an earlier method
  2. 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.

Answer selected by alibrahimzada
Comment options

Thank you so much, @jketema.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /