001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: */
028:
029: package org.netbeans.modules.java.hints.errors;
030:
031: import com.sun.source.util.TreePath;
032: import java.util.List;
033: import org.netbeans.api.java.source.CompilationInfo;
034: import org.netbeans.modules.java.hints.infrastructure.ErrorHintsTestBase;
035: import org.netbeans.spi.editor.hints.Fix;
036:
037: /**
038: *
039: * @author Jan Lahoda
040: */
041: public class AddCastTest extends ErrorHintsTestBase {
042:
043: public AddCastTest(String testName) {
044: super (testName);
045: }
046:
047: public void test117868() throws Exception {
048: performFixTest(
049: "test/Test.java",
050: "package test; public class Test {private void test() {int length = 0; byte b = |length & 0xFF00; } }",
051: "[AddCastFix:...length&0xFF00:byte]",
052: "package test; public class Test {private void test() {int length = 0; byte b = (byte) (length & 0xFF00); } }");
053: }
054:
055: public void test118284() throws Exception {
056: performFixTest(
057: "test/Test.java",
058: "package test; public class Test {private Object[][] o; private String test() {return |o[0][0];} }",
059: "[AddCastFix:...o[][]:String]",
060: "package test; public class Test {private Object[][] o; private String test() {return (String) o[0][0];} }");
061: }
062:
063: public void testMethodInvocation1() throws Exception {
064: performFixTest(
065: "test/Test.java",
066: "package test; public class Test {private void test() {Object o = null; |x(o);} private void x(String s) {}}",
067: "[AddCastFix:...o:String]",
068: "package test; public class Test {private void test() {Object o = null; x((String) o);} private void x(String s) {}}");
069: }
070:
071: public void testMethodInvocation2() throws Exception {
072: performFixTest(
073: "test/Test.java",
074: "package test; public class Test {private void test() {java.util.List<String> l = null; Object o = null; |l.add(o);} }",
075: "[AddCastFix:...o:String]",
076: "package test; public class Test {private void test() {java.util.List<String> l = null; Object o = null; l.add((String) o);} }");
077: }
078:
079: public void testNewClass1() throws Exception {
080: performFixTest(
081: "test/Test.java",
082: "package test; public class Test {private static void test() {Object o = null; |new Test(o);} public Test(String s) {} }",
083: "[AddCastFix:...o:String]",
084: "package test; public class Test {private static void test() {Object o = null; new Test((String) o);} public Test(String s) {} }");
085: }
086:
087: @Override
088: protected List<Fix> computeFixes(CompilationInfo info, int pos,
089: TreePath path) throws Exception {
090: return new AddCast().run(info, null, pos, path, null);
091: }
092:
093: @Override
094: protected String toDebugString(CompilationInfo info, Fix f) {
095: if (f instanceof AddCastFix) {
096: return ((AddCastFix) f).toDebugString();
097: }
098:
099: return super.toDebugString(info, f);
100: }
101:
102: }
|