summaryrefslogtreecommitdiffstats
path: root/jill/src/com/android/jill/Options.java
blob: 7b41c578075050d660ba1f4fc260e6f9024ef5ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.jill;

import com.android.jill.utils.FileUtils;

import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.Option;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

/**
 * Jill command line options.
 */
public class Options {

  @CheckForNull
  @Argument(
      usage =
      "class files to be transformed contained recursively in directories or in a zip/jar file",
      metaVar = "FILE|DIRECTORY")
  private File binaryFile;

  @Option(name = "--verbose", usage = "enable verbosity (default: false)")
  private  boolean verbose = false;

  @Option(name = "-h", aliases = "--help", usage = "display help")
  protected boolean help;

  @CheckForNull
  @Option(name = "--output", usage = "output file", metaVar = "FILE")
  protected File outputDirOrZip;

  @Option(name = "--version", usage = "display version")
  protected boolean version;

  protected ContainerType container = ContainerType.ZIP;

  @Option(name = "--no-debug", usage = "disable debug info emission")
  protected boolean disableEmitDebugInfo = false;

  public void checkValidity() throws IllegalOptionsException {
    if (askForVersion() || askForHelp()) {
      return;
    }

    if (binaryFile != null) {
      checkBinaryFileValidity();
    } else {
      throw new IllegalOptionsException("Input file not provided");
    }
    if (outputDirOrZip != null) {
      if (container == ContainerType.DIR) {
        checkOutputDir();
      }
    } else {
      throw new IllegalOptionsException("Output directory not provided");
    }
  }

  public void setBinaryFile(@Nonnull File binaryFile) {
    this.binaryFile = binaryFile;
  }

  @Nonnull
  public File getOutputDir() {
    assert outputDirOrZip != null;
    return outputDirOrZip;
  }

  @Nonnull
  public File getBinaryFile() {
    assert binaryFile != null;
    return binaryFile;
  }

  public boolean askForVersion() {
    return version;
  }

  public boolean askForHelp() {
    return help;
  }

  public void setVerbose(boolean verbose) {
    this.verbose = verbose;
  }

  public boolean isVerbose() {
    return verbose;
  }

  public boolean isEmitDebugInfo() {
    return !disableEmitDebugInfo;
  }

  @Nonnull
  public ContainerType getContainer() {
    return container;
  }

  private void checkBinaryFileValidity() throws IllegalOptionsException {
    assert binaryFile != null;

    if (!binaryFile.exists()) {
      throw new IllegalOptionsException(binaryFile.getName() + " does not exists.");
    }

    if (binaryFile.isFile() && FileUtils.isJarFile(binaryFile)) {
      return;
    }

    if (binaryFile.isFile() && !FileUtils.isJavaBinaryFile(binaryFile)
        && !FileUtils.isJarFile(binaryFile)) {
      throw new IllegalOptionsException(binaryFile.getName() + " is not a supported binary file.");
    }

    List<File> binaryFiles = new ArrayList<File>();
    FileUtils.getJavaBinaryFiles(binaryFile, binaryFiles);
    if (binaryFiles.isEmpty()) {
      System.err.println("Warning: Folder " + binaryFile.getName()
          + " does not contains class files.");
    }
  }

  private void checkOutputDir() throws IllegalOptionsException {
    assert outputDirOrZip != null;

    if (!outputDirOrZip.exists()) {
      throw new IllegalOptionsException(outputDirOrZip.getName() + " does not exist.");
    }

    if (!outputDirOrZip.canRead() || !outputDirOrZip.canWrite()) {
      throw new IllegalOptionsException("The specified output folder '"
          + outputDirOrZip.getAbsolutePath()
          + "' for jack files cannot be written to or read from.");
    }
  }
}