Giter Site home page Giter Site logo

diffj's Introduction

DiffJ

DiffJ compares Java files based on their code, without regard to formatting, organization, comments, or whitespace. Differences are reported as to the precise type of change.

DiffJ was designed for comparing code when refactoring and reformatting, with the differences narrowed to the extent possible, thus isolating changes.

SUMMARY

diffj [ options ] from-file to-file

FEATURES

Understanding Java content: DiffJ handles Java syntax (through version 1.6, at this writing), and compares code based on the actual Java code, not line-by-line.

For example, the following are considered equivalent:

    Integer[] ary  =  new Integer[ index( str, ch ) + str.length( ) ];
    
    Integer[] ary = new Integer[index(str, ch) + str.length()];

Ignoring whitespace. DiffJ ignores whitespace when comparing code, so it works well when, for example, tabs have been replaced with spaces, when indentation levels have changed, or when the end-of-line style has changed.

Ignoring order of declarations. DiffJ does not consider the organization of the code, such as the order of methods within a class. Thus if methods are reordered (perhaps by accessibility), DiffJ will not report that as being a difference, since the code itself has not changed. The order of declarations applies to all Java type declarations (inner classes, fields, methods, and constructors).

Ignoring order of import statements. As with declarations, DiffJ does not consider reordered import statements to be a change in the code.

Ignoring comments: DiffJ skips comments when comparing code. Thus comments that have been updated (perhaps as a result of running DoctorJ) will not be seen as a change.

Showing added and deleted declarations: DiffJ reports declarations that have been added and deleted, described by their Java type. For example:

d0/Removed.java <=> d1/Removed.java
2,3d1,4 method removed: contender(Double[], StringBuilder)
  class Removed {
!     public void contender(Double[] dary, StringBuilder sb) {
!     }
  
      public void contender() {
      }

Showing parameters: DiffJ reports constructor and methods parameters that have been added, deleted, and changed:

2c2 parameter name changed from idx to index
  class Changed {
!     void changed(int idx) {
      }
  }

  class Changed {
!     void changed(int index) {
      }
  }

Showing code differences narrowly: DiffJ reports the actual location (lines and columns) of code changes, and the output (in context and highlight mode) displays the exact change. Thus if a variable changes within a line of code, that variable itself is highlighted for the from- and to-files.

An example: the example below is contrived, in that differences are marked <like so;>. In the real world (where GitHub doesn't strip font colors) the output is in glorious color(s).

code>./example/d0/Changed.java <=> ./example/d1/Changed.java
9,28a21,22 method added: newMethod()
          this.size = size;
      }
  
!     public <void newMethod() {>
! <    }>
  }

10c8 implemented type changed from Map to List
  import java.io.File;
  
  abstract public class Changed 
!     implements <Map>
   {
  
    public void obsoleteMethod() {

  import java.util.List;
  import java.util.Map;
  
! public abstract class Changed implements <List> {
      /**
       * The maximum size of this object.
       */

13,14d8,23 method removed: obsoleteMethod()
      implements Map
   {
  
!   public <void obsoleteMethod() {>
! <  }>
  
    public Changed( int  s ) 
     {

16c17 parameter name changed from s to size
    public void obsoleteMethod() {
    }
  
!   public Changed( int  <s> ) 
     {
       size = s;
     }

      private final int size;
      private int index;
  
!     public Changed(int <size>) {
          this.size = size;
      }
  

18c18 code changed in Changed(int)
  
    public Changed( int  s ) 
     {
!      <size = s>;
     }
  
    private int size;

      private int index;
  
      public Changed(int size) {
!         <this.size = size>;
      }
  
      public void newMethod() {

21c14 modifier added: final
       size = s;
     }
  
!   <private> int size;
  
    // @todo - comment this field
    static final public int MAX_SIZE = 317;

       */
      public final static int MAX_SIZE = 317;
  
!     private <final> int size;
      private int index;
  
      public Changed(int size) {

26c15 variable changed from idx to index
    // @todo - comment this field
    static final public int MAX_SIZE = 317;
  
!   private int <idx>;
      
  }

      public final static int MAX_SIZE = 317;
  
      private final int size;
!     private int <index>;
  
      public Changed(int size) {
          this.size = size;

Configuration files: DiffJ looks for a configuration file as ~/.diffrc, and uses the name/value pairs there for its options. All options are supported, with the syntax "name: value". For boolean options (such as --highlight/--no-highlight), the value should be either true or false.

As an example, the following file sets DiffJ to use the source version as 1.6, and to show context with non-default colors:

context: true
highlight: true
from-color: bold blue on green
to-color: underscore magenta on cyan

OPTIONS

--brief   
    Display output in brief form.

--context
    Show context (non-brief form only).

--[no-]highlight
    Use colors (context output only).

--recurse
    Process directories recursively.

--from-source VERSION
    The Java source version of from-file (default: 1.5).

--to-source VERSION
    The Java source version of to-file (default: 1.5).

--from-color COLOR
    The text color of the from-file text (default: red).

--to-color COLOR
    The text color of the to-file text (default: yellow).

--source VERSION
    The Java source version of from-file and to-file (default: 1.5).

-u
    Output unified context. Unused; exists for compatibility with GNU diff.

-L NAME  --name NAME
    Set the first/second name to be displayed. This is useful for diffing
    with an external program, such as svn, where the file names are the temp
    files, and which passes in the real names as arguments.

--verbose
    Run in verbose mode (for debugging).

-h  --help
    Show help summary.

-v  --version
    Display the version.

EXAMPLES

% diffj old/Foo.java new/Foo.java
    Compares the two files, in the brief output format.

% diffj --context branches/3.1.4 trunk
    Compares the files in the two directories, reporting their differences
    with context.

% diffj --highlight -r branches/3.1.4 branches/3.1.5
    Compares the two directories recursively, reporting their differences
    with context, and changes highlighted.

% diffj --highlight --from-color "bold red on white" --to-color "cyan on black" ../v1.2.3 .
    Displays differences in the given colors.

% diffj --from-source 1.4 --to-source 1.6 -r ~myproj/old ~myproj/current
    Compares the code, using Java 1.4 as the source of the from-files, and
    1.6 as the source for the to-files.

DOWNLOAD

Source:

*Github*: https://github.com/jpace/diffj

Binaries

*Tarball*: 

*Zip*: 

*Debian*: 

diffj's People

Contributors

jpace avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

diffj's Issues

DiffJ compares without taking code location into consideration

DiffJ v1.6.4. When I compare the file to itself - DiffJ finds differences.

class MyClass {
    static {
        int a = 1;
    }
    static {
    	int b = 2;
    }
}

Output:

test.java <=> test.java
3c6 code changed in static block
<         int a = 1;
---
>     	int b = 2;

6c3 code changed in static block
<     	int b = 2;
---
>         int a = 1;

Related to #6

Java 7 and greater syntax not supported

org.incava.diffj.lang.DiffJException: Error parsing /tmp/Owner-8b625617cb24353ee906fab080c7677a6ec37870.java: Encountered " ">" "> ""

On that line:
this.pets = new HashSet<>();

Changes in explicit constructor invocation aren't detected

DiffJ v1.6.4 doesn't see a difference between the two classes:

public class MyClass {
    public int a;

    public MyClass(int a) {
        this.a = a;
    }

    public MyClass() {
        this(1);
    }
}

and

public class MyClass {
    public int a;

    public MyClass(int a) {
        this.a = a;
    }

    public MyClass() {
        this(2);
    }
}

The difference is: this(1); => this(2);

This is a real-life case where I experience the issue.

Differ crashing with java.lang.IndexOutOfBoundsException when adding line to constructor

Hello, please apply following, and run the test:
https://gist.github.com/shpelda/9177654

diffj will crash with

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at org.incava.diffj.code.StatementList.getRangeAt(StatementList.java:50)
at org.incava.diffj.code.StatementListDifferenceAdd.getFromRange(StatementListDifferenceAdd.java:22)
at org.incava.diffj.code.StatementListDifferenceDelta.process(StatementListDifferenceDelta.java:23)
at org.incava.diffj.code.StatementListDifference.execute(StatementListDifference.java:18)
at org.incava.diffj.code.Block.compareCodeNew(Block.java:55)
at org.incava.diffj.code.Block.compareCode(Block.java:42)
at org.incava.diffj.function.Ctor.compareCode(Ctor.java:45)
at org.incava.diffj.function.Ctor.diff(Ctor.java:31)

Note that assertion in junit is invalid. i just need a way to reproduce the crash.

Java Varargs syntax not supported

Hello,
Code that has Overloaded Methods and two of the method have same parameters, but one has varargs is causing inconsistencies on diffj's output, here is an example:

When running this class against itself on diffj:

class Test {
	public void test(String message) {
		System.out.println(message);
	}

	public void test(String ...message) {
		for (int i = 0; i < message.length; i++) {
			System.out.println(message[i]);
		}
	}
}

the following output is returned:

Test.java <=> Test.java
3c7,8 code changed in test(String)
< 		System.out.println(message);
---
> 		for (int i = 0; i < message.length; i++) {
> 			System.out.println(message[i]);

4a9 code added in test(String)
< 	}
---
> 		}

7d3 code removed in test(String)
< 		for (int i = 0; i < message.length; i++) {
---
> 		System.out.println(message);

8d3 code removed in test(String)
< 			System.out.println(message[i]);
---
> 		System.out.println(message);

9d4 code removed in test(String)
< 		}
---
> 	}

I think this is happening because diffj is considering those two methods are same, and it is comparing them with each other.

Annotation at method's parameter level causing ClassCastException

I am trying to compare two versions of a class (attached), and a ClassCastException is thrown.
Cannot cast 'net.sourceforge.pmd.lang.java.ast.ASTType' to 'net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId'

Depuring the application, I'm supposing it is an error in handling annotations at the method parameter level (see line 397): public void prepare(@SuppressWarnings("rawtypes") Map<String, Object> topoConf, String schedulerLocalDir) {

Nimbus.java.txt
Nimbus.java.txt

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.