jira.codehaus.org

  • Log In Access more options
    • Online Help
    • Keyboard Shortcuts
    • About JIRA
    • JIRA Credits
    • What?s New
  • Dashboards Access more options (Alt+d)
  • Projects Access more options (Alt+p)
  • Issues Access more options (Alt+i)
  • Sonar
  • SONAR-1369

CPD Plugin "Ignore Literals" causes "false" detections with big array of Strings

  • Log In
  • Views
    • XML
    • Word
    • Printable

Details

  • Type: Improvement Improvement
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 1.12
  • Fix Version/s: 2.11
  • Component/s: Duplications
  • Labels:
    None
  • Environment:
    Sonar runs on Red Hat Linux, connects to Oracle database, fetches code from CVS repository in a GForge installation.

Description

We set up the "Settings -> CPD Plugin -> Ignore Literals" to be "true".

Sonar correctly detected this code to be copy/paste (code paraphrased):
List peopleList = new ArrayList();
peopleList.add("a name");
peopleList.add("some name");
peopleList.add("another name");
peopleList.add("some other name");

was a copy of

List houseList = new ArrayList();
houseList.add("a house");
houseList.add("some house");
houseList.add("another house");
houseList.add("some other house");

For reasons beyond the scope of this bug, we need the strings to be in the code. So we changed the code into:
String [] people = new String[]{"a name", "some name", "another name", "some other name"};
and
String [] houses = new String[]{"a house", "some house", "another house", "some other house"};

The string arrays are also detected as duplicates. Although I technically understand why this is happening, I think this prevents developers from creating multiple large arrays with strings in any part of the code.

Is this the desired effect and if so, how do the people who wrote the CPD plugin suggest this is solved, given that the strings can not be externalized (the need to be part of the code, per requirement of the customer).

Issue Links

relates to

New Feature - A new feature of the product, which has yet to be developed. SONAR-1091 Copy Paste Code checking over all Projects

  • Major - Major loss of function.
  • Closed - The issue is considered finished, the resolution is correct. Issues which are not closed can be reopened.

Activity

Ascending order - Click to sort in descending order
  • All
  • Comments
  • Work Log
  • History
  • Activity
Hide
Permalink
Freddy Mallet added a comment - 27/Jan/10 9:45 AM

You're right Rolf.

When the "Ignore Literals" is set to false, the statement "peopleList.add("a name").add("another name")" is tokenized to get the following tokens : 'people', 'add', 'a name', 'add', 'another name'.

When the "Ignore Literals" is set to true, with the same statement, we get the following list of tokens : 'people', 'add', 'literal', 'add', 'literal'.

In fact 'literal' token should not be included in the token list and we should get 'people', 'add', 'add'.

if you agree, I'm going to change the type of this ticket from Bug to Improvement.

Show
Freddy Mallet added a comment - 27/Jan/10 9:45 AM You're right Rolf. When the "Ignore Literals" is set to false, the statement "peopleList.add("a name").add("another name")" is tokenized to get the following tokens : 'people', 'add', 'a name', 'add', 'another name'. When the "Ignore Literals" is set to true, with the same statement, we get the following list of tokens : 'people', 'add', 'literal', 'add', 'literal'. In fact 'literal' token should not be included in the token list and we should get 'people', 'add', 'add'. if you agree, I'm going to change the type of this ticket from Bug to Improvement.
Hide
Permalink
Rolf added a comment - 27/Jan/10 2:55 PM

You're right, moving from bug to improvement seems like a good idea. Thanks!

Show
Rolf added a comment - 27/Jan/10 2:55 PM You're right, moving from bug to improvement seems like a good idea. Thanks!
Hide
Permalink
Freddy Mallet added a comment - 24/Aug/11 12:09 PM

Hi Rolf, with our new clones detection mechanism (see SONAR-1091), initialization of big array of Strings or chaining calls of the same method a lot of times won't generate any duplications.

Show
Freddy Mallet added a comment - 24/Aug/11 12:09 PM Hi Rolf, with our new clones detection mechanism (see SONAR-1091), initialization of big array of Strings or chaining calls of the same method a lot of times won't generate any duplications.

People

  • Assignee:
    Freddy Mallet
    Reporter:
    Rolf
Vote (1)
Watch (2)

Dates

  • Created:
    26/Jan/10 9:06 AM
    Updated:
    09/Sep/11 9:21 AM
    Resolved:
    01/Sep/11 2:15 PM
  • Atlassian JIRA (v5.0.4#731-sha1:3aa7374)
  • Report a problem
  • Powered by a free Atlassian JIRA open source license for Codehaus. Try JIRA - bug tracking software for your team.