Search in sources :

Example 1 with InvalidCommandLineException

use of com.zwitserloot.cmdreader.InvalidCommandLineException in project lombok by rzwitserloot.

the class PoolConstantsApp method runApp.

@Override
public int runApp(List<String> raw) throws Exception {
    CmdReader<CmdArgs> reader = CmdReader.of(CmdArgs.class);
    CmdArgs args;
    try {
        args = reader.make(raw.toArray(new String[0]));
        if (args.help) {
            System.out.println(reader.generateCommandLineHelp("java -jar lombok.jar -printpool"));
            return 0;
        }
    } catch (InvalidCommandLineException e) {
        System.err.println(e.getMessage());
        System.err.println(reader.generateCommandLineHelp("java -jar lombok.jar -printpool"));
        return 1;
    }
    List<File> filesToProcess = PostCompilerApp.cmdArgsToFiles(args.classFiles);
    int filesVisited = 0;
    boolean moreThanOne = filesToProcess.size() > 1;
    for (File file : filesToProcess) {
        if (!file.exists() || !file.isFile()) {
            System.out.printf("Cannot find file '%s'\n", file.getAbsolutePath());
            continue;
        }
        filesVisited++;
        if (moreThanOne)
            System.out.printf("Processing '%s'\n", file.getAbsolutePath());
        System.out.println(new ClassFileMetaData(PostCompilerApp.readFile(file)).poolContent());
    }
    if (moreThanOne)
        System.out.printf("Total files visited: %d\n", filesVisited);
    return filesVisited == 0 ? 1 : 0;
}
Also used : InvalidCommandLineException(com.zwitserloot.cmdreader.InvalidCommandLineException) File(java.io.File)

Example 2 with InvalidCommandLineException

use of com.zwitserloot.cmdreader.InvalidCommandLineException in project lombok by rzwitserloot.

the class PostCompilerApp method runApp.

@Override
public int runApp(List<String> raw) throws Exception {
    CmdReader<CmdArgs> reader = CmdReader.of(CmdArgs.class);
    CmdArgs args;
    try {
        args = reader.make(raw.toArray(new String[0]));
        if (args.help) {
            System.out.println(reader.generateCommandLineHelp("java -jar lombok.jar post-compile"));
            return 0;
        }
    } catch (InvalidCommandLineException e) {
        System.err.println(e.getMessage());
        System.err.println(reader.generateCommandLineHelp("java -jar lombok.jar post-compile"));
        return 1;
    }
    int filesVisited = 0, filesTouched = 0;
    for (File file : cmdArgsToFiles(args.classFiles)) {
        if (!file.exists() || !file.isFile()) {
            System.out.printf("Cannot find file '%s'\n", file);
            continue;
        }
        filesVisited++;
        if (args.verbose)
            System.out.println("Processing " + file.getAbsolutePath());
        byte[] original = readFile(file);
        byte[] clone = original.clone();
        byte[] transformed = PostCompiler.applyTransformations(clone, file.toString(), DiagnosticsReceiver.CONSOLE);
        if (clone != transformed && !Arrays.equals(original, transformed)) {
            filesTouched++;
            if (args.verbose)
                System.out.println("Rewriting " + file.getAbsolutePath());
            writeFile(file, transformed);
        }
    }
    if (args.verbose) {
        System.out.printf("Total files visited: %d total files changed: %d\n", filesVisited, filesTouched);
    }
    return filesVisited == 0 ? 1 : 0;
}
Also used : InvalidCommandLineException(com.zwitserloot.cmdreader.InvalidCommandLineException) File(java.io.File)

Example 3 with InvalidCommandLineException

use of com.zwitserloot.cmdreader.InvalidCommandLineException in project lombok by rzwitserloot.

the class CreateLombokRuntimeApp method runApp.

@Override
public int runApp(List<String> rawArgs) throws Exception {
    CmdReader<CmdArgs> reader = CmdReader.of(CmdArgs.class);
    CmdArgs args;
    try {
        args = reader.make(rawArgs.toArray(new String[0]));
    } catch (InvalidCommandLineException e) {
        printHelp(reader, e.getMessage(), System.err);
        return 1;
    }
    if (args.help) {
        printHelp(reader, null, System.out);
        return 0;
    }
    initializeInfoObjects();
    if (args.print) {
        printRuntimeDependents();
    }
    int errCode = 0;
    if (args.create) {
        File out = new File("./lombok-runtime.jar");
        if (args.output != null) {
            out = new File(args.output);
            if (out.isDirectory())
                out = new File(out, "lombok-runtime.jar");
        }
        try {
            errCode = writeRuntimeJar(out);
        } catch (Exception e) {
            System.err.println("ERROR: Creating " + canonical(out) + " failed: ");
            e.printStackTrace();
            return 1;
        }
    }
    return errCode;
}
Also used : InvalidCommandLineException(com.zwitserloot.cmdreader.InvalidCommandLineException) File(java.io.File) IOException(java.io.IOException) InvalidCommandLineException(com.zwitserloot.cmdreader.InvalidCommandLineException)

Example 4 with InvalidCommandLineException

use of com.zwitserloot.cmdreader.InvalidCommandLineException in project lombok by rzwitserloot.

the class Delombok method main.

public static void main(String[] rawArgs) {
    CmdReader<CmdArgs> reader = CmdReader.of(CmdArgs.class);
    CmdArgs args;
    try {
        args = reader.make(rawArgs);
    } catch (InvalidCommandLineException e) {
        System.err.println("ERROR: " + e.getMessage());
        System.err.println(reader.generateCommandLineHelp("delombok"));
        System.exit(1);
        return;
    }
    if (args.help || (args.input.isEmpty() && !args.formatHelp)) {
        if (!args.help)
            System.err.println("ERROR: no files or directories to delombok specified.");
        System.err.println(reader.generateCommandLineHelp("delombok"));
        System.exit(args.help ? 0 : 1);
        return;
    }
    Delombok delombok = new Delombok();
    if (args.quiet)
        delombok.setFeedback(new PrintStream(new OutputStream() {

            @Override
            public void write(int b) throws IOException {
            //dummy - do nothing.
            }
        }));
    if (args.formatHelp) {
        System.out.println("Available format keys (to use, -f key:value -f key2:value2 -f ... ):");
        for (Map.Entry<String, String> e : FormatPreferences.getKeysAndDescriptions().entrySet()) {
            System.out.print("  ");
            System.out.print(e.getKey());
            System.out.println(":");
            System.out.println(indentAndWordbreak(e.getValue(), 4, 70));
        }
        System.out.println("Example: -f indent:4 -f emptyLines:indent");
        System.out.println("The '-f pretty' option is shorthand for '-f suppressWarnings:skip -f generated:skip -f danceAroundIdeChecks:skip -f generateDelombokComment:skip -f javaLangAsFQN:skip'");
        System.exit(0);
        return;
    }
    try {
        delombok.setFormatPreferences(formatOptionsToMap(args.format));
    } catch (InvalidFormatOptionException e) {
        System.out.println(e.getMessage() + " Try --format-help.");
        System.exit(1);
        return;
    }
    if (args.encoding != null) {
        try {
            delombok.setCharset(args.encoding);
        } catch (UnsupportedCharsetException e) {
            System.err.println("ERROR: Not a known charset: " + args.encoding);
            System.exit(1);
            return;
        }
    }
    if (args.verbose)
        delombok.setVerbose(true);
    if (args.nocopy)
        delombok.setNoCopy(true);
    if (args.print) {
        delombok.setOutputToStandardOut();
    } else {
        delombok.setOutput(new File(args.target));
    }
    if (args.classpath != null)
        delombok.setClasspath(args.classpath);
    if (args.sourcepath != null)
        delombok.setSourcepath(args.sourcepath);
    if (args.bootclasspath != null)
        delombok.setBootclasspath(args.bootclasspath);
    try {
        for (String in : args.input) {
            File f = new File(in).getAbsoluteFile();
            if (f.isFile()) {
                delombok.addFile(f.getParentFile(), f.getName());
            } else if (f.isDirectory()) {
                delombok.addDirectory(f);
            } else if (!f.exists()) {
                if (!args.quiet)
                    System.err.println("WARNING: does not exist - skipping: " + f);
            } else {
                if (!args.quiet)
                    System.err.println("WARNING: not a standard file or directory - skipping: " + f);
            }
        }
        delombok.delombok();
    } catch (Exception e) {
        if (!args.quiet) {
            String msg = e.getMessage();
            if (msg != null && msg.startsWith("DELOMBOK: "))
                System.err.println(msg.substring("DELOMBOK: ".length()));
            else {
                e.printStackTrace();
            }
            System.exit(1);
            return;
        }
    }
}
Also used : PrintStream(java.io.PrintStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) InvalidCommandLineException(com.zwitserloot.cmdreader.InvalidCommandLineException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) InvalidCommandLineException(com.zwitserloot.cmdreader.InvalidCommandLineException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) File(java.io.File)

Aggregations

InvalidCommandLineException (com.zwitserloot.cmdreader.InvalidCommandLineException)4 File (java.io.File)4 IOException (java.io.IOException)2 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 PrintStream (java.io.PrintStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)1 HashMap (java.util.HashMap)1 IdentityHashMap (java.util.IdentityHashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1