Java Package Correction

0

I have a lot of java projects and I am often making new ones. I have a few helper classes (Vector, Logger, etc.) and it takes a lot of time correcting the first line to the correct package, I would really need a little program to help me fix it automatically.

Your program will first take the name of the package the files are moved to (eg. com.google.raycasting), after that each file with ****<FILENAME>**** between them. You read the input however you want (STDIN, program arguments, file, etc.).

When an EOF is reached, the program should output each file in the same way as in the input, but with the package declaration changed.

Example:

Input:

com.loovjo.tetris
****Vector.java****
package com.loovjo.someoldproject;
class Vector {
    public Vector (int x, int y) {
        System.out.println(x + ", " + y);
    }
}
****Logger.java****
package com.loovjo.someoldproject;
class Logger {
    public String name;
    public Logger(String name) {
        this.name = name;
    }
}

(Of course these aren't the real files.)

Output:

****Vector.java****
package com.loovjo.tetris;
class Vector {
    public Vector (int x, int y) {
        System.out.println(x + ", " + y);
    }
}
****Logger.java****
package com.loovjo.tetris;
class Vector {
    public Vector (int x, int y) {
        System.out.println(x + ", " + y);
    }
}

Further specification on Input/Output:

  • The package declaration will not always be on the first line, the input may contain some newlines/spaces before it. If this is the case, those characters must be there in the output.
  • The files in the input will always have correct syntax.

Remember, this is Code Golf, so the shortest code wins!

Loovjo

Posted 2015-04-18T16:53:13.540

Reputation: 7 357

or .. you can create those helper classes as separate jars and import them ;) – Optimizer – 2015-04-18T16:59:59.880

But where's the fun in that? – Loovjo – 2015-04-18T17:00:41.983

2Test cases should really cover the corners. How about a class which starts with two lines /*Hello*/package foo;//world (both with leading and trailing whitespace)? – Peter Taylor – 2015-04-18T19:07:06.753

I need more examples of input/output. – mbomb007 – 2015-04-18T19:50:01.207

Answers

1

Mathematica, 106 bytes

StringReplace[Rest@#~StringRiffle~"\n","package "~~__~~";"->"package "<>#[[1]]<>";"]&@StringSplit[#,"\n"]&

Works by splitting the string into its lines and returning all but the first line with any package declarations replaced with the new package.

LegionMammal978

Posted 2015-04-18T16:53:13.540

Reputation: 15 731

The Java spec doesn't require the package statement to begin at the beginning of a line, end at the end of a line, or to be on a single line. – Peter Taylor – 2015-04-18T19:05:46.590

@PeterTaylor The code rejoins them before doing the replacement. – LegionMammal978 – 2015-04-18T22:30:21.357

0

Java, 286 bytes

import java.util.*;class P{public static final void main(String[]a){Scanner s=new Scanner(System.in);for(String p="package "+s.nextLine(),e;(e=s.findWithinHorizon("(package[^;]+)|\"([^\"]|\\\\.)*\"|//.*|/\\*([^*]|\\*[^/])*|.|\n",0))!=null;System.out.print(s.match().start(1)>=0?p:e));}}

Take input from STDIN. Test case:

com.loovjo.tetris
****Foo.java****
    package something;
****Bar.java****
package
something.something
;
"package aaa;"
// package bbbb;
/**
 * package cccc;
 */
****Baz.java****
//comment:
package dddd;

Output:

****Foo.java****
    package com.loovjo.tetris;
****Bar.java****
package com.loovjo.tetris;
"package aaa;"
// package bbbb;
/**
 * package cccc;
 */
****Baz.java****
//comment:
package com.loovjo.tetris;

Cannot work with things like package/*;*/stuff, because regex.

kennytm

Posted 2015-04-18T16:53:13.540

Reputation: 6 847