Skip to content

Commit

Permalink
Bug 562891 - Null annotations not honoured by create getter/setter
Browse files Browse the repository at this point in the history
- fix SelfEncapsulateFieldRefactoring create getter and setter methods
  to copy over annotations from the base field
- add new test to NullAnnotationsQuickFixTest1d8
  • Loading branch information
jjohnstn committed Apr 28, 2022
1 parent 4a9572f commit 95fc3a4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 .
Expand Down Expand Up @@ -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<IJavaCompletionProposal> 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<String, String> options= JavaCore.getOptions();
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<IExtendedModifier> 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<Dimension> extraDimensions= DimensionRewrite.copyDimensions(fFieldDeclaration.extraDimensions(), rewriter);
param.extraDimensions().addAll(extraDimensions);

Expand Down Expand Up @@ -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<IExtendedModifier> 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);

Expand Down

0 comments on commit 95fc3a4

Please sign in to comment.