Skipper07
Skipper07

Reputation: 1281

How to find a syntactic features from a code fragment in a text file?

I have code fragments in a text file and have to find out what features (e.g is there a getter/mutator in a line) are associated with a specified line in a code fragment. For example, in a given code fragment

1 public HTMLEditor() {
2  //install the source configuration
3  setSourceViewerConfiguration(new HTMLConfiguration());
4  //install the document provider
5  setDocumentProvider(new HTMLDocumentProvider()); }
6 protected void createActions() {
7  super.createActions();
8 //... add other editor actions here
9 }}

Line 2,4 and 8 contains a comment and I can check it simply by writing following source code. Let's say

public boolean containsComment(String line) {
    // if line contains comment
    if(line.contains("//")){
        return true;            
    }else{
        return false;
    }
}

Similarly I have to check many such features by writing methods like containsMutator(string line){//some code here what is it i don't know}

Some main features are as under:

  1. Is there a mutator in a line (setter method)
  2. Accessor method (getter method)
  3. Does line contain a method
  4. contains an exception or not (Both try catch and extends choices)
  5. Contains an array
  6. Contains a reflection call
  7. Contains an anonymous class
  8. Call to Java SDK and so on

A line can contain more than one feature e.g line 5 in above snippet has a set method and new keyword too. Also, words such as new or get or etc should only be checked in source code and not in comments part. Same like this there are many such cases; Same as my containsComment which is easier to check other features such as exception checking, new or other keywords checking are little trickier and needs some complex code to handle.

What is the best way to check such features? Should I use regex or any other way?

NOTE: I have solved most of the features above with line.contains(). Please could someone help me in these i.e., i) Contains a reflection call,

ii) Contains an anonymous class

ObjectInterestedInFooObjects() {
   result = someLookup.lookupResult(Foo.class);
    result.addLookupListener(this);
     resultChanged(null);
 }

iii) how to check if method or class is referred and call to Java SDK is made.

Upvotes: 1

Views: 102

Answers (1)

Dawnkeeper
Dawnkeeper

Reputation: 2875

For some of this the approach with line.contains() will work.

But how do you e.g determine that something is a setter method? Just by the method signature? If not, you would have to parse the respective implementation (that you will most of the time not even have).

So do you want to say "this might be a setter" then you can play with contains() and regex. If you want to say "this IS a setter" you will need a lot more; starting with a clean concept of what makes up a special language feature.

Upvotes: 1

Related Questions