Missing return statement fix is now part of the distribution.
1.1 --- a/javahints/nbproject/project.properties Mon Feb 06 19:35:42 2012 +0100
1.2 +++ b/javahints/nbproject/project.properties Sat Feb 11 18:09:19 2012 +0100
1.3 @@ -50,7 +50,7 @@
1.4 auxiliary.org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.text-limit-width=80
1.5 javac.compilerargs=-Xlint:unchecked
1.6 javac.source=1.6
1.7 -spec.version.base=2.52.0
1.8 +spec.version.base=2.53.0
1.9
1.10 nbm.needs.restart=true
1.11 requires.nb.javac=true
2.1 --- a/javahints/src/org/netbeans/modules/javahints/MissingReturn.java Mon Feb 06 19:35:42 2012 +0100
2.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
2.3 @@ -1,202 +0,0 @@
2.4 -/*
2.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2.6 - *
2.7 - * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
2.8 - *
2.9 - * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
2.10 - * Other names may be trademarks of their respective owners.
2.11 - *
2.12 - * The contents of this file are subject to the terms of either the GNU
2.13 - * General Public License Version 2 only ("GPL") or the Common
2.14 - * Development and Distribution License("CDDL") (collectively, the
2.15 - * "License"). You may not use this file except in compliance with the
2.16 - * License. You can obtain a copy of the License at
2.17 - * http://www.netbeans.org/cddl-gplv2.html
2.18 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
2.19 - * specific language governing permissions and limitations under the
2.20 - * License. When distributing the software, include this License Header
2.21 - * Notice in each file and include the License file at
2.22 - * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
2.23 - * particular file as subject to the "Classpath" exception as provided
2.24 - * by Oracle in the GPL Version 2 section of the License file that
2.25 - * accompanied this code. If applicable, add the following below the
2.26 - * License Header, with the fields enclosed by brackets [] replaced by
2.27 - * your own identifying information:
2.28 - * "Portions Copyrighted [year] [name of copyright owner]"
2.29 - *
2.30 - * If you wish your version of this file to be governed by only the CDDL
2.31 - * or only the GPL Version 2, indicate your decision by adding
2.32 - * "[Contributor] elects to include this software in this distribution
2.33 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
2.34 - * single choice of license, a recipient has the option to distribute
2.35 - * your version of this file under either the CDDL, the GPL Version 2 or
2.36 - * to extend the choice of license to its licensees as provided above.
2.37 - * However, if you add GPL Version 2 code and therefore, elected the GPL
2.38 - * Version 2 license, then the option applies only if the new code is
2.39 - * made subject to such option by the copyright holder.
2.40 - *
2.41 - * Contributor(s):
2.42 - *
2.43 - * Portions Copyrighted 2011 Sun Microsystems, Inc.
2.44 - */
2.45 -package org.netbeans.modules.javahints;
2.46 -
2.47 -import com.sun.source.tree.*;
2.48 -import com.sun.source.tree.Tree.Kind;
2.49 -import com.sun.source.util.TreePath;
2.50 -import java.util.*;
2.51 -import javax.lang.model.type.TypeKind;
2.52 -import javax.lang.model.type.TypeMirror;
2.53 -import org.netbeans.api.annotations.common.NonNull;
2.54 -import org.netbeans.api.java.source.*;
2.55 -import org.netbeans.api.java.source.JavaSource.Phase;
2.56 -import org.netbeans.modules.java.hints.errors.ChangeMethodReturnType;
2.57 -import org.netbeans.modules.java.hints.errors.Utilities;
2.58 -import org.netbeans.modules.java.hints.jackpot.spi.JavaFix;
2.59 -import org.netbeans.modules.java.hints.spi.ErrorRule;
2.60 -import org.netbeans.spi.editor.hints.ChangeInfo;
2.61 -import org.netbeans.spi.editor.hints.Fix;
2.62 -import org.openide.filesystems.FileObject;
2.63 -import org.openide.util.NbBundle;
2.64 -
2.65 -/**
2.66 - *
2.67 - * @author lahvac
2.68 - */
2.69 -public class MissingReturn implements ErrorRule<Void> {
2.70 -
2.71 - private final static Set<String> CODES = new HashSet<String>(Arrays.asList(
2.72 - "compiler.err.missing.ret.stmt"
2.73 - ));
2.74 -
2.75 - @Override
2.76 - public Set<String> getCodes() {
2.77 - return CODES;
2.78 - }
2.79 -
2.80 - @Override
2.81 - public List<Fix> run(CompilationInfo info, String diagnosticKey, int offset, TreePath treePath, ErrorRule.Data<Void> data) {
2.82 - List<Fix> result = new ArrayList<Fix>();
2.83 -
2.84 - treePath = info.getTreeUtilities().pathFor(offset - 1);
2.85 -
2.86 - TreePath method = treePath.getParentPath();
2.87 - MethodTree mt = (MethodTree) method.getLeaf();
2.88 - TypeMirror type = info.getTrees().getTypeMirror(new TreePath(method, mt.getReturnType()));
2.89 -
2.90 - result.add(createChangeToTypeFix(info, treePath.getParentPath(), info.getTypes().getNoType(TypeKind.VOID)));
2.91 -
2.92 - if (type != null && treePath.getLeaf().getKind() == Kind.BLOCK) {
2.93 - result.add(new AddReturnFixImpl(info, treePath, type.getKind()));
2.94 - }
2.95 -
2.96 - return result;
2.97 - }
2.98 -
2.99 - @Override
2.100 - public String getId() {
2.101 - return MissingReturn.class.getName();
2.102 - }
2.103 -
2.104 - @Override
2.105 - public String getDisplayName() {
2.106 - return NbBundle.getMessage(MissingReturn.class, "DN_MissingReturn");
2.107 - }
2.108 -
2.109 - @Override
2.110 - public void cancel() {}
2.111 -
2.112 - private static final class AddReturnFixImpl implements Fix {
2.113 -
2.114 - private final TreePathHandle targetBlock;
2.115 - private final TypeKind targetKind;
2.116 -
2.117 - public AddReturnFixImpl(CompilationInfo info, TreePath block, TypeKind kind) {
2.118 - this.targetBlock = TreePathHandle.create(block, info);
2.119 - this.targetKind = kind;
2.120 - }
2.121 -
2.122 - @Override
2.123 - public String getText() {
2.124 - return NbBundle.getMessage(MissingReturn.class, "FIX_AddReturn");
2.125 - }
2.126 -
2.127 - public ChangeInfo implement() throws Exception {
2.128 - FileObject file = targetBlock.getFileObject();
2.129 - JavaSource js = JavaSource.forFileObject(file);
2.130 -
2.131 - ModificationResult mr = js.runModificationTask(new Task<WorkingCopy>() {
2.132 - public void run(WorkingCopy wc) throws Exception {
2.133 - wc.toPhase(Phase.RESOLVED);
2.134 -
2.135 - TreePath block = targetBlock.resolve(wc);
2.136 -
2.137 - if (block == null) {
2.138 - return ; //TODO: log
2.139 - }
2.140 -
2.141 - //TODO:copied from NotInitializedVariable!
2.142 - Object value;
2.143 - if (targetKind.isPrimitive()) {
2.144 - if (targetKind == TypeKind.BOOLEAN) {
2.145 - value = false;
2.146 - }
2.147 - else {
2.148 - value = 0;
2.149 - }
2.150 - }
2.151 - else {
2.152 - value = null;
2.153 - }
2.154 -
2.155 - TreeMaker make = wc.getTreeMaker();
2.156 - LiteralTree returnValue = make.Literal(value);
2.157 - BlockTree blockTree = (BlockTree) block.getLeaf();
2.158 -
2.159 - wc.tag(returnValue, Utilities.TAG_SELECT);
2.160 - wc.rewrite(blockTree, make.addBlockStatement(blockTree, make.Return(returnValue)));
2.161 - }
2.162 - });
2.163 -
2.164 - return Utilities.commitAndComputeChangeInfo(file, mr);
2.165 - }
2.166 -
2.167 - }
2.168 -
2.169 - public static Fix createChangeToTypeFix(CompilationInfo info, TreePath method, @NonNull TypeMirror newType) {
2.170 - return JavaFix.toEditorFix(new FixImpl(info, method, TypeMirrorHandle.create(newType), info.getTypeUtilities().getTypeName(newType).toString()));
2.171 - }
2.172 -
2.173 - private static final class FixImpl extends JavaFix {
2.174 -
2.175 - private final TypeMirrorHandle targetTypeHandle;
2.176 - private final String targetTypeDN;
2.177 -
2.178 - public FixImpl(CompilationInfo info, TreePath tp, TypeMirrorHandle targetTypeHandle, String targetTypeDN) {
2.179 - super(info, tp);
2.180 - this.targetTypeHandle = targetTypeHandle;
2.181 - this.targetTypeDN = targetTypeDN;
2.182 - }
2.183 -
2.184 - @Override
2.185 - protected String getText() {
2.186 - return NbBundle.getMessage(ChangeMethodReturnType.class, "FIX_ChangeMethodReturnType", targetTypeDN);
2.187 - }
2.188 -
2.189 - @Override
2.190 - protected void performRewrite(WorkingCopy wc, TreePath tp, boolean canShowUI) {
2.191 - TypeMirror targetType = targetTypeHandle.resolve(wc);
2.192 -
2.193 - if (targetType == null) {
2.194 - //XXX: log
2.195 - return ;
2.196 - }
2.197 -
2.198 - MethodTree mt = (MethodTree) tp.getLeaf();
2.199 - TreeMaker make = wc.getTreeMaker();
2.200 -
2.201 - wc.rewrite(mt.getReturnType(), make.Type(targetType));
2.202 - }
2.203 -
2.204 - }
2.205 -}
3.1 --- a/javahints/test/unit/src/org/netbeans/modules/javahints/MissingReturnTest.java Mon Feb 06 19:35:42 2012 +0100
3.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
3.3 @@ -1,89 +0,0 @@
3.4 -/*
3.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3.6 - *
3.7 - * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
3.8 - *
3.9 - * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
3.10 - * Other names may be trademarks of their respective owners.
3.11 - *
3.12 - * The contents of this file are subject to the terms of either the GNU
3.13 - * General Public License Version 2 only ("GPL") or the Common
3.14 - * Development and Distribution License("CDDL") (collectively, the
3.15 - * "License"). You may not use this file except in compliance with the
3.16 - * License. You can obtain a copy of the License at
3.17 - * http://www.netbeans.org/cddl-gplv2.html
3.18 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
3.19 - * specific language governing permissions and limitations under the
3.20 - * License. When distributing the software, include this License Header
3.21 - * Notice in each file and include the License file at
3.22 - * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
3.23 - * particular file as subject to the "Classpath" exception as provided
3.24 - * by Oracle in the GPL Version 2 section of the License file that
3.25 - * accompanied this code. If applicable, add the following below the
3.26 - * License Header, with the fields enclosed by brackets [] replaced by
3.27 - * your own identifying information:
3.28 - * "Portions Copyrighted [year] [name of copyright owner]"
3.29 - *
3.30 - * If you wish your version of this file to be governed by only the CDDL
3.31 - * or only the GPL Version 2, indicate your decision by adding
3.32 - * "[Contributor] elects to include this software in this distribution
3.33 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
3.34 - * single choice of license, a recipient has the option to distribute
3.35 - * your version of this file under either the CDDL, the GPL Version 2 or
3.36 - * to extend the choice of license to its licensees as provided above.
3.37 - * However, if you add GPL Version 2 code and therefore, elected the GPL
3.38 - * Version 2 license, then the option applies only if the new code is
3.39 - * made subject to such option by the copyright holder.
3.40 - *
3.41 - * Contributor(s):
3.42 - *
3.43 - * Portions Copyrighted 2011 Sun Microsystems, Inc.
3.44 - */
3.45 -package org.netbeans.modules.javahints;
3.46 -
3.47 -import com.sun.source.util.TreePath;
3.48 -import java.util.List;
3.49 -import org.netbeans.api.java.source.CompilationInfo;
3.50 -import org.netbeans.modules.java.hints.infrastructure.ErrorHintsTestBase;
3.51 -import org.netbeans.spi.editor.hints.Fix;
3.52 -import org.openide.util.NbBundle;
3.53 -
3.54 -/**
3.55 - *
3.56 - * @author lahvac
3.57 - */
3.58 -public class MissingReturnTest extends ErrorHintsTestBase {
3.59 -
3.60 - public MissingReturnTest(String name) {
3.61 - super(name);
3.62 - }
3.63 -
3.64 - public void testReturnTypeToVoid() throws Exception {
3.65 - performFixTest("test/Test.java",
3.66 - "package test; public class Test { private String t() { }| }",
3.67 - "FIX_ChangeMethodReturnType void",
3.68 - "package test; public class Test { private void t() { } }");
3.69 - }
3.70 -
3.71 - public void testAddReturn() throws Exception {
3.72 - performFixTest("test/Test.java",
3.73 - "package test; public class Test { private String t() {\n}| }",
3.74 - "FIX_AddReturn",
3.75 - "package test; public class Test { private String t() { return null; } }");
3.76 - }
3.77 -
3.78 - @Override
3.79 - protected List<Fix> computeFixes(CompilationInfo info, int pos, TreePath path) throws Exception {
3.80 - return new MissingReturn().run(info, null, pos, path, null);
3.81 - }
3.82 -
3.83 - @Override
3.84 - protected String toDebugString(CompilationInfo info, Fix f) {
3.85 - return f.getText();
3.86 - }
3.87 -
3.88 - static {
3.89 - NbBundle.setBranding("test");
3.90 - }
3.91 -
3.92 -}