001: /*******************************************************************************
002: * Copyright (c) 2006, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.compiler.apt.util;
011:
012: import java.util.HashSet;
013: import java.util.Set;
014: import java.util.StringTokenizer;
015:
016: /**
017: * Class used to handle options in the EclipseFileManager and the EclipseCompiler
018: */
019: public final class Options {
020: private static final Set<String> ZERO_ARGUMENT_OPTIONS;
021: private static final Set<String> ONE_ARGUMENT_OPTIONS;
022: private static final Set<String> FILE_MANAGER_OPTIONS;
023: static {
024: ZERO_ARGUMENT_OPTIONS = new HashSet<String>();
025: ZERO_ARGUMENT_OPTIONS.add("-progress");//$NON-NLS-1$
026: ZERO_ARGUMENT_OPTIONS.add("-proceedOnError");//$NON-NLS-1$
027: ZERO_ARGUMENT_OPTIONS.add("-time");//$NON-NLS-1$
028: ZERO_ARGUMENT_OPTIONS.add("-v");//$NON-NLS-1$
029: ZERO_ARGUMENT_OPTIONS.add("-version");//$NON-NLS-1$
030: ZERO_ARGUMENT_OPTIONS.add("-showversion");//$NON-NLS-1$
031: ZERO_ARGUMENT_OPTIONS.add("-deprecation");//$NON-NLS-1$
032: ZERO_ARGUMENT_OPTIONS.add("-help");//$NON-NLS-1$
033: ZERO_ARGUMENT_OPTIONS.add("-?");//$NON-NLS-1$
034: ZERO_ARGUMENT_OPTIONS.add("-help:warn");//$NON-NLS-1$
035: ZERO_ARGUMENT_OPTIONS.add("-?:warn");//$NON-NLS-1$
036: ZERO_ARGUMENT_OPTIONS.add("-noExit");//$NON-NLS-1$
037: ZERO_ARGUMENT_OPTIONS.add("-verbose");//$NON-NLS-1$
038: ZERO_ARGUMENT_OPTIONS.add("-referenceInfo");//$NON-NLS-1$
039: ZERO_ARGUMENT_OPTIONS.add("-inlineJSR");//$NON-NLS-1$
040: ZERO_ARGUMENT_OPTIONS.add("-g");//$NON-NLS-1$
041: ZERO_ARGUMENT_OPTIONS.add("-g:none");//$NON-NLS-1$
042: ZERO_ARGUMENT_OPTIONS.add("-nowarn");//$NON-NLS-1$
043: ZERO_ARGUMENT_OPTIONS.add("-warn:none");//$NON-NLS-1$
044: ZERO_ARGUMENT_OPTIONS.add("-preserveAllLocals");//$NON-NLS-1$
045: ZERO_ARGUMENT_OPTIONS.add("-enableJavadoc");//$NON-NLS-1$
046: ZERO_ARGUMENT_OPTIONS.add("-Xemacs");//$NON-NLS-1$
047: ZERO_ARGUMENT_OPTIONS.add("-X");//$NON-NLS-1$
048: ZERO_ARGUMENT_OPTIONS.add("-O");//$NON-NLS-1$
049: ZERO_ARGUMENT_OPTIONS.add("-1.3");//$NON-NLS-1$
050: ZERO_ARGUMENT_OPTIONS.add("-1.4");//$NON-NLS-1$
051: ZERO_ARGUMENT_OPTIONS.add("-1.5");//$NON-NLS-1$
052: ZERO_ARGUMENT_OPTIONS.add("-5");//$NON-NLS-1$
053: ZERO_ARGUMENT_OPTIONS.add("-5.0");//$NON-NLS-1$
054: ZERO_ARGUMENT_OPTIONS.add("-1.6");//$NON-NLS-1$
055: ZERO_ARGUMENT_OPTIONS.add("-6");//$NON-NLS-1$
056: ZERO_ARGUMENT_OPTIONS.add("-6.0");//$NON-NLS-1$
057: ZERO_ARGUMENT_OPTIONS.add("-proc:only");//$NON-NLS-1$
058: ZERO_ARGUMENT_OPTIONS.add("-proc:none");//$NON-NLS-1$
059: ZERO_ARGUMENT_OPTIONS.add("-XprintProcessorInfo");//$NON-NLS-1$
060: ZERO_ARGUMENT_OPTIONS.add("-XprintRounds");//$NON-NLS-1$
061:
062: FILE_MANAGER_OPTIONS = new HashSet<String>();
063: FILE_MANAGER_OPTIONS.add("-bootclasspath");//$NON-NLS-1$
064: FILE_MANAGER_OPTIONS.add("-encoding");//$NON-NLS-1$
065: FILE_MANAGER_OPTIONS.add("-d");//$NON-NLS-1$
066: FILE_MANAGER_OPTIONS.add("-classpath");//$NON-NLS-1$
067: FILE_MANAGER_OPTIONS.add("-cp");//$NON-NLS-1$
068: FILE_MANAGER_OPTIONS.add("-sourcepath");//$NON-NLS-1$
069: FILE_MANAGER_OPTIONS.add("-extdirs");//$NON-NLS-1$
070: FILE_MANAGER_OPTIONS.add("-endorseddirs");//$NON-NLS-1$
071: FILE_MANAGER_OPTIONS.add("-s");//$NON-NLS-1$
072: FILE_MANAGER_OPTIONS.add("-processorpath");//$NON-NLS-1$
073:
074: ONE_ARGUMENT_OPTIONS = new HashSet<String>();
075: ONE_ARGUMENT_OPTIONS.addAll(FILE_MANAGER_OPTIONS);
076: ONE_ARGUMENT_OPTIONS.add("-log");//$NON-NLS-1$
077: ONE_ARGUMENT_OPTIONS.add("-repeat");//$NON-NLS-1$
078: ONE_ARGUMENT_OPTIONS.add("-maxProblems");//$NON-NLS-1$
079: ONE_ARGUMENT_OPTIONS.add("-source");//$NON-NLS-1$
080: ONE_ARGUMENT_OPTIONS.add("-target");//$NON-NLS-1$
081: ONE_ARGUMENT_OPTIONS.add("-processor");//$NON-NLS-1$
082:
083: }
084:
085: public static int processOptionsFileManager(String option) {
086: if (option == null)
087: return -1;
088: if (FILE_MANAGER_OPTIONS.contains(option)) {
089: return 1;
090: }
091: return -1;
092: }
093:
094: public static int processOptions(String option) {
095: if (option == null)
096: return -1;
097: if (ZERO_ARGUMENT_OPTIONS.contains(option)) {
098: return 0;
099: }
100: if (ONE_ARGUMENT_OPTIONS.contains(option)) {
101: return 1;
102: }
103: if (option.startsWith("-g")) { //$NON-NLS-1$
104: int length = option.length();
105: if (length > 3) {
106: StringTokenizer tokenizer = new StringTokenizer(option
107: .substring(3, option.length()), ",");//$NON-NLS-1$
108: while (tokenizer.hasMoreTokens()) {
109: String token = tokenizer.nextToken();
110: if ("vars".equals(token) || "lines".equals(token) || "source".equals(token)) {//$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
111: continue;
112: }
113: return -1;
114: }
115: return 0;
116: }
117: return -1;
118: }
119: if (option.startsWith("-warn")) {//$NON-NLS-1$
120: int length = option.length();
121: if (length <= 6) {
122: return -1;
123: }
124: int warnTokenStart;
125: switch (option.charAt(6)) {
126: case '+':
127: warnTokenStart = 7;
128: break;
129: case '-':
130: warnTokenStart = 7;
131: break;
132: default:
133: warnTokenStart = 6;
134: }
135:
136: StringTokenizer tokenizer = new StringTokenizer(option
137: .substring(warnTokenStart, option.length()), ","); //$NON-NLS-1$
138: int tokenCounter = 0;
139:
140: while (tokenizer.hasMoreTokens()) {
141: String token = tokenizer.nextToken();
142: tokenCounter++;
143: if ("constructorName".equals(token)//$NON-NLS-1$
144: || token.equals("pkgDefaultMethod")//$NON-NLS-1$
145: || token.equals("packageDefaultMethod")//$NON-NLS-1$
146: || token.equals("maskedCatchBlock")//$NON-NLS-1$
147: || token.equals("maskedCatchBlocks")//$NON-NLS-1$
148: || token.equals("deprecation")//$NON-NLS-1$
149: || token.equals("allDeprecation")//$NON-NLS-1$
150: || token.equals("unusedLocal")//$NON-NLS-1$
151: || token.equals("unusedLocals")//$NON-NLS-1$
152: || token.equals("unusedArgument")//$NON-NLS-1$
153: || token.equals("unusedArguments")//$NON-NLS-1$
154: || token.equals("unusedImport")//$NON-NLS-1$
155: || token.equals("unusedImports")//$NON-NLS-1$
156: || token.equals("unusedPrivate")//$NON-NLS-1$
157: || token.equals("unusedLabel")//$NON-NLS-1$
158: || token.equals("localHiding")//$NON-NLS-1$
159: || token.equals("fieldHiding")//$NON-NLS-1$
160: || token.equals("specialParamHiding")//$NON-NLS-1$
161: || token.equals("conditionAssign")//$NON-NLS-1$
162: || token.equals("syntheticAccess")//$NON-NLS-1$
163: || token.equals("synthetic-access")//$NON-NLS-1$
164: || token.equals("nls")//$NON-NLS-1$
165: || token.equals("staticReceiver")//$NON-NLS-1$
166: || token.equals("indirectStatic")//$NON-NLS-1$
167: || token.equals("noEffectAssign")//$NON-NLS-1$
168: || token.equals("intfNonInherited")//$NON-NLS-1$
169: || token.equals("interfaceNonInherited")//$NON-NLS-1$
170: || token.equals("charConcat")//$NON-NLS-1$
171: || token.equals("noImplicitStringConversion")//$NON-NLS-1$
172: || token.equals("semicolon")//$NON-NLS-1$
173: || token.equals("serial")//$NON-NLS-1$
174: || token.equals("emptyBlock")//$NON-NLS-1$
175: || token.equals("uselessTypeCheck")//$NON-NLS-1$
176: || token.equals("unchecked")//$NON-NLS-1$
177: || token.equals("unsafe")//$NON-NLS-1$
178: || token.equals("raw")//$NON-NLS-1$
179: || token.equals("finalBound")//$NON-NLS-1$
180: || token.equals("suppress")//$NON-NLS-1$
181: || token.equals("warningToken")//$NON-NLS-1$
182: || token.equals("unnecessaryElse")//$NON-NLS-1$
183: || token.equals("javadoc")//$NON-NLS-1$
184: || token.equals("allJavadoc")//$NON-NLS-1$
185: || token.equals("assertIdentifier")//$NON-NLS-1$
186: || token.equals("enumIdentifier")//$NON-NLS-1$
187: || token.equals("finally")//$NON-NLS-1$
188: || token.equals("unusedThrown")//$NON-NLS-1$
189: || token.equals("unqualifiedField")//$NON-NLS-1$
190: || token.equals("unqualified-field-access")//$NON-NLS-1$
191: || token.equals("typeHiding")//$NON-NLS-1$
192: || token.equals("varargsCast")//$NON-NLS-1$
193: || token.equals("null")//$NON-NLS-1$
194: || token.equals("boxing")//$NON-NLS-1$
195: || token.equals("over-ann")//$NON-NLS-1$
196: || token.equals("dep-ann")//$NON-NLS-1$
197: || token.equals("intfAnnotation")//$NON-NLS-1$
198: || token.equals("enumSwitch")//$NON-NLS-1$
199: || token.equals("incomplete-switch")//$NON-NLS-1$
200: || token.equals("hiding")//$NON-NLS-1$
201: || token.equals("static-access")//$NON-NLS-1$
202: || token.equals("unused")//$NON-NLS-1$
203: || token.equals("paramAssign")//$NON-NLS-1$
204: || token.equals("discouraged")//$NON-NLS-1$
205: || token.equals("forbidden")//$NON-NLS-1$
206: || token.equals("fallthrough")) {//$NON-NLS-1$
207: continue;
208: } else if (token.equals("tasks")) {//$NON-NLS-1$
209: String taskTags = "";//$NON-NLS-1$
210: int start = token.indexOf('(');
211: int end = token.indexOf(')');
212: if (start >= 0 && end >= 0 && start < end) {
213: taskTags = token.substring(start + 1, end)
214: .trim();
215: taskTags = taskTags.replace('|', ',');
216: }
217: if (taskTags.length() == 0) {
218: return -1;
219: }
220: continue;
221: } else {
222: return -1;
223: }
224: }
225: if (tokenCounter == 0) {
226: return -1;
227: } else {
228: return 0;
229: }
230: }
231: if (option.startsWith("-J")//$NON-NLS-1$
232: || option.startsWith("-X")//$NON-NLS-1$
233: || option.startsWith("-A")) {//$NON-NLS-1$
234: return 0;
235: }
236: return -1;
237: }
238: }
|