1.1 --- a/api/src/org/netbeans/modules/jackpot30/impl/batch/BatchUtilities.java Sun Jan 16 13:56:54 2011 +0100
1.2 +++ b/api/src/org/netbeans/modules/jackpot30/impl/batch/BatchUtilities.java Sun Jan 16 13:56:54 2011 +0100
1.3 @@ -212,7 +212,7 @@
1.4 }
1.5 }
1.6
1.7 - private static boolean applyFixes(WorkingCopy copy, Map<Project, Set<String>> processedDependencyChanges, Collection<? extends ErrorDescription> hints, Collection<? super MessageImpl> problems) throws IllegalStateException, Exception {
1.8 + public static boolean applyFixes(WorkingCopy copy, Map<Project, Set<String>> processedDependencyChanges, Collection<? extends ErrorDescription> hints, Collection<? super MessageImpl> problems) throws IllegalStateException, Exception {
1.9 List<JavaFix> fixes = new ArrayList<JavaFix>();
1.10 for (ErrorDescription ed : hints) {
1.11 if (!ed.getFixes().isComputed()) {
2.1 --- a/api/src/org/netbeans/modules/jackpot30/spi/Hacks.java Sun Jan 16 13:56:54 2011 +0100
2.2 +++ b/api/src/org/netbeans/modules/jackpot30/spi/Hacks.java Sun Jan 16 13:56:54 2011 +0100
2.3 @@ -41,15 +41,14 @@
2.4
2.5 import com.sun.source.tree.Scope;
2.6 import com.sun.source.tree.Tree;
2.7 +import com.sun.source.util.TreePath;
2.8 import com.sun.tools.javac.tree.JCTree;
2.9 import com.sun.tools.javac.tree.JCTree.JCErroneous;
2.10 import java.io.ByteArrayOutputStream;
2.11 import java.io.File;
2.12 import java.io.IOException;
2.13 import java.io.OutputStream;
2.14 -import java.net.MalformedURLException;
2.15 import java.net.URI;
2.16 -import java.net.URL;
2.17 import java.util.ArrayList;
2.18 import java.util.Arrays;
2.19 import java.util.Collection;
2.20 @@ -58,7 +57,8 @@
2.21 import java.util.LinkedList;
2.22 import java.util.List;
2.23 import java.util.Map;
2.24 -import java.util.StringTokenizer;
2.25 +import java.util.Set;
2.26 +import java.util.concurrent.atomic.AtomicBoolean;
2.27 import java.util.logging.Level;
2.28 import java.util.logging.Logger;
2.29 import java.util.prefs.Preferences;
2.30 @@ -82,9 +82,17 @@
2.31 import org.netbeans.api.java.classpath.ClassPath.Entry;
2.32 import org.netbeans.api.java.source.ClasspathInfo;
2.33 import org.netbeans.api.java.source.CompilationInfo;
2.34 +import org.netbeans.api.java.source.WorkingCopy;
2.35 +import org.netbeans.api.project.Project;
2.36 +import org.netbeans.modules.jackpot30.impl.MessageImpl;
2.37 +import org.netbeans.modules.jackpot30.impl.RulesManager;
2.38 import org.netbeans.modules.jackpot30.impl.Utilities;
2.39 -import org.netbeans.spi.java.classpath.support.ClassPathSupport;
2.40 +import org.netbeans.modules.jackpot30.impl.batch.BatchUtilities;
2.41 +import org.netbeans.modules.jackpot30.impl.hints.HintsInvoker;
2.42 +import org.netbeans.modules.jackpot30.spi.HintDescription.PatternDescription;
2.43 +import org.netbeans.spi.editor.hints.ErrorDescription;
2.44 import org.openide.filesystems.FileUtil;
2.45 +import org.openide.util.Exceptions;
2.46
2.47 /**
2.48 *
2.49 @@ -205,4 +213,45 @@
2.50 public static interface HintPreferencesProvider {
2.51 public Preferences findPreferences(HintMetadata hm);
2.52 }
2.53 +
2.54 + public static void findHintsAndApplyFixes(WorkingCopy copy, Iterable<? extends HintDescription> hints, TreePath on, AtomicBoolean cancel) {
2.55 + Map<Tree.Kind, List<HintDescription>> kindHints = new HashMap<Tree.Kind, List<HintDescription>>();
2.56 + Map<PatternDescription, List<HintDescription>> patternHints = new HashMap<PatternDescription, List<HintDescription>>();
2.57 +
2.58 + RulesManager.sortOut(hints, kindHints, patternHints);
2.59 +
2.60 + if (!kindHints.isEmpty()) {
2.61 + throw new IllegalStateException();
2.62 + }
2.63 +
2.64 + HintsInvoker inv = new HintsInvoker(copy, cancel);
2.65 + Map<String, List<PatternDescription>> patterns = HintsInvoker.computePatternTests(patternHints);
2.66 + Map<String, Collection<TreePath>> occurringPattern = new HashMap<String, Collection<TreePath>>();
2.67 +
2.68 + for (String key : patterns.keySet()) {
2.69 + occurringPattern.put(key, Collections.singleton(on));
2.70 + }
2.71 +
2.72 + List<ErrorDescription> errs = new ArrayList<ErrorDescription>();
2.73 +
2.74 + for (List<ErrorDescription> v : inv.doComputeHints(copy, occurringPattern, patterns, patternHints).values()) {
2.75 + errs.addAll(v);
2.76 + }
2.77 +
2.78 + List<MessageImpl> problems = new LinkedList<MessageImpl>();
2.79 +
2.80 + try {
2.81 + if (BatchUtilities.applyFixes(copy, Collections.<Project, Set<String>>emptyMap(), errs, problems)) {
2.82 + throw new IllegalStateException();
2.83 + }
2.84 + } catch (IllegalStateException ex) {
2.85 + Exceptions.printStackTrace(ex);
2.86 + } catch (Exception ex) {
2.87 + Exceptions.printStackTrace(ex);
2.88 + }
2.89 +
2.90 + if (!problems.isEmpty()) {
2.91 + throw new IllegalStateException(problems.get(0).text);
2.92 + }
2.93 + }
2.94 }
3.1 --- a/bridges/refactoring/src/org/netbeans/modules/jackpot30/refactoring/RefactoringActionsProviderExt.java Sun Jan 16 13:56:54 2011 +0100
3.2 +++ b/bridges/refactoring/src/org/netbeans/modules/jackpot30/refactoring/RefactoringActionsProviderExt.java Sun Jan 16 13:56:54 2011 +0100
3.3 @@ -43,8 +43,12 @@
3.4 import java.util.logging.Logger;
3.5 import javax.lang.model.element.Element;
3.6 import javax.lang.model.element.ElementKind;
3.7 +import javax.lang.model.element.ExecutableElement;
3.8 +import javax.lang.model.element.VariableElement;
3.9 +import javax.lang.model.type.TypeKind;
3.10 import org.netbeans.api.java.source.CompilationInfo;
3.11 import org.netbeans.api.java.source.TreePathHandle;
3.12 +import org.netbeans.modules.jackpot30.refactoring.invertboolean.InvertBooleanRefactoringUI;
3.13 import org.netbeans.modules.jackpot30.refactoring.noconstructor.ReplaceConstructorRefactoringUI;
3.14 import org.netbeans.modules.refactoring.api.RenameRefactoring;
3.15 import org.netbeans.modules.refactoring.java.RetoucheUtils;
3.16 @@ -112,6 +116,53 @@
3.17 RetoucheUtils.invokeAfterScanFinished(task, "Remove Constructor");//getActionName(RefactoringActionsFactory.renameAction()));
3.18 }
3.19
3.20 + private static RefactoringUI doInvertBooleanImpl(TreePathHandle selectedElement, CompilationInfo info) {
3.21 + Element selected = selectedElement.resolveElement(info);
3.22 + if (selected==null) {
3.23 +// logger().log(Level.INFO, "doRename: " + selectedElement, new NullPointerException("selected")); // NOI18N
3.24 + return null;
3.25 + }
3.26 + if (selected.getKind().isField() && ((VariableElement) selected).asType().getKind() == TypeKind.BOOLEAN) {
3.27 + return new InvertBooleanRefactoringUI(selectedElement, ((VariableElement) selected).getSimpleName().toString());
3.28 + }
3.29 + if (selected.getKind() == ElementKind.METHOD && ((ExecutableElement) selected).getReturnType().getKind() == TypeKind.BOOLEAN) {
3.30 + return new InvertBooleanRefactoringUI(selectedElement, ((ExecutableElement) selected).getSimpleName().toString());
3.31 + }
3.32 +
3.33 + return null;
3.34 + }
3.35 +
3.36 + public static void doInvertBoolean(final Lookup lookup) {
3.37 + Runnable task;
3.38 + EditorCookie ec = lookup.lookup(EditorCookie.class);
3.39 + if (isFromEditor(ec)) {
3.40 + task = new TextComponentTask(ec) {
3.41 + @Override
3.42 + protected RefactoringUI createRefactoringUI(TreePathHandle selectedElement,int startOffset,int endOffset, final CompilationInfo info) {
3.43 + return doInvertBooleanImpl(selectedElement, info);
3.44 + }
3.45 + };
3.46 + } else {
3.47 + task = new TreePathHandleTask(new HashSet<Node>(lookup.lookupAll(Node.class)), true) {
3.48 +
3.49 + RefactoringUI ui;
3.50 +
3.51 + @Override
3.52 + protected void treePathHandleResolved(TreePathHandle handle, CompilationInfo javac) {
3.53 + ui = doInvertBooleanImpl(handle, javac);
3.54 + }
3.55 +
3.56 + @Override
3.57 + protected RefactoringUI createRefactoringUI(Collection<TreePathHandle> handles) {
3.58 + return ui;
3.59 + }
3.60 +
3.61 + };
3.62 + }
3.63 +
3.64 + RetoucheUtils.invokeAfterScanFinished(task, "Remove Constructor");//getActionName(RefactoringActionsFactory.renameAction()));
3.65 + }
3.66 +
3.67 // static String getActionName(Action action) {
3.68 // String arg = (String) action.getValue(Action.NAME);
3.69 // arg = arg.replace("&", ""); // NOI18N
3.70 @@ -120,6 +171,14 @@
3.71
3.72
3.73 public static boolean canReplaceConstructor(Lookup lookup) {
3.74 + return canRefactor(lookup);
3.75 + }
3.76 +
3.77 + public static boolean canInvertBoolean(Lookup lookup) {
3.78 + return canRefactor(lookup);
3.79 + }
3.80 +
3.81 + private static boolean canRefactor(Lookup lookup) {
3.82 Collection<? extends Node> nodes = new HashSet<Node>(lookup.lookupAll(Node.class));
3.83 if(nodes.size() != 1)
3.84 return false;
4.1 --- a/bridges/refactoring/src/org/netbeans/modules/jackpot30/refactoring/RefactoringPluginFactoryImpl.java Sun Jan 16 13:56:54 2011 +0100
4.2 +++ b/bridges/refactoring/src/org/netbeans/modules/jackpot30/refactoring/RefactoringPluginFactoryImpl.java Sun Jan 16 13:56:54 2011 +0100
4.3 @@ -39,6 +39,8 @@
4.4
4.5 package org.netbeans.modules.jackpot30.refactoring;
4.6
4.7 +import org.netbeans.modules.jackpot30.refactoring.invertboolean.InvertBooleanRefactoring;
4.8 +import org.netbeans.modules.jackpot30.refactoring.invertboolean.InvertBooleanRefactoringPluginImpl;
4.9 import org.netbeans.modules.jackpot30.refactoring.noconstructor.ReplaceConstructorRefactoring;
4.10 import org.netbeans.modules.jackpot30.refactoring.noconstructor.ReplaceConstructorRefactoringPluginImpl;
4.11 import org.netbeans.modules.refactoring.api.AbstractRefactoring;
4.12 @@ -63,6 +65,10 @@
4.13 return new ReplaceConstructorRefactoringPluginImpl((ReplaceConstructorRefactoring) refactoring);
4.14 }
4.15
4.16 + if (refactoring instanceof InvertBooleanRefactoring) {
4.17 + return new InvertBooleanRefactoringPluginImpl((InvertBooleanRefactoring) refactoring);
4.18 + }
4.19 +
4.20 return null;
4.21 }
4.22
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/bridges/refactoring/src/org/netbeans/modules/jackpot30/refactoring/invertboolean/Bundle.properties Sun Jan 16 13:56:54 2011 +0100
5.3 @@ -0,0 +1,2 @@
5.4 +InvertBooleanRefactoringPanel.nameField.text=
5.5 +InvertBooleanRefactoringPanel.jLabel1.text=New name:
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
6.2 +++ b/bridges/refactoring/src/org/netbeans/modules/jackpot30/refactoring/invertboolean/InvertBooleanAction.java Sun Jan 16 13:56:54 2011 +0100
6.3 @@ -0,0 +1,75 @@
6.4 +/*
6.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
6.6 + *
6.7 + * Copyright 2010-2011 Sun Microsystems, Inc. All rights reserved.
6.8 + *
6.9 + * The contents of this file are subject to the terms of either the GNU
6.10 + * General Public License Version 2 only ("GPL") or the Common
6.11 + * Development and Distribution License("CDDL") (collectively, the
6.12 + * "License"). You may not use this file except in compliance with the
6.13 + * License. You can obtain a copy of the License at
6.14 + * http://www.netbeans.org/cddl-gplv2.html
6.15 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
6.16 + * specific language governing permissions and limitations under the
6.17 + * License. When distributing the software, include this License Header
6.18 + * Notice in each file and include the License file at
6.19 + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
6.20 + * particular file as subject to the "Classpath" exception as provided
6.21 + * by Sun in the GPL Version 2 section of the License file that
6.22 + * accompanied this code. If applicable, add the following below the
6.23 + * License Header, with the fields enclosed by brackets [] replaced by
6.24 + * your own identifying information:
6.25 + * "Portions Copyrighted [year] [name of copyright owner]"
6.26 + *
6.27 + * If you wish your version of this file to be governed by only the CDDL
6.28 + * or only the GPL Version 2, indicate your decision by adding
6.29 + * "[Contributor] elects to include this software in this distribution
6.30 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
6.31 + * single choice of license, a recipient has the option to distribute
6.32 + * your version of this file under either the CDDL, the GPL Version 2 or
6.33 + * to extend the choice of license to its licensees as provided above.
6.34 + * However, if you add GPL Version 2 code and therefore, elected the GPL
6.35 + * Version 2 license, then the option applies only if the new code is
6.36 + * made subject to such option by the copyright holder.
6.37 + *
6.38 + * Contributor(s):
6.39 + *
6.40 + * Portions Copyrighted 2010-2011 Sun Microsystems, Inc.
6.41 + */
6.42 +package org.netbeans.modules.jackpot30.refactoring.invertboolean;
6.43 +
6.44 +import javax.swing.Action;
6.45 +import org.netbeans.modules.jackpot30.refactoring.RefactoringActionsProviderExt;
6.46 +import org.netbeans.modules.refactoring.java.ui.JavaRefactoringGlobalAction;
6.47 +import org.openide.util.HelpCtx;
6.48 +import org.openide.util.Lookup;
6.49 +import org.openide.util.NbBundle.Messages;
6.50 +
6.51 +public final class InvertBooleanAction extends JavaRefactoringGlobalAction {
6.52 +
6.53 + @Messages("LBL_InvertBooleanAction=Invert Boolean")
6.54 + public InvertBooleanAction() {
6.55 + super(Bundle.LBL_InvertBooleanAction(), null); // NOI18N
6.56 + putValue("noIconInMenu", Boolean.TRUE); // NOI18N
6.57 + }
6.58 +
6.59 + public final void performAction(Lookup context) {
6.60 + RefactoringActionsProviderExt.doInvertBoolean(context);
6.61 + }
6.62 +
6.63 + public org.openide.util.HelpCtx getHelpCtx() {
6.64 + return HelpCtx.DEFAULT_HELP;
6.65 + }
6.66 +
6.67 + protected boolean asynchronous() {
6.68 + return false;
6.69 + }
6.70 +
6.71 + protected boolean enable(Lookup context) {
6.72 + return RefactoringActionsProviderExt.canInvertBoolean(context);
6.73 + }
6.74 +
6.75 + public static Action create() {
6.76 + return InvertBooleanAction.findObject(InvertBooleanAction.class, true);
6.77 + }
6.78 +}
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
7.2 +++ b/bridges/refactoring/src/org/netbeans/modules/jackpot30/refactoring/invertboolean/InvertBooleanRefactoring.java Sun Jan 16 13:56:54 2011 +0100
7.3 @@ -0,0 +1,69 @@
7.4 +/*
7.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
7.6 + *
7.7 + * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
7.8 + *
7.9 + * The contents of this file are subject to the terms of either the GNU
7.10 + * General Public License Version 2 only ("GPL") or the Common
7.11 + * Development and Distribution License("CDDL") (collectively, the
7.12 + * "License"). You may not use this file except in compliance with the
7.13 + * License. You can obtain a copy of the License at
7.14 + * http://www.netbeans.org/cddl-gplv2.html
7.15 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
7.16 + * specific language governing permissions and limitations under the
7.17 + * License. When distributing the software, include this License Header
7.18 + * Notice in each file and include the License file at
7.19 + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
7.20 + * particular file as subject to the "Classpath" exception as provided
7.21 + * by Sun in the GPL Version 2 section of the License file that
7.22 + * accompanied this code. If applicable, add the following below the
7.23 + * License Header, with the fields enclosed by brackets [] replaced by
7.24 + * your own identifying information:
7.25 + * "Portions Copyrighted [year] [name of copyright owner]"
7.26 + *
7.27 + * If you wish your version of this file to be governed by only the CDDL
7.28 + * or only the GPL Version 2, indicate your decision by adding
7.29 + * "[Contributor] elects to include this software in this distribution
7.30 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
7.31 + * single choice of license, a recipient has the option to distribute
7.32 + * your version of this file under either the CDDL, the GPL Version 2 or
7.33 + * to extend the choice of license to its licensees as provided above.
7.34 + * However, if you add GPL Version 2 code and therefore, elected the GPL
7.35 + * Version 2 license, then the option applies only if the new code is
7.36 + * made subject to such option by the copyright holder.
7.37 + *
7.38 + * Contributor(s):
7.39 + *
7.40 + * Portions Copyrighted 2010 Sun Microsystems, Inc.
7.41 + */
7.42 +
7.43 +package org.netbeans.modules.jackpot30.refactoring.invertboolean;
7.44 +
7.45 +import org.netbeans.api.java.source.TreePathHandle;
7.46 +import org.netbeans.modules.refactoring.api.AbstractRefactoring;
7.47 +import org.openide.util.lookup.Lookups;
7.48 +
7.49 +/**
7.50 + *
7.51 + * @author lahvac
7.52 + */
7.53 +public class InvertBooleanRefactoring extends AbstractRefactoring {
7.54 +
7.55 + private final TreePathHandle original;
7.56 + private final String newName;
7.57 +
7.58 + public InvertBooleanRefactoring(TreePathHandle original, String newName) {
7.59 + super(Lookups.singleton(original));
7.60 + this.original = original;
7.61 + this.newName = newName;
7.62 + }
7.63 +
7.64 + public TreePathHandle getOriginal() {
7.65 + return original;
7.66 + }
7.67 +
7.68 + public String getNewName() {
7.69 + return newName;
7.70 + }
7.71 +
7.72 +}
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
8.2 +++ b/bridges/refactoring/src/org/netbeans/modules/jackpot30/refactoring/invertboolean/InvertBooleanRefactoringPanel.form Sun Jan 16 13:56:54 2011 +0100
8.3 @@ -0,0 +1,58 @@
8.4 +<?xml version="1.1" encoding="UTF-8" ?>
8.5 +
8.6 +<Form version="1.5" maxVersion="1.7" type="org.netbeans.modules.form.forminfo.JPanelFormInfo">
8.7 + <AuxValues>
8.8 + <AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="1"/>
8.9 + <AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/>
8.10 + <AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/>
8.11 + <AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/>
8.12 + <AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="true"/>
8.13 + <AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/>
8.14 + <AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
8.15 + <AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
8.16 + <AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
8.17 + </AuxValues>
8.18 +
8.19 + <Layout>
8.20 + <DimensionLayout dim="0">
8.21 + <Group type="103" groupAlignment="0" attributes="0">
8.22 + <Group type="102" alignment="0" attributes="0">
8.23 + <EmptySpace max="-2" attributes="0"/>
8.24 + <Component id="jLabel1" min="-2" max="-2" attributes="0"/>
8.25 + <EmptySpace max="-2" attributes="0"/>
8.26 + <Component id="nameField" pref="249" max="32767" attributes="0"/>
8.27 + <EmptySpace max="-2" attributes="0"/>
8.28 + </Group>
8.29 + </Group>
8.30 + </DimensionLayout>
8.31 + <DimensionLayout dim="1">
8.32 + <Group type="103" groupAlignment="0" attributes="0">
8.33 + <Group type="102" alignment="0" attributes="0">
8.34 + <EmptySpace max="-2" attributes="0"/>
8.35 + <Group type="103" groupAlignment="3" attributes="0">
8.36 + <Component id="jLabel1" alignment="3" min="-2" max="-2" attributes="0"/>
8.37 + <Component id="nameField" alignment="3" min="-2" max="-2" attributes="0"/>
8.38 + </Group>
8.39 + <EmptySpace max="32767" attributes="0"/>
8.40 + </Group>
8.41 + </Group>
8.42 + </DimensionLayout>
8.43 + </Layout>
8.44 + <SubComponents>
8.45 + <Component class="javax.swing.JLabel" name="jLabel1">
8.46 + <Properties>
8.47 + <Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
8.48 + <ResourceString bundle="org/netbeans/modules/jackpot30/refactoring/noconstructor/Bundle.properties" key="InvertBooleanRefactoringPanel.jLabel1.text" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, "{key}")"/>
8.49 + </Property>
8.50 + </Properties>
8.51 + </Component>
8.52 + <Component class="javax.swing.JTextField" name="nameField">
8.53 + <Properties>
8.54 + <Property name="columns" type="int" value="15"/>
8.55 + <Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
8.56 + <ResourceString bundle="org/netbeans/modules/jackpot30/refactoring/noconstructor/Bundle.properties" key="InvertBooleanRefactoringPanel.nameField.text" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, "{key}")"/>
8.57 + </Property>
8.58 + </Properties>
8.59 + </Component>
8.60 + </SubComponents>
8.61 +</Form>
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
9.2 +++ b/bridges/refactoring/src/org/netbeans/modules/jackpot30/refactoring/invertboolean/InvertBooleanRefactoringPanel.java Sun Jan 16 13:56:54 2011 +0100
9.3 @@ -0,0 +1,124 @@
9.4 +/*
9.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
9.6 + *
9.7 + * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
9.8 + *
9.9 + * The contents of this file are subject to the terms of either the GNU
9.10 + * General Public License Version 2 only ("GPL") or the Common
9.11 + * Development and Distribution License("CDDL") (collectively, the
9.12 + * "License"). You may not use this file except in compliance with the
9.13 + * License. You can obtain a copy of the License at
9.14 + * http://www.netbeans.org/cddl-gplv2.html
9.15 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
9.16 + * specific language governing permissions and limitations under the
9.17 + * License. When distributing the software, include this License Header
9.18 + * Notice in each file and include the License file at
9.19 + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
9.20 + * particular file as subject to the "Classpath" exception as provided
9.21 + * by Sun in the GPL Version 2 section of the License file that
9.22 + * accompanied this code. If applicable, add the following below the
9.23 + * License Header, with the fields enclosed by brackets [] replaced by
9.24 + * your own identifying information:
9.25 + * "Portions Copyrighted [year] [name of copyright owner]"
9.26 + *
9.27 + * If you wish your version of this file to be governed by only the CDDL
9.28 + * or only the GPL Version 2, indicate your decision by adding
9.29 + * "[Contributor] elects to include this software in this distribution
9.30 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
9.31 + * single choice of license, a recipient has the option to distribute
9.32 + * your version of this file under either the CDDL, the GPL Version 2 or
9.33 + * to extend the choice of license to its licensees as provided above.
9.34 + * However, if you add GPL Version 2 code and therefore, elected the GPL
9.35 + * Version 2 license, then the option applies only if the new code is
9.36 + * made subject to such option by the copyright holder.
9.37 + *
9.38 + * Contributor(s):
9.39 + *
9.40 + * Portions Copyrighted 2010 Sun Microsystems, Inc.
9.41 + */
9.42 +
9.43 +package org.netbeans.modules.jackpot30.refactoring.invertboolean;
9.44 +
9.45 +import javax.swing.event.ChangeEvent;
9.46 +import javax.swing.event.ChangeListener;
9.47 +import javax.swing.event.DocumentEvent;
9.48 +import javax.swing.event.DocumentListener;
9.49 +import org.netbeans.api.annotations.common.NonNull;
9.50 +
9.51 +/**
9.52 + *
9.53 + * @author lahvac
9.54 + */
9.55 +public class InvertBooleanRefactoringPanel extends javax.swing.JPanel {
9.56 +
9.57 + public InvertBooleanRefactoringPanel(final @NonNull ChangeListener parent) {
9.58 + initComponents();
9.59 + nameField.getDocument().addDocumentListener(new DocumentListener() {
9.60 + @Override
9.61 + public void insertUpdate(DocumentEvent e) {
9.62 + parent.stateChanged(new ChangeEvent(InvertBooleanRefactoringPanel.this));
9.63 + }
9.64 + @Override
9.65 + public void removeUpdate(DocumentEvent e) {
9.66 + parent.stateChanged(new ChangeEvent(InvertBooleanRefactoringPanel.this));
9.67 + }
9.68 + @Override
9.69 + public void changedUpdate(DocumentEvent e) {}
9.70 + });
9.71 + }
9.72 +
9.73 + /** This method is called from within the constructor to
9.74 + * initialize the form.
9.75 + * WARNING: Do NOT modify this code. The content of this method is
9.76 + * always regenerated by the Form Editor.
9.77 + */
9.78 + @SuppressWarnings("unchecked")
9.79 + // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
9.80 + private void initComponents() {
9.81 +
9.82 + jLabel1 = new javax.swing.JLabel();
9.83 + nameField = new javax.swing.JTextField();
9.84 +
9.85 + jLabel1.setText(org.openide.util.NbBundle.getMessage(InvertBooleanRefactoringPanel.class, "InvertBooleanRefactoringPanel.jLabel1.text")); // NOI18N
9.86 +
9.87 + nameField.setColumns(15);
9.88 + nameField.setText(org.openide.util.NbBundle.getMessage(InvertBooleanRefactoringPanel.class, "InvertBooleanRefactoringPanel.nameField.text")); // NOI18N
9.89 +
9.90 + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
9.91 + this.setLayout(layout);
9.92 + layout.setHorizontalGroup(
9.93 + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
9.94 + .addGroup(layout.createSequentialGroup()
9.95 + .addContainerGap()
9.96 + .addComponent(jLabel1)
9.97 + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
9.98 + .addComponent(nameField, javax.swing.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE)
9.99 + .addContainerGap())
9.100 + );
9.101 + layout.setVerticalGroup(
9.102 + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
9.103 + .addGroup(layout.createSequentialGroup()
9.104 + .addContainerGap()
9.105 + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
9.106 + .addComponent(jLabel1)
9.107 + .addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
9.108 + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
9.109 + );
9.110 + }// </editor-fold>//GEN-END:initComponents
9.111 +
9.112 +
9.113 + // Variables declaration - do not modify//GEN-BEGIN:variables
9.114 + private javax.swing.JLabel jLabel1;
9.115 + private javax.swing.JTextField nameField;
9.116 + // End of variables declaration//GEN-END:variables
9.117 +
9.118 + public void initialize(String origName) {
9.119 + nameField.setText(origName);
9.120 + nameField.setSelectionStart(0);
9.121 + nameField.setSelectionStart(origName.length());
9.122 + }
9.123 +
9.124 + public String getName() {
9.125 + return nameField.getText();
9.126 + }
9.127 +}
10.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
10.2 +++ b/bridges/refactoring/src/org/netbeans/modules/jackpot30/refactoring/invertboolean/InvertBooleanRefactoringPluginImpl.java Sun Jan 16 13:56:54 2011 +0100
10.3 @@ -0,0 +1,247 @@
10.4 +/*
10.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
10.6 + *
10.7 + * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
10.8 + *
10.9 + * The contents of this file are subject to the terms of either the GNU
10.10 + * General Public License Version 2 only ("GPL") or the Common
10.11 + * Development and Distribution License("CDDL") (collectively, the
10.12 + * "License"). You may not use this file except in compliance with the
10.13 + * License. You can obtain a copy of the License at
10.14 + * http://www.netbeans.org/cddl-gplv2.html
10.15 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
10.16 + * specific language governing permissions and limitations under the
10.17 + * License. When distributing the software, include this License Header
10.18 + * Notice in each file and include the License file at
10.19 + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
10.20 + * particular file as subject to the "Classpath" exception as provided
10.21 + * by Sun in the GPL Version 2 section of the License file that
10.22 + * accompanied this code. If applicable, add the following below the
10.23 + * License Header, with the fields enclosed by brackets [] replaced by
10.24 + * your own identifying information:
10.25 + * "Portions Copyrighted [year] [name of copyright owner]"
10.26 + *
10.27 + * If you wish your version of this file to be governed by only the CDDL
10.28 + * or only the GPL Version 2, indicate your decision by adding
10.29 + * "[Contributor] elects to include this software in this distribution
10.30 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
10.31 + * single choice of license, a recipient has the option to distribute
10.32 + * your version of this file under either the CDDL, the GPL Version 2 or
10.33 + * to extend the choice of license to its licensees as provided above.
10.34 + * However, if you add GPL Version 2 code and therefore, elected the GPL
10.35 + * Version 2 license, then the option applies only if the new code is
10.36 + * made subject to such option by the copyright holder.
10.37 + *
10.38 + * Contributor(s):
10.39 + *
10.40 + * Portions Copyrighted 2010 Sun Microsystems, Inc.
10.41 + */
10.42 +
10.43 +package org.netbeans.modules.jackpot30.refactoring.invertboolean;
10.44 +
10.45 +import com.sun.source.tree.ClassTree;
10.46 +import com.sun.source.tree.MethodTree;
10.47 +import com.sun.source.tree.ReturnTree;
10.48 +import com.sun.source.tree.Tree;
10.49 +import com.sun.source.tree.Tree.Kind;
10.50 +import com.sun.source.tree.VariableTree;
10.51 +import com.sun.source.util.TreePath;
10.52 +import com.sun.source.util.TreePathScanner;
10.53 +import java.io.IOException;
10.54 +import java.util.HashMap;
10.55 +import java.util.Map;
10.56 +import javax.lang.model.element.Modifier;
10.57 +import javax.lang.model.element.TypeElement;
10.58 +import org.netbeans.api.java.source.JavaSource;
10.59 +import org.netbeans.api.java.source.JavaSource.Phase;
10.60 +import org.netbeans.api.java.source.ModificationResult;
10.61 +import org.netbeans.api.java.source.Task;
10.62 +import org.netbeans.api.java.source.TreePathHandle;
10.63 +import org.netbeans.api.java.source.WorkingCopy;
10.64 +import org.netbeans.modules.jackpot30.spi.Hacks;
10.65 +import org.netbeans.modules.jackpot30.spi.PatternConvertor;
10.66 +import org.netbeans.modules.jackpot30.spi.refactoring.JackpotBasedRefactoring;
10.67 +import org.netbeans.modules.refactoring.api.Problem;
10.68 +import org.openide.util.Exceptions;
10.69 +import org.openide.util.MapFormat;
10.70 +
10.71 +/**
10.72 + *
10.73 + * @author lahvac
10.74 + */
10.75 +public class InvertBooleanRefactoringPluginImpl extends JackpotBasedRefactoring {
10.76 +
10.77 + private final InvertBooleanRefactoring invertBooleanRefactoring;
10.78 +
10.79 + public InvertBooleanRefactoringPluginImpl(InvertBooleanRefactoring replaceConstructorRefactoring) {
10.80 + super(replaceConstructorRefactoring);
10.81 + this.invertBooleanRefactoring = replaceConstructorRefactoring;
10.82 + }
10.83 +
10.84 + @Override
10.85 + public Problem preCheck() {
10.86 + return null;
10.87 + }
10.88 +
10.89 + @Override
10.90 + public Problem checkParameters() {
10.91 + return null;
10.92 + }
10.93 +
10.94 + @Override
10.95 + public Problem fastCheckParameters() {
10.96 + return null;
10.97 + }
10.98 +
10.99 + private static final String INVERT_FIXES =
10.100 + "=> ${newName-with-enclosing}$ $left != $right; :: matchesWithBind($val, \"$left == $right\")\n" +
10.101 + "=> ${newName-with-enclosing}$ $left == $right; :: matchesWithBind($val, \"$left != $right\")\n" +
10.102 + "=> ${newName-with-enclosing}$ $op; :: matchesWithBind($val, \"!($op)\")" +
10.103 + "=> ${newName-with-enclosing}$ $op; :: matchesWithBind($val, \"(!$op)\")" +
10.104 + "=> ${newName-with-enclosing}$ $op; :: !matchesAny($val, \"!($op)\") && matchesWithBind($val, \"!$op\")\n" +
10.105 + "=> ${newName-with-enclosing}$ false; :: matchesAny($val, \"true\")\n" +
10.106 + "=> ${newName-with-enclosing}$ true; :: matchesAny($val, \"false\")\n" +
10.107 + "=> ${newName-with-enclosing}$ !$val; :: otherwise\n";
10.108 +
10.109 + private static final String VAR_SCRIPT_TEMPLATE =
10.110 + " $enclosing.${originalName}$ :: $enclosing instanceof ${enclosing}$ && !parentMatches(\"$enclosing.${originalName}$ = $newVal\") && !parentMatches(\"!$enclosing.${originalName}$\")\n" +
10.111 + "=> !$enclosing.${newName}$\n" +
10.112 + ";;\n" +
10.113 + " !$enclosing.${originalName}$ :: $enclosing instanceof ${enclosing}$\n" +
10.114 + "=> $enclosing.${newName}$\n" +
10.115 + ";;\n" +
10.116 + " $enclosing.${originalName}$ = $val :: $enclosing instanceof ${enclosing}$ && !matchesAny($val, \"!$enclosing.${originalName}$\")\n" +
10.117 + INVERT_FIXES.replace(";", "").replace("${newName-with-enclosing}$", "$enclosing.${newName}$ =") +
10.118 + ";;\n";
10.119 +
10.120 + private static final String VAR_SCRIPT_TEMPLATE_STATIC =
10.121 + " ${enclosing}$.${originalName}$ :: !parentMatches(\"$enclosing.${originalName}$ = $newVal\") && !parentMatches(\"!$enclosing.${originalName}$\")\n" +
10.122 + "=> !${enclosing}$.${newName}$\n" +
10.123 + ";;\n" +
10.124 + " !${enclosing}$.${originalName}$\n" +
10.125 + "=> ${enclosing}$.${newName}$\n" +
10.126 + ";;\n" +
10.127 + " ${enclosing}$.${originalName}$ = $val :: !matchesAny($val, \"!$enclosing.${originalName}$\")\n" +
10.128 + INVERT_FIXES.replace(";", "").replace("${newName-with-enclosing}$", "${enclosing}$.${newName}$ =") +
10.129 + ";;\n";
10.130 +
10.131 + private static final String VAR_INIT =
10.132 + " $mods$ $type ${originalName}$ = $val;" +
10.133 + INVERT_FIXES.replace("${newName-with-enclosing}$", "$mods$ $type ${newName}$ =") +
10.134 + ";;";
10.135 +
10.136 + private static final String MTH_INIT =
10.137 + " return $val;" +
10.138 + INVERT_FIXES.replace("${newName-with-enclosing}$", "return ") +
10.139 + ";;";
10.140 +
10.141 + @Override
10.142 + protected void prepareAndConstructRule(final Context result) {
10.143 + final TreePathHandle original = invertBooleanRefactoring.getOriginal();
10.144 +
10.145 + try {
10.146 + ModificationResult mod = JavaSource.forFileObject(original.getFileObject()).runModificationTask(new Task<WorkingCopy>() {
10.147 +
10.148 + @Override
10.149 + public void run(final WorkingCopy parameter) throws Exception {
10.150 + parameter.toPhase(Phase.RESOLVED);
10.151 +
10.152 + final TreePath path = original.resolve(parameter);
10.153 + Map<String, String> arguments = new HashMap<String, String>();
10.154 + String scriptTemplate;
10.155 + Tree leaf = path.getLeaf();
10.156 + TypeElement parent = (TypeElement) parameter.getTrees().getElement(path.getParentPath());
10.157 +
10.158 + arguments.put("newName", invertBooleanRefactoring.getNewName());
10.159 + arguments.put("enclosing", parent.getQualifiedName().toString());
10.160 +
10.161 + if (leaf.getKind() == Kind.VARIABLE) {
10.162 + VariableTree var = (VariableTree) leaf;
10.163 +
10.164 + scriptTemplate = var.getModifiers().getFlags().contains(Modifier.STATIC) ? VAR_SCRIPT_TEMPLATE_STATIC : VAR_SCRIPT_TEMPLATE;
10.165 + arguments.put("originalName", var.getName().toString());
10.166 +
10.167 + if (var.getInitializer() != null) {
10.168 + MapFormat format = new MapFormat(arguments);
10.169 + format.setLeftBrace("${");
10.170 + format.setRightBrace("}$");
10.171 + String initFormat = format.format(VAR_INIT);
10.172 +
10.173 + Hacks.findHintsAndApplyFixes(parameter, PatternConvertor.create(initFormat), path, cancel);
10.174 + }
10.175 + } else if (leaf.getKind() == Kind.METHOD) {
10.176 + MethodTree mt = (MethodTree) leaf;
10.177 +
10.178 + arguments.put("originalName", mt.getName().toString());
10.179 +
10.180 + MapFormat format = new MapFormat(arguments);
10.181 + format.setLeftBrace("${");
10.182 + format.setRightBrace("}$");
10.183 + final String mthFormat = format.format(MTH_INIT);
10.184 +
10.185 + new TreePathScanner<Void, Void>() {
10.186 + @Override public Void visitReturn(ReturnTree node, Void p) {
10.187 + Hacks.findHintsAndApplyFixes(parameter, PatternConvertor.create(mthFormat), getCurrentPath(), cancel);
10.188 + return super.visitReturn(node, p);
10.189 + }
10.190 + @Override public Void visitClass(ClassTree node, Void p) {
10.191 + return null;
10.192 + }
10.193 + }.scan(path, null);
10.194 +
10.195 + parameter.rewrite(leaf, parameter.getTreeMaker().setLabel(leaf, invertBooleanRefactoring.getNewName()));
10.196 +
10.197 + StringBuilder parameters = new StringBuilder();
10.198 + StringBuilder constraints = new StringBuilder();
10.199 + int count = 1;
10.200 + for (VariableTree vt : mt.getParameters()) {
10.201 + if (count > 1) {
10.202 + parameters.append(", ");
10.203 + constraints.append(" && ");
10.204 + }
10.205 + parameters.append("$").append(count);
10.206 + constraints.append("$").append(count).append(" instanceof ").append(parameter.getTrees().getTypeMirror(new TreePath(new TreePath(path, vt), vt.getType())));
10.207 + count++;
10.208 + }
10.209 +
10.210 + String andConstraints = (constraints.length() > 0 ? " && " : "") + constraints;
10.211 +
10.212 + StringBuilder script = new StringBuilder();
10.213 +
10.214 + if (mt.getModifiers().getFlags().contains(Modifier.STATIC)) {
10.215 + script.append(" ${enclosing}$.<$T$>${originalName}$(").append(parameters).append(") :: !parentMatches(\"!$enclosing.${originalName}$($args$)\") ").append(andConstraints);
10.216 + script.append("=> !${enclosing}$.<$T$>${newName}$(").append(parameters).append(")\n");
10.217 + script.append(";;\n");
10.218 + script.append(" !${enclosing}$.<$T$>${originalName}$(").append(parameters).append(") :: ").append(constraints);
10.219 + script.append("=> ${enclosing}$.<$T$>${newName}$(").append(parameters).append(")\n");
10.220 + script.append(";;\n");
10.221 + } else {
10.222 + script.append(" $enclosing.<$T$>${originalName}$(").append(parameters).append(") :: $enclosing instanceof ${enclosing}$ && !parentMatches(\"!$enclosing.${originalName}$($args$)\") ").append(andConstraints);
10.223 + script.append("=> !$enclosing.<$T$>${newName}$(").append(parameters).append(")\n");
10.224 + script.append(";;\n");
10.225 + script.append(" !$enclosing.<$T$>${originalName}$(").append(parameters).append(") :: $enclosing instanceof ${enclosing}$ ").append(andConstraints);
10.226 + script.append("=> $enclosing.<$T$>${newName}$(").append(parameters).append(")\n");
10.227 + script.append(";;\n");
10.228 + }
10.229 +
10.230 + scriptTemplate = script.toString();
10.231 + } else {
10.232 + throw new UnsupportedOperationException();
10.233 + }
10.234 +
10.235 + MapFormat format = new MapFormat(arguments);
10.236 +
10.237 + format.setLeftBrace("${");
10.238 + format.setRightBrace("}$");
10.239 +
10.240 + result.addScript(parent.getQualifiedName().toString(), format.format(scriptTemplate), ScriptOptions.RUN/*, ScriptOptions.STORE*/);
10.241 + }
10.242 + });
10.243 +
10.244 + result.addModificationResult(mod);
10.245 + } catch (IOException ex) {
10.246 + Exceptions.printStackTrace(ex);
10.247 + }
10.248 + }
10.249 +
10.250 +}
11.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
11.2 +++ b/bridges/refactoring/src/org/netbeans/modules/jackpot30/refactoring/invertboolean/InvertBooleanRefactoringUI.java Sun Jan 16 13:56:54 2011 +0100
11.3 @@ -0,0 +1,133 @@
11.4 +/*
11.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
11.6 + *
11.7 + * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
11.8 + *
11.9 + * The contents of this file are subject to the terms of either the GNU
11.10 + * General Public License Version 2 only ("GPL") or the Common
11.11 + * Development and Distribution License("CDDL") (collectively, the
11.12 + * "License"). You may not use this file except in compliance with the
11.13 + * License. You can obtain a copy of the License at
11.14 + * http://www.netbeans.org/cddl-gplv2.html
11.15 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
11.16 + * specific language governing permissions and limitations under the
11.17 + * License. When distributing the software, include this License Header
11.18 + * Notice in each file and include the License file at
11.19 + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
11.20 + * particular file as subject to the "Classpath" exception as provided
11.21 + * by Sun in the GPL Version 2 section of the License file that
11.22 + * accompanied this code. If applicable, add the following below the
11.23 + * License Header, with the fields enclosed by brackets [] replaced by
11.24 + * your own identifying information:
11.25 + * "Portions Copyrighted [year] [name of copyright owner]"
11.26 + *
11.27 + * If you wish your version of this file to be governed by only the CDDL
11.28 + * or only the GPL Version 2, indicate your decision by adding
11.29 + * "[Contributor] elects to include this software in this distribution
11.30 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
11.31 + * single choice of license, a recipient has the option to distribute
11.32 + * your version of this file under either the CDDL, the GPL Version 2 or
11.33 + * to extend the choice of license to its licensees as provided above.
11.34 + * However, if you add GPL Version 2 code and therefore, elected the GPL
11.35 + * Version 2 license, then the option applies only if the new code is
11.36 + * made subject to such option by the copyright holder.
11.37 + *
11.38 + * Contributor(s):
11.39 + *
11.40 + * Portions Copyrighted 2010 Sun Microsystems, Inc.
11.41 + */
11.42 +
11.43 +package org.netbeans.modules.jackpot30.refactoring.invertboolean;
11.44 +
11.45 +import java.awt.Component;
11.46 +import javax.lang.model.SourceVersion;
11.47 +import javax.swing.event.ChangeListener;
11.48 +import org.netbeans.api.java.source.TreePathHandle;
11.49 +import org.netbeans.modules.refactoring.api.AbstractRefactoring;
11.50 +import org.netbeans.modules.refactoring.api.Problem;
11.51 +import org.netbeans.modules.refactoring.spi.ui.CustomRefactoringPanel;
11.52 +import org.netbeans.modules.refactoring.spi.ui.RefactoringUI;
11.53 +import org.openide.util.HelpCtx;
11.54 +
11.55 +/**
11.56 + *
11.57 + * @author lahvac
11.58 + */
11.59 +public class InvertBooleanRefactoringUI implements RefactoringUI {
11.60 +
11.61 + private final TreePathHandle path;
11.62 + private String name;
11.63 + private InvertBooleanRefactoringPanel panel;
11.64 +
11.65 + public InvertBooleanRefactoringUI(TreePathHandle path, String name) {
11.66 + this.path = path;
11.67 + this.name = name;
11.68 + }
11.69 +
11.70 + @Override
11.71 + public String getName() {
11.72 + return "InvertBooleanRefactoringUI";
11.73 + }
11.74 +
11.75 + @Override
11.76 + public String getDescription() {
11.77 + return "InvertBooleanRefactoringUI";
11.78 + }
11.79 +
11.80 + @Override
11.81 + public boolean isQuery() {
11.82 + return false;
11.83 + }
11.84 +
11.85 + @Override
11.86 + public CustomRefactoringPanel getPanel(final ChangeListener parent) {
11.87 + return new CustomRefactoringPanel() {
11.88 + @Override
11.89 + public void initialize() {
11.90 + panel.initialize(name);
11.91 + }
11.92 + @Override
11.93 + public Component getComponent() {
11.94 + if (panel == null) {
11.95 + panel = new InvertBooleanRefactoringPanel(parent);
11.96 + }
11.97 + return panel;
11.98 + }
11.99 + };
11.100 + }
11.101 +
11.102 + @Override
11.103 + public Problem setParameters() {
11.104 + this.name = panel.getName();
11.105 + return null;
11.106 + }
11.107 +
11.108 + @Override
11.109 + public Problem checkParameters() {
11.110 + String name = this.name != null ? this.name : panel.getName();
11.111 +
11.112 + if (name == null || name.length() == 0) {
11.113 + return new Problem(true, "No factory method name specified.");
11.114 + }
11.115 + if (!SourceVersion.isIdentifier(name)) {
11.116 + return new Problem(true, name + " is not an identifier.");
11.117 + }
11.118 + return null;
11.119 + }
11.120 +
11.121 + @Override
11.122 + public boolean hasParameters() {
11.123 + return true;
11.124 + }
11.125 +
11.126 + @Override
11.127 + public AbstractRefactoring getRefactoring() {
11.128 + return new InvertBooleanRefactoring(path, name);
11.129 + }
11.130 +
11.131 + @Override
11.132 + public HelpCtx getHelpCtx() {
11.133 + return null;
11.134 + }
11.135 +
11.136 +}
12.1 --- a/bridges/refactoring/src/org/netbeans/modules/jackpot30/refactoring/layer.xml Sun Jan 16 13:56:54 2011 +0100
12.2 +++ b/bridges/refactoring/src/org/netbeans/modules/jackpot30/refactoring/layer.xml Sun Jan 16 13:56:54 2011 +0100
12.3 @@ -6,6 +6,9 @@
12.4 <file name="org-netbeans-modules-jackpot30-refactoring-noconstructor-ReplaceConstructorAction.instance">
12.5 <attr name="instanceCreate" methodvalue="org.netbeans.modules.jackpot30.refactoring.noconstructor.ReplaceConstructorAction.create"/>
12.6 </file>
12.7 + <file name="org-netbeans-modules-jackpot30-refactoring-invertboolean-InvertBooleanAction.instance">
12.8 + <attr name="instanceCreate" methodvalue="org.netbeans.modules.jackpot30.refactoring.invertboolean.InvertBooleanAction.create"/>
12.9 + </file>
12.10 </folder>
12.11 </folder>
12.12 <folder name="Menu">
12.13 @@ -22,6 +25,10 @@
12.14 <attr name="originalFile" stringvalue="Actions/Refactoring/org-netbeans-modules-jackpot30-refactoring-noconstructor-ReplaceConstructorAction.instance"/>
12.15 <attr name="position" intvalue="2150"/>
12.16 </file>
12.17 + <file name="org-netbeans-modules-jackpot30-refactoring-invertboolean-InvertBooleanAction.shadow">
12.18 + <attr name="originalFile" stringvalue="Actions/Refactoring/org-netbeans-modules-jackpot30-refactoring-invertboolean-InvertBooleanAction.instance"/>
12.19 + <attr name="position" intvalue="2152"/>
12.20 + </file>
12.21 </folder>
12.22 </folder>
12.23 </filesystem>
13.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
13.2 +++ b/bridges/refactoring/test/unit/src/org/netbeans/modules/jackpot30/refactoring/invertboolean/InvertBooleanRefactoringPluginImplTest.java Sun Jan 16 13:56:54 2011 +0100
13.3 @@ -0,0 +1,192 @@
13.4 +/*
13.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
13.6 + *
13.7 + * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
13.8 + *
13.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
13.10 + * Other names may be trademarks of their respective owners.
13.11 + *
13.12 + * The contents of this file are subject to the terms of either the GNU
13.13 + * General Public License Version 2 only ("GPL") or the Common
13.14 + * Development and Distribution License("CDDL") (collectively, the
13.15 + * "License"). You may not use this file except in compliance with the
13.16 + * License. You can obtain a copy of the License at
13.17 + * http://www.netbeans.org/cddl-gplv2.html
13.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13.19 + * specific language governing permissions and limitations under the
13.20 + * License. When distributing the software, include this License Header
13.21 + * Notice in each file and include the License file at
13.22 + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
13.23 + * particular file as subject to the "Classpath" exception as provided
13.24 + * by Oracle in the GPL Version 2 section of the License file that
13.25 + * accompanied this code. If applicable, add the following below the
13.26 + * License Header, with the fields enclosed by brackets [] replaced by
13.27 + * your own identifying information:
13.28 + * "Portions Copyrighted [year] [name of copyright owner]"
13.29 + *
13.30 + * If you wish your version of this file to be governed by only the CDDL
13.31 + * or only the GPL Version 2, indicate your decision by adding
13.32 + * "[Contributor] elects to include this software in this distribution
13.33 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
13.34 + * single choice of license, a recipient has the option to distribute
13.35 + * your version of this file under either the CDDL, the GPL Version 2 or
13.36 + * to extend the choice of license to its licensees as provided above.
13.37 + * However, if you add GPL Version 2 code and therefore, elected the GPL
13.38 + * Version 2 license, then the option applies only if the new code is
13.39 + * made subject to such option by the copyright holder.
13.40 + *
13.41 + * Contributor(s):
13.42 + *
13.43 + * Portions Copyrighted 2011 Sun Microsystems, Inc.
13.44 + */
13.45 +
13.46 +package org.netbeans.modules.jackpot30.refactoring.invertboolean;
13.47 +
13.48 +import com.sun.source.tree.ClassTree;
13.49 +import com.sun.source.tree.CompilationUnitTree;
13.50 +import com.sun.source.tree.MethodTree;
13.51 +import com.sun.source.tree.VariableTree;
13.52 +import com.sun.source.util.TreePath;
13.53 +import org.netbeans.api.java.source.CompilationController;
13.54 +import org.netbeans.api.java.source.JavaSource;
13.55 +import org.netbeans.api.java.source.SourceUtils;
13.56 +import org.netbeans.api.java.source.Task;
13.57 +import org.netbeans.api.java.source.TreePathHandle;
13.58 +import org.netbeans.modules.jackpot30.spi.refactoring.JackpotBasedRefactoringTestBase;
13.59 +import org.netbeans.modules.parsing.api.indexing.IndexingManager;
13.60 +import org.netbeans.modules.parsing.impl.indexing.errors.TaskCache;
13.61 +import org.netbeans.modules.refactoring.api.RefactoringSession;
13.62 +import org.openide.filesystems.FileObject;
13.63 +
13.64 +/**
13.65 + *
13.66 + * @author lahvac
13.67 + */
13.68 +public class InvertBooleanRefactoringPluginImplTest extends JackpotBasedRefactoringTestBase {
13.69 +
13.70 + public InvertBooleanRefactoringPluginImplTest(String name) {
13.71 + super(name);
13.72 + }
13.73 +
13.74 + public void testInvertField1() throws Exception {
13.75 + writeFilesAndWaitForScan(src,
13.76 + new File("test/Test.java", "package test;\n public class Test {\n public boolean b = Boolean.getBoolean(\"\"); { b = Boolean.getBoolean(\"\"); if (b) System.err.println(1);\n } }\n"),
13.77 + new File("test/Use.java", "package test; public class Use { { new Test().b = Boolean.getBoolean(\"\"); if (new Test().b) System.err.println(1);\n } }")
13.78 + );
13.79 +
13.80 + performFieldTest();
13.81 +
13.82 + assertContent(src,
13.83 + new File("test/Test.java", "package test;\n public class Test {\n public boolean c = !Boolean.getBoolean(\"\"); { c = !Boolean.getBoolean(\"\"); if (!c) System.err.println(1);\n } }\n"),
13.84 + new File("test/Use.java", "package test; public class Use { { new Test().c = !Boolean.getBoolean(\"\"); if (!new Test().c) System.err.println(1);\n } }")/*,
13.85 + new File("META-INF/upgrade/test.Test.hint", "new test.Test($1, $2) :: $1 instanceof int && $2 instanceof java.util.List<java.lang.String> => test.Test.create($1, $2);;")*/
13.86 + );
13.87 + }
13.88 +
13.89 + public void testInvertField2() throws Exception {
13.90 + writeFilesAndWaitForScan(src,
13.91 + new File("test/Test.java", "package test;\n public class Test {\n public boolean b = Integer.getInteger(\"\") == 0; { b = Integer.getInteger(\"\") != 0; b = !b; boolean n1 = false; b = !n1; b = !(n1); b = (!n1); b = true; b = false; System.err.println(!b);\n } }\n")
13.92 + );
13.93 +
13.94 + performFieldTest();
13.95 +
13.96 + assertContent(src,
13.97 + new File("test/Test.java", "package test;\n public class Test {\n public boolean c = Integer.getInteger(\"\") != 0; { c = Integer.getInteger(\"\") == 0; c = !c; boolean n1 = false; c = n1; c = n1; c = n1; c = false; c = true; System.err.println(c);\n } }\n")/*,
13.98 + new File("META-INF/upgrade/test.Test.hint", "new test.Test($1, $2) :: $1 instanceof int && $2 instanceof java.util.List<java.lang.String> => test.Test.create($1, $2);;")*/
13.99 + );
13.100 + }
13.101 +
13.102 + public void testInvertFieldStatic() throws Exception {
13.103 + writeFilesAndWaitForScan(src,
13.104 + new File("test/Test.java", "package test;\n public class Test {\n public static boolean b = true; static { b = Integer.getInteger(\"\") != 0; \n } }\n")
13.105 + );
13.106 +
13.107 + performFieldTest();
13.108 +
13.109 + assertContent(src,
13.110 + new File("test/Test.java", "package test;\n public class Test {\n public static boolean c = false; static { " + /*XXX:*/ "Test." + "c = Integer.getInteger(\"\") == 0; \n } }\n")/*,
13.111 + new File("META-INF/upgrade/test.Test.hint", "new test.Test($1, $2) :: $1 instanceof int && $2 instanceof java.util.List<java.lang.String> => test.Test.create($1, $2);;")*/
13.112 + );
13.113 + }
13.114 +
13.115 + public void testInvertMethod() throws Exception {
13.116 + writeFilesAndWaitForScan(src,
13.117 + new File("test/Test.java", "package test;\n public class Test {\n public boolean b() { if (true) return Boolean.getBoolean(\"\"); else return false; } { if (b()) System.err.println(1); \n } }\n"),
13.118 + new File("test/Use.java", "package test; public class Use { { if (!new Test().b()) System.err.println(1);\n } }")
13.119 + );
13.120 +
13.121 + performMethodTest();
13.122 +
13.123 + assertContent(src,
13.124 + new File("test/Test.java", "package test;\n public class Test {\n public boolean c() { if (true) return !Boolean.getBoolean(\"\"); else return true; } { if (!c()) System.err.println(1); \n } }\n"),
13.125 + new File("test/Use.java", "package test; public class Use { { if (new Test().c()) System.err.println(1);\n } }")/*,
13.126 + new File("META-INF/upgrade/test.Test.hint", "new test.Test($1, $2) :: $1 instanceof int && $2 instanceof java.util.List<java.lang.String> => test.Test.create($1, $2);;")*/
13.127 + );
13.128 + }
13.129 +
13.130 + public void testInvertMethodStaticTypeParam() throws Exception {
13.131 + writeFilesAndWaitForScan(src,
13.132 + new File("test/Test.java", "package test;\n public class Test {\n public static <T> boolean b(T t) { return true; } { if (Test.<String>b(null)) System.err.println(1); \n } }\n")
13.133 + );
13.134 +
13.135 + performMethodTest();
13.136 +
13.137 + assertContent(src,
13.138 + new File("test/Test.java", "package test;\n public class Test {\n public static <T> boolean c(T t) { return false; } { if (!Test.<String>c(null)) System.err.println(1); \n } }\n")/*,
13.139 + new File("META-INF/upgrade/test.Test.hint", "new test.Test($1, $2) :: $1 instanceof int && $2 instanceof java.util.List<java.lang.String> => test.Test.create($1, $2);;")*/
13.140 + );
13.141 + }
13.142 +
13.143 + private void performFieldTest() throws Exception {
13.144 + final InvertBooleanRefactoring[] r = new InvertBooleanRefactoring[1];
13.145 + FileObject testFile = src.getFileObject("test/Test.java");
13.146 +
13.147 + JavaSource.forFileObject(testFile).runUserActionTask(new Task<CompilationController>() {
13.148 +
13.149 + public void run(CompilationController parameter) throws Exception {
13.150 + parameter.toPhase(JavaSource.Phase.RESOLVED);
13.151 + CompilationUnitTree cut = parameter.getCompilationUnit();
13.152 +
13.153 + VariableTree var = (VariableTree) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(1);
13.154 +
13.155 + TreePath tp = TreePath.getPath(cut, var);
13.156 + r[0] = new InvertBooleanRefactoring(TreePathHandle.create(tp, parameter), "c");
13.157 + }
13.158 + }, true);
13.159 +
13.160 + RefactoringSession rs = RefactoringSession.create("Session");
13.161 + r[0].prepare(rs);
13.162 + rs.doRefactoring(true);
13.163 +
13.164 + IndexingManager.getDefault().refreshIndex(src.getURL(), null);
13.165 + SourceUtils.waitScanFinished();
13.166 + assertEquals(false, TaskCache.getDefault().isInError(src, true));
13.167 + }
13.168 +
13.169 + private void performMethodTest() throws Exception {
13.170 + final InvertBooleanRefactoring[] r = new InvertBooleanRefactoring[1];
13.171 + FileObject testFile = src.getFileObject("test/Test.java");
13.172 +
13.173 + JavaSource.forFileObject(testFile).runUserActionTask(new Task<CompilationController>() {
13.174 +
13.175 + public void run(CompilationController parameter) throws Exception {
13.176 + parameter.toPhase(JavaSource.Phase.RESOLVED);
13.177 + CompilationUnitTree cut = parameter.getCompilationUnit();
13.178 +
13.179 + MethodTree var = (MethodTree) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(1);
13.180 +
13.181 + TreePath tp = TreePath.getPath(cut, var);
13.182 + r[0] = new InvertBooleanRefactoring(TreePathHandle.create(tp, parameter), "c");
13.183 + }
13.184 + }, true);
13.185 +
13.186 + RefactoringSession rs = RefactoringSession.create("Session");
13.187 + r[0].prepare(rs);
13.188 + rs.doRefactoring(true);
13.189 +
13.190 + IndexingManager.getDefault().refreshIndex(src.getURL(), null);
13.191 + SourceUtils.waitScanFinished();
13.192 + assertEquals(false, TaskCache.getDefault().isInError(src, true));
13.193 + }
13.194 +
13.195 +}