diff options
| author | Manju Mathew | 2013-07-24 13:52:11 +0000 |
|---|---|---|
| committer | Dani Megert | 2013-07-24 13:52:11 +0000 |
| commit | 74903cbca471e4ba0e503ada3a029c76046b5fcf (patch) | |
| tree | 4d54139e1aba2b78aedb8d707600f3ea28aca7f8 | |
| parent | 68906597e0b2e7bab42767bb4d21b85794e3634c (diff) | |
| download | eclipse.jdt.ui-74903cbca471e4ba0e503ada3a029c76046b5fcf.tar.gz eclipse.jdt.ui-74903cbca471e4ba0e503ada3a029c76046b5fcf.tar.xz eclipse.jdt.ui-74903cbca471e4ba0e503ada3a029c76046b5fcf.zip | |
Fixed bug 409520: [1.8][quick fix] "Add unimplemented methods" should
not create stubs for default methods
2 files changed, 206 insertions, 2 deletions
diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/QuickFixTest18.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/QuickFixTest18.java new file mode 100644 index 0000000000..384630cd48 --- /dev/null +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/QuickFixTest18.java @@ -0,0 +1,198 @@ +/******************************************************************************* + * Copyright (c) 2013 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * This is an implementation of an early-draft specification developed under the Java Community Process (JCP) and + * is made available for testing and evaluation purposes only. + * The code is not compatible with any specification of the JCP. + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.jdt.ui.tests.quickfix; + +import java.util.ArrayList; +import java.util.Hashtable; + +import junit.framework.Test; +import junit.framework.TestSuite; + +import org.eclipse.jdt.testplugin.JavaProjectHelper; +import org.eclipse.jdt.testplugin.TestOptions; + +import org.eclipse.jface.preference.IPreferenceStore; + +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.IPackageFragment; +import org.eclipse.jdt.core.IPackageFragmentRoot; +import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants; + +import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility; +import org.eclipse.jdt.internal.corext.template.java.CodeTemplateContextType; + +import org.eclipse.jdt.ui.PreferenceConstants; +import org.eclipse.jdt.ui.tests.core.Java18ProjectTestSetup; +import org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal; + +import org.eclipse.jdt.internal.ui.JavaPlugin; + + +public class QuickFixTest18 extends QuickFixTest { + + private static final Class THIS= QuickFixTest18.class; + + private IJavaProject fJProject1; + + private IPackageFragmentRoot fSourceFolder; + + public QuickFixTest18(String name) { + super(name); + } + + public static Test suite() { + return new Java18ProjectTestSetup(new TestSuite(THIS)); + } + + public static Test setUpTest(Test test) { + return new Java18ProjectTestSetup(test); + } + + @Override + protected void setUp() throws Exception { + Hashtable options= TestOptions.getDefaultOptions(); + options.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, JavaCore.SPACE); + options.put(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, "4"); + options.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION_ON_LOCAL_VARIABLE, JavaCore.DO_NOT_INSERT); + + + JavaCore.setOptions(options); + + IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore(); + store.setValue(PreferenceConstants.CODEGEN_ADD_COMMENTS, false); + + fJProject1= Java18ProjectTestSetup.getProject(); + + StubUtility.setCodeTemplate(CodeTemplateContextType.METHODSTUB_ID, "", null); + StubUtility.setCodeTemplate(CodeTemplateContextType.CONSTRUCTORSTUB_ID, "", null); + + fSourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src"); + } + + + @Override + protected void tearDown() throws Exception { + JavaProjectHelper.clear(fJProject1, Java18ProjectTestSetup.getDefaultClasspath()); + } + + public void testUnimplementedMethods() throws Exception { + IPackageFragment pack2= fSourceFolder.createPackageFragment("test2", false, null); + StringBuffer buf= new StringBuffer(); + buf.append("package test2;\n"); + buf.append("import java.io.IOException;\n"); + buf.append("public interface Inter {\n"); + buf.append(" int getCount(Object[] o) throws IOException;\n"); + buf.append(" static int staticMethod(Object[] o) throws IOException{return 10;}\n"); + buf.append(" default int defaultMethod(Object[] o) throws IOException{return 20;}\n"); + buf.append("}\n"); + pack2.createCompilationUnit("Inter.java", buf.toString(), false, null); + + + IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null); + buf= new StringBuffer(); + buf.append("package test1;\n"); + buf.append("import test2.Inter;\n"); + buf.append("public class E implements Inter{\n"); + buf.append("}\n"); + ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null); + + CompilationUnit astRoot= getASTRoot(cu); + ArrayList proposals= collectCorrections(cu, astRoot); + assertNumberOfProposals(proposals, 2); + assertCorrectLabels(proposals); + + CUCorrectionProposal proposal= (CUCorrectionProposal)proposals.get(1); + String preview1= getPreviewContent(proposal); + + buf= new StringBuffer(); + buf.append("package test1;\n"); + buf.append("import test2.Inter;\n"); + buf.append("public abstract class E implements Inter{\n"); + buf.append("}\n"); + String expected1= buf.toString(); + + proposal= (CUCorrectionProposal)proposals.get(0); + String preview2= getPreviewContent(proposal); + + buf= new StringBuffer(); + buf.append("package test1;\n"); + buf.append("import java.io.IOException;\n"); + buf.append("\n"); + buf.append("import test2.Inter;\n"); + buf.append("public class E implements Inter{\n"); + buf.append("\n"); + buf.append(" @Override\n"); + buf.append(" public int getCount(Object[] o) throws IOException {\n"); + buf.append(" return 0;\n"); + buf.append(" }\n"); + buf.append("}\n"); + String expected2= buf.toString(); + + assertEqualStringsIgnoreOrder(new String[] { preview1, preview2 }, new String[] { expected1, expected2 }); + + } + + public void testUnimplementedMethods2() throws Exception { + StringBuffer buf= new StringBuffer(); + IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null); + buf= new StringBuffer(); + buf.append("package test1;\n"); + buf.append("public class MyString implements CharSequence{\n"); + buf.append("}\n"); + ICompilationUnit cu= pack1.createCompilationUnit("MyString.java", buf.toString(), false, null); + + CompilationUnit astRoot= getASTRoot(cu); + ArrayList proposals= collectCorrections(cu, astRoot, 3); + assertNumberOfProposals(proposals, 2); + assertCorrectLabels(proposals); + + CUCorrectionProposal proposal= (CUCorrectionProposal)proposals.get(1); + String preview1= getPreviewContent(proposal); + + buf= new StringBuffer(); + buf.append("package test1;\n"); + buf.append("public abstract class MyString implements CharSequence{\n"); + buf.append("}\n"); + String expected1= buf.toString(); + + proposal= (CUCorrectionProposal)proposals.get(0); + String preview2= getPreviewContent(proposal); + + buf= new StringBuffer(); + buf.append("package test1;\n"); + buf.append("public class MyString implements CharSequence{\n"); + buf.append("\n"); + buf.append(" @Override\n"); + buf.append(" public char charAt(int arg0) {\n"); + buf.append(" return 0;\n"); + buf.append(" }\n\n"); + buf.append(" @Override\n"); + buf.append(" public int length() {\n"); + buf.append(" return 0;\n"); + buf.append(" }\n\n"); + buf.append(" @Override\n"); + buf.append(" public CharSequence subSequence(int arg0, int arg1) {\n"); + buf.append(" return null;\n"); + buf.append(" }\n"); + buf.append("}\n"); + String expected2= buf.toString(); + + assertEqualStringsIgnoreOrder(new String[] { preview1, preview2 }, new String[] { expected1, expected2 }); + + } +} diff --git a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/codemanipulation/StubUtility2.java b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/codemanipulation/StubUtility2.java index 5b2be40713..16efc180fb 100644 --- a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/codemanipulation/StubUtility2.java +++ b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/codemanipulation/StubUtility2.java @@ -5,6 +5,10 @@ * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * + * This is an implementation of an early-draft specification developed under the Java Community Process (JCP) and + * is made available for testing and evaluation purposes only. + * The code is not compatible with any specification of the JCP. + * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ @@ -547,8 +551,10 @@ public final class StubUtility2 { if (impl == null || !Bindings.isVisibleInHierarchy(impl, currPack)) { if (impl != null) allMethods.remove(impl); - toImplement.add(curr); - allMethods.add(curr); + if (Modifier.isAbstract(curr.getModifiers())) { + toImplement.add(curr); + allMethods.add(curr); + } } } ITypeBinding[] superInterfaces= typeBinding.getInterfaces(); |
