diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/NullAnnotationsQuickFixTest1d8.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/NullAnnotationsQuickFixTest1d8.java index 44c50a2f977..1101881310a 100644 --- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/NullAnnotationsQuickFixTest1d8.java +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/NullAnnotationsQuickFixTest1d8.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2017, 2020 GK Software AG and others. + * Copyright (c) 2017, 2022 GK Software AG and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -57,6 +57,7 @@ import org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal; import org.eclipse.jdt.internal.ui.JavaPlugin; +import org.eclipse.jdt.internal.ui.text.correction.AssistContext; /** * Those tests are made to run on Java Spider 1.8 . @@ -1208,6 +1209,39 @@ public void testBug513209d() throws Exception { assertEqualString(preview, buf.toString()); } @Test + public void testBug562891() throws Exception { + IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null); + StringBuilder buf= new StringBuilder(); + buf.append("package test1;\n"); + buf.append("import org.eclipse.jdt.annotation.*;\n"); + buf.append("@NonNullByDefault\n"); + buf.append("public class A {\n"); + buf.append(" private @Nullable String foo;\n"); + buf.append("}\n"); + ICompilationUnit cu = pack1.createCompilationUnit("A.java", buf.toString(), false, null); + AssistContext context= new AssistContext(cu, buf.toString().indexOf("foo"), 0); + ArrayList proposals= collectAssists(context, false); + + assertCorrectLabels(proposals); + + buf= new StringBuilder(); + buf.append("package test1;\n"); + buf.append("import org.eclipse.jdt.annotation.*;\n"); + buf.append("@NonNullByDefault\n"); + buf.append("public class A {\n"); + buf.append(" private @Nullable String foo;\n"); + buf.append("\n"); + buf.append(" public @Nullable String getFoo() {\n"); + buf.append(" return foo;\n"); + buf.append(" }\n"); + buf.append("\n"); + buf.append(" public void setFoo(@Nullable String foo) {\n"); + buf.append(" this.foo = foo;\n"); + buf.append(" }\n"); + buf.append("}\n"); + assertExpectedExistInProposals(proposals, new String[] {buf.toString()}); + } + @Test public void testBug525424() throws Exception { Hashtable options= JavaCore.getOptions(); try { diff --git a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/sef/SelfEncapsulateFieldRefactoring.java b/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/sef/SelfEncapsulateFieldRefactoring.java index 62c498664a2..31be3489176 100644 --- a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/sef/SelfEncapsulateFieldRefactoring.java +++ b/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/sef/SelfEncapsulateFieldRefactoring.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2020 IBM Corporation and others. + * Copyright (c) 2000, 2022 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -54,6 +54,7 @@ import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.core.dom.AbstractTypeDeclaration; +import org.eclipse.jdt.core.dom.Annotation; import org.eclipse.jdt.core.dom.AnonymousClassDeclaration; import org.eclipse.jdt.core.dom.Assignment; import org.eclipse.jdt.core.dom.Block; @@ -64,6 +65,7 @@ import org.eclipse.jdt.core.dom.Dimension; import org.eclipse.jdt.core.dom.Expression; import org.eclipse.jdt.core.dom.FieldDeclaration; +import org.eclipse.jdt.core.dom.IExtendedModifier; import org.eclipse.jdt.core.dom.IMethodBinding; import org.eclipse.jdt.core.dom.ITypeBinding; import org.eclipse.jdt.core.dom.IVariableBinding; @@ -647,6 +649,17 @@ private MethodDeclaration createSetterMethod(AST ast, ASTRewrite rewriter, Strin result.parameters().add(param); param.setName(ast.newSimpleName(fArgName)); param.setType((Type)rewriter.createCopyTarget(type)); + List modifiers= field.modifiers(); + for (IExtendedModifier modifier : modifiers) { + if (modifier.isAnnotation()) { + String annotationName= ((Annotation)modifier).getTypeName().getFullyQualifiedName(); + if ("NonNull".equals(annotationName) || "Nullable".equals(annotationName)) { //$NON-NLS-1$ //$NON-NLS-2$ + Annotation fieldAnnotation= (Annotation)rewriter.createCopyTarget((Annotation)modifier); + param.modifiers().add(fieldAnnotation); + } + } + } + List extraDimensions= DimensionRewrite.copyDimensions(fFieldDeclaration.extraDimensions(), rewriter); param.extraDimensions().addAll(extraDimensions); @@ -689,6 +702,16 @@ private MethodDeclaration createGetterMethod(AST ast, ASTRewrite rewriter, Strin MethodDeclaration result= ast.newMethodDeclaration(); result.setName(ast.newSimpleName(fGetterName)); result.modifiers().addAll(ASTNodeFactory.newModifiers(ast, createModifiers())); + List modifiers= field.modifiers(); + for (IExtendedModifier modifier : modifiers) { + if (modifier.isAnnotation()) { + String annotationName= ((Annotation)modifier).getTypeName().getFullyQualifiedName(); + if ("NonNull".equals(annotationName) || "Nullable".equals(annotationName)) { //$NON-NLS-1$ //$NON-NLS-2$ + Annotation fieldAnnotation= (Annotation)rewriter.createCopyTarget((Annotation)modifier); + result.modifiers().add(fieldAnnotation); + } + } + } Type returnType= DimensionRewrite.copyTypeAndAddDimensions(type, fFieldDeclaration.extraDimensions(), rewriter); result.setReturnType2(returnType);