Fri, 05 Mar 2010 16:12:33 -0800
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
Reviewed-by: darcy
jjg@201 | 1 | /* |
xdono@229 | 2 | * Copyright 2008-2009 Sun Microsystems, Inc. All Rights Reserved. |
jjg@201 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jjg@201 | 4 | * |
jjg@201 | 5 | * This code is free software; you can redistribute it and/or modify it |
jjg@201 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jjg@201 | 7 | * published by the Free Software Foundation. Sun designates this |
jjg@201 | 8 | * particular file as subject to the "Classpath" exception as provided |
jjg@201 | 9 | * by Sun in the LICENSE file that accompanied this code. |
jjg@201 | 10 | * |
jjg@201 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jjg@201 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jjg@201 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jjg@201 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
jjg@201 | 15 | * accompanied this code). |
jjg@201 | 16 | * |
jjg@201 | 17 | * You should have received a copy of the GNU General Public License version |
jjg@201 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
jjg@201 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jjg@201 | 20 | * |
jjg@201 | 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
jjg@201 | 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
jjg@201 | 23 | * have any questions. |
jjg@201 | 24 | */ |
jjg@201 | 25 | |
jjg@201 | 26 | import java.awt.GridBagConstraints; |
jjg@201 | 27 | import java.awt.GridBagLayout; |
jjg@201 | 28 | import java.awt.event.ActionEvent; |
jjg@201 | 29 | import java.awt.event.ActionListener; |
jjg@201 | 30 | import java.awt.event.FocusEvent; |
jjg@201 | 31 | import java.awt.event.FocusListener; |
jjg@201 | 32 | import java.awt.event.ItemEvent; |
jjg@201 | 33 | import java.awt.event.ItemListener; |
jjg@201 | 34 | import java.io.BufferedReader; |
jjg@201 | 35 | import java.io.BufferedWriter; |
jjg@201 | 36 | import java.io.File; |
jjg@201 | 37 | import java.io.FileReader; |
jjg@201 | 38 | import java.io.FileWriter; |
jjg@201 | 39 | import java.io.IOException; |
jjg@201 | 40 | import java.io.Reader; |
jjg@201 | 41 | import java.io.Writer; |
jjg@201 | 42 | import java.util.ArrayList; |
jjg@201 | 43 | import java.util.Arrays; |
jjg@201 | 44 | import java.util.List; |
jjg@201 | 45 | import java.util.Properties; |
jjg@201 | 46 | import javax.swing.JButton; |
jjg@201 | 47 | import javax.swing.JCheckBox; |
jjg@201 | 48 | import javax.swing.JComboBox; |
jjg@201 | 49 | import javax.swing.JDialog; |
jjg@201 | 50 | import javax.swing.JLabel; |
jjg@201 | 51 | import javax.swing.JOptionPane; |
jjg@201 | 52 | import javax.swing.JPanel; |
jjg@201 | 53 | import javax.swing.JTextField; |
jjg@201 | 54 | |
jjg@201 | 55 | import javax.swing.SwingUtilities; |
jjg@201 | 56 | import org.apache.tools.ant.BuildException; |
jjg@201 | 57 | import org.apache.tools.ant.Project; |
jjg@201 | 58 | import org.apache.tools.ant.Task; |
jjg@201 | 59 | |
jjg@201 | 60 | /** |
jjg@201 | 61 | * Task to allow the user to control langtools tools built when using NetBeans. |
jjg@201 | 62 | * |
jjg@201 | 63 | * There are two primary modes. |
jjg@201 | 64 | * 1) Property mode. In this mode, property names are provided to get values |
jjg@201 | 65 | * that may be specified by the user, either directly in a GUI dialog, or |
jjg@201 | 66 | * read from a properties file. If the GUI dialog is invoked, values may |
jjg@201 | 67 | * optionally be set for future use. |
jjg@201 | 68 | * 2) Setup mode. In this mode, no property names are provided, and the GUI |
jjg@201 | 69 | * is invoked to allow the user to set or reset values for use in property mode. |
jjg@201 | 70 | */ |
jjg@201 | 71 | public class SelectToolTask extends Task { |
jjg@201 | 72 | /** |
jjg@201 | 73 | * Set the location of the private properties file used to keep the retain |
jjg@201 | 74 | * user preferences for this repository. |
jjg@201 | 75 | */ |
jjg@201 | 76 | public void setPropertyFile(File propertyFile) { |
jjg@201 | 77 | this.propertyFile = propertyFile; |
jjg@201 | 78 | } |
jjg@201 | 79 | |
jjg@201 | 80 | /** |
jjg@201 | 81 | * Set the name of the property which will be set to the name of the |
jjg@201 | 82 | * selected tool, if any. If no tool is selected, the property will |
jjg@201 | 83 | * remain unset. |
jjg@201 | 84 | */ |
jjg@201 | 85 | public void setToolProperty(String toolProperty) { |
jjg@201 | 86 | this.toolProperty = toolProperty; |
jjg@201 | 87 | } |
jjg@201 | 88 | |
jjg@201 | 89 | /** |
jjg@201 | 90 | * Set the name of the property which will be set to the execution args of the |
jjg@201 | 91 | * selected tool, if any. The args default to an empty string. |
jjg@201 | 92 | */ |
jjg@201 | 93 | public void setArgsProperty(String argsProperty) { |
jjg@201 | 94 | this.argsProperty = argsProperty; |
jjg@201 | 95 | } |
jjg@201 | 96 | |
jjg@201 | 97 | /** |
jjg@201 | 98 | * Specify whether or not to pop up a dialog if the user has not specified |
jjg@201 | 99 | * a default value for a property. |
jjg@201 | 100 | */ |
jjg@201 | 101 | public void setAskIfUnset(boolean askIfUnset) { |
jjg@201 | 102 | this.askIfUnset = askIfUnset; |
jjg@201 | 103 | } |
jjg@201 | 104 | |
jjg@201 | 105 | @Override |
jjg@201 | 106 | public void execute() { |
jjg@201 | 107 | Project p = getProject(); |
jjg@201 | 108 | |
jjg@201 | 109 | Properties props = readProperties(propertyFile); |
jjg@201 | 110 | toolName = props.getProperty("tool.name"); |
jjg@201 | 111 | if (toolName != null) { |
jjg@201 | 112 | toolArgs = props.getProperty(toolName + ".args", ""); |
jjg@201 | 113 | } |
jjg@201 | 114 | |
jjg@201 | 115 | if (toolProperty == null || |
jjg@201 | 116 | askIfUnset && (toolName == null |
jjg@201 | 117 | || (argsProperty != null && toolArgs == null))) { |
jjg@201 | 118 | showGUI(props); |
jjg@201 | 119 | } |
jjg@201 | 120 | |
jjg@201 | 121 | // finally, return required values, if any |
jjg@201 | 122 | if (toolProperty != null && !(toolName == null || toolName.equals(""))) { |
jjg@201 | 123 | p.setProperty(toolProperty, toolName); |
jjg@201 | 124 | |
jjg@201 | 125 | if (argsProperty != null && toolArgs != null) |
jjg@201 | 126 | p.setProperty(argsProperty, toolArgs); |
jjg@201 | 127 | } |
jjg@201 | 128 | } |
jjg@201 | 129 | |
jjg@201 | 130 | void showGUI(Properties fileProps) { |
jjg@201 | 131 | Properties guiProps = new Properties(fileProps); |
jjg@201 | 132 | JOptionPane p = createPane(guiProps); |
jjg@201 | 133 | p.createDialog("Select Tool").setVisible(true); |
jjg@201 | 134 | |
jjg@201 | 135 | toolName = (String) toolChoice.getSelectedItem(); |
jjg@201 | 136 | toolArgs = argsField.getText(); |
jjg@201 | 137 | |
jjg@201 | 138 | if (defaultCheck.isSelected()) { |
jjg@201 | 139 | if (toolName.equals("")) { |
jjg@201 | 140 | fileProps.remove("tool.name"); |
jjg@201 | 141 | } else { |
jjg@201 | 142 | fileProps.put("tool.name", toolName); |
jjg@201 | 143 | fileProps.put(toolName + ".args", toolArgs); |
jjg@201 | 144 | } |
jjg@201 | 145 | writeProperties(propertyFile, fileProps); |
jjg@201 | 146 | } |
jjg@201 | 147 | } |
jjg@201 | 148 | |
jjg@201 | 149 | JOptionPane createPane(final Properties props) { |
jjg@201 | 150 | JPanel body = new JPanel(new GridBagLayout()); |
jjg@201 | 151 | GridBagConstraints lc = new GridBagConstraints(); |
jjg@201 | 152 | lc.insets.right = 10; |
jjg@201 | 153 | lc.insets.bottom = 3; |
jjg@201 | 154 | GridBagConstraints fc = new GridBagConstraints(); |
jjg@201 | 155 | fc.anchor = GridBagConstraints.WEST; |
jjg@201 | 156 | fc.gridx = 1; |
jjg@201 | 157 | fc.gridwidth = GridBagConstraints.REMAINDER; |
jjg@201 | 158 | fc.insets.bottom = 3; |
jjg@201 | 159 | |
jjg@201 | 160 | JLabel toolLabel = new JLabel("Tool:"); |
jjg@201 | 161 | body.add(toolLabel, lc); |
jjg@201 | 162 | String[] toolChoices = { "apt", "javac", "javadoc", "javah", "javap" }; |
jjg@201 | 163 | if (true || toolProperty == null) { |
jjg@201 | 164 | // include empty value in setup mode |
jjg@201 | 165 | List<String> l = new ArrayList<String>(Arrays.asList(toolChoices)); |
jjg@201 | 166 | l.add(0, ""); |
jjg@201 | 167 | toolChoices = l.toArray(new String[l.size()]); |
jjg@201 | 168 | } |
jjg@201 | 169 | toolChoice = new JComboBox(toolChoices); |
jjg@201 | 170 | if (toolName != null) |
jjg@201 | 171 | toolChoice.setSelectedItem(toolName); |
jjg@201 | 172 | toolChoice.addItemListener(new ItemListener() { |
jjg@201 | 173 | public void itemStateChanged(ItemEvent e) { |
jjg@201 | 174 | String tn = (String) e.getItem(); |
jjg@201 | 175 | argsField.setText(getDefaultArgsForTool(props, tn)); |
jjg@201 | 176 | if (toolProperty != null) |
jjg@201 | 177 | okButton.setEnabled(!tn.equals("")); |
jjg@201 | 178 | } |
jjg@201 | 179 | }); |
jjg@201 | 180 | body.add(toolChoice, fc); |
jjg@201 | 181 | |
jjg@201 | 182 | argsField = new JTextField(getDefaultArgsForTool(props, toolName), 40); |
jjg@201 | 183 | if (toolProperty == null || argsProperty != null) { |
jjg@201 | 184 | JLabel argsLabel = new JLabel("Args:"); |
jjg@201 | 185 | body.add(argsLabel, lc); |
jjg@201 | 186 | body.add(argsField, fc); |
jjg@201 | 187 | argsField.addFocusListener(new FocusListener() { |
jjg@201 | 188 | public void focusGained(FocusEvent e) { |
jjg@201 | 189 | } |
jjg@201 | 190 | public void focusLost(FocusEvent e) { |
jjg@201 | 191 | String toolName = (String) toolChoice.getSelectedItem(); |
jjg@201 | 192 | if (toolName.length() > 0) |
jjg@201 | 193 | props.put(toolName + ".args", argsField.getText()); |
jjg@201 | 194 | } |
jjg@201 | 195 | }); |
jjg@201 | 196 | } |
jjg@201 | 197 | |
jjg@201 | 198 | defaultCheck = new JCheckBox("Set as default"); |
jjg@201 | 199 | if (toolProperty == null) |
jjg@201 | 200 | defaultCheck.setSelected(true); |
jjg@201 | 201 | else |
jjg@201 | 202 | body.add(defaultCheck, fc); |
jjg@201 | 203 | |
jjg@201 | 204 | final JOptionPane p = new JOptionPane(body); |
jjg@201 | 205 | okButton = new JButton("OK"); |
jjg@201 | 206 | okButton.setEnabled(toolProperty == null || (toolName != null && !toolName.equals(""))); |
jjg@201 | 207 | okButton.addActionListener(new ActionListener() { |
jjg@201 | 208 | public void actionPerformed(ActionEvent e) { |
jjg@201 | 209 | JDialog d = (JDialog) SwingUtilities.getAncestorOfClass(JDialog.class, p); |
jjg@201 | 210 | d.setVisible(false); |
jjg@201 | 211 | } |
jjg@201 | 212 | }); |
jjg@201 | 213 | p.setOptions(new Object[] { okButton }); |
jjg@201 | 214 | |
jjg@201 | 215 | return p; |
jjg@201 | 216 | } |
jjg@201 | 217 | |
jjg@201 | 218 | Properties readProperties(File file) { |
jjg@201 | 219 | Properties p = new Properties(); |
jjg@201 | 220 | if (file != null && file.exists()) { |
jjg@201 | 221 | Reader in = null; |
jjg@201 | 222 | try { |
jjg@201 | 223 | in = new BufferedReader(new FileReader(file)); |
jjg@201 | 224 | p.load(in); |
jjg@201 | 225 | in.close(); |
jjg@201 | 226 | } catch (IOException e) { |
jjg@201 | 227 | throw new BuildException("error reading property file", e); |
jjg@201 | 228 | } finally { |
jjg@201 | 229 | if (in != null) { |
jjg@201 | 230 | try { |
jjg@201 | 231 | in.close(); |
jjg@201 | 232 | } catch (IOException e) { |
jjg@201 | 233 | throw new BuildException("cannot close property file", e); |
jjg@201 | 234 | } |
jjg@201 | 235 | } |
jjg@201 | 236 | } |
jjg@201 | 237 | } |
jjg@201 | 238 | return p; |
jjg@201 | 239 | } |
jjg@201 | 240 | |
jjg@201 | 241 | void writeProperties(File file, Properties p) { |
jjg@201 | 242 | if (file != null) { |
jjg@201 | 243 | Writer out = null; |
jjg@201 | 244 | try { |
jjg@201 | 245 | File dir = file.getParentFile(); |
jjg@201 | 246 | if (dir != null && !dir.exists()) |
jjg@201 | 247 | dir.mkdirs(); |
jjg@201 | 248 | out = new BufferedWriter(new FileWriter(file)); |
jjg@201 | 249 | p.store(out, "langtools properties"); |
jjg@201 | 250 | out.close(); |
jjg@201 | 251 | } catch (IOException e) { |
jjg@201 | 252 | throw new BuildException("error writing property file", e); |
jjg@201 | 253 | } finally { |
jjg@201 | 254 | if (out != null) { |
jjg@201 | 255 | try { |
jjg@201 | 256 | out.close(); |
jjg@201 | 257 | } catch (IOException e) { |
jjg@201 | 258 | throw new BuildException("cannot close property file", e); |
jjg@201 | 259 | } |
jjg@201 | 260 | } |
jjg@201 | 261 | } |
jjg@201 | 262 | } |
jjg@201 | 263 | } |
jjg@201 | 264 | |
jjg@201 | 265 | String getDefaultArgsForTool(Properties props, String tn) { |
jjg@201 | 266 | return (tn == null || tn.equals("")) ? "" : props.getProperty(tn + ".args", ""); |
jjg@201 | 267 | } |
jjg@201 | 268 | |
jjg@201 | 269 | // Ant task parameters |
jjg@201 | 270 | private boolean askIfUnset; |
jjg@201 | 271 | private String toolProperty; |
jjg@201 | 272 | private String argsProperty; |
jjg@201 | 273 | private File propertyFile; |
jjg@201 | 274 | |
jjg@201 | 275 | // GUI components |
jjg@201 | 276 | private JComboBox toolChoice; |
jjg@201 | 277 | private JTextField argsField; |
jjg@201 | 278 | private JCheckBox defaultCheck; |
jjg@201 | 279 | private JButton okButton; |
jjg@201 | 280 | |
jjg@201 | 281 | // Result values for the client |
jjg@201 | 282 | private String toolName; |
jjg@201 | 283 | private String toolArgs; |
jjg@201 | 284 | } |