Results 1 to 7 of 7

Thread: PMD with java

  1. #1
    swati.jyoti is offline Member
    Join Date
    Mar 2009
    Posts
    42
    Rep Power
    0

    Question PMD with java

    final String[] sourceFieldNames = SourceFieldNames[i].split(COMMA);

    It gave a warning for this...
    Avoid using final local variables ,turn them into fields...

    but wen i used without final it warned by saying local variables should be assigned final..

    what to do??

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default

    Hi Swathi,
    We can't give solutions with assumptions.Even if we give,it will not be perfect.Please send the code.This one line is not understandable.
    Gothru this link
    Java Basics: Methods 4 - Local variables
    -Regards
    Ramya
    Last edited by RamyaSivakanth; 07-01-2009 at 12:11 PM.
    Ramya:cool:

  3. #3
    swati.jyoti is offline Member
    Join Date
    Mar 2009
    Posts
    42
    Rep Power
    0

    Default re: Pmd with java

    boolean isTimeStampValid(String timeStamp) {
    final String timeStamp1 = timeStamp.trim();
    String[] timeArray = null;
    if (timeStamp1 == null) {
    return false;
    }
    timeArray = timeStamp1.split(SPLIT_DOT);
    if ((timeArray.length )!= 2 &&( timeArray.length != 3)) {
    return false;
    }
    return true;
    }

    ok dats the code...
    If i dont use final in timestamp1 then it says to use it,n if i use it then warns me to avoid it...(checking with PMD)

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default

    Hi Swathi,
    I have just run the piece of code like this below with my own param value.
    I have not got any error.Might be u need to verify the calling method code.

    I have just executed with some value like this below.I have not got any compiletime/run time error.

    Java Code:
    class TestTime 
    {
    final String SPLIT_DOT =":";
    boolean isTimeStampValid(String timeStamp) {
    final String timeStamp1 = timeStamp.trim();
    String[] timeArray = null;
    if (timeStamp1 == null) {
    return false;
    }
    timeArray = timeStamp1.split(".");
    if ((timeArray.length )!= 2 &&( timeArray.length != 3)) {
    return false;
    }
    return true;
    }
    
    	public static void main(String[] args) 
    	{
    		new TestTime().isTimeStampValid("00.00");
    	}
    }
    Ramya:cool:

  5. #5
    corlettk is offline Member
    Join Date
    Apr 2009
    Location
    Brisbane
    Posts
    86
    Rep Power
    0

    Default

    Quote Originally Posted by RamyaSivakanth View Post
    Hi Swathi, I have just run the piece of code like this below with my own param value....
    My 2c...

    Java Code:
    package forums;
    
    public class TimeStamp 
    {
      private final String fieldSeperator;
    
      public TimeStamp() {
        this(":");
      }
      public TimeStamp(String fieldSeperator) {
        this.fieldSeperator = fieldSeperator;
      }
    
      public boolean isValid(String timeStamp) {
        if ( isNullOrEmpty(timeStamp) ) {
          return false;
        }
        final String[] timeArray = timeStamp.trim().split(fieldSeperator);
        return timeArray.length==2 || timeArray.length==3;
      }
    
      private static boolean isNullOrEmpty(String s) {
        return s==null || s.trim().length()==0;
      }
    
      public static String isOrIsNot(boolean b) {
        return b ? "is" : "is not";
      }
    
      public static void main(String[] args) {
        System.out.println("00.00 " +isOrIsNot(new TimeStamp().isValid("00.00"))+" valid");
        System.out.println("00.00 " +isOrIsNot(new TimeStamp("\\.").isValid("00.00"))+" valid");
      }
    
    }

  6. #6
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    283
    Rep Power
    7

    Default

    There is nothing wrong with this variable declaration. But probably what Swati means to say is when the code is verified using PMD , it gives warning in either case. Not sure but this might be an issue with the PMD tool itself.

  7. #7
    swati.jyoti is offline Member
    Join Date
    Mar 2009
    Posts
    42
    Rep Power
    0

    Default Re:

    R u using PMD 4 checking the code?U wl then get the warning else u will not get...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •