Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020 Microsoft Corporation. and others.
* Copyright (c) 2020, 2026 Microsoft Corporation. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -9,6 +9,7 @@
*
* Contributors:
* Microsoft Corporation - initial API and implementation
* IBM Corporation - Pattern for overriden methods
*******************************************************************************/

package org.eclipse.jdt.ls.core.internal.preferences;
Expand Down Expand Up @@ -138,7 +139,7 @@ public class CodeTemplatePreferences {
/**
* Default value for override comments
*/
public static final String CODETEMPLATE_OVERRIDECOMMENT_DEFAULT = "";
public static final String CODETEMPLATE_OVERRIDECOMMENT_DEFAULT = "/** (non-Javadoc)\n" + " * ${see_to_overridden}\n" + " */";

/**
* Default value for method comments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1138,4 +1138,73 @@ public void testAddRecordCommentWithGenerics() throws Exception {
assertCodeActions(cu, e1);
}

@Test
public void testAddJavadocForOverriddenMethod() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("/**\n");
buf.append(" */\n");
buf.append("public class E {\n");
buf.append(" @Override\n");
buf.append(" public String toString() {\n");
buf.append(" return \"E []\";\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("/**\n");
buf.append(" */\n");
buf.append("public class E {\n");
buf.append(" /** (non-Javadoc)\n");
buf.append(" * @see java.lang.Object#toString()\n");
buf.append(" */\n");
buf.append(" @Override\n");
buf.append(" public String toString() {\n");
buf.append(" return \"E []\";\n");
buf.append(" }\n");
buf.append("}\n");
Expected e1 = new Expected("Add Javadoc comment", buf.toString());
assertCodeActions(cu, e1);
}

@Test
public void testAddJavadocForOverriddenMethodInRecord() throws Exception {
Map<String, String> options17 = new HashMap<>(fJProject1.getOptions(false));
JavaModelUtil.setComplianceOptions(options17, JavaCore.VERSION_17);
fJProject1.setOptions(options17);

IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("/**\n");
buf.append(" */\n");
buf.append("public record Person(String name, int age) {\n");
buf.append(" @Override\n");
buf.append(" public String toString() {\n");
buf.append(" return name + \" (\" + age + \")\";\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("Person.java", buf.toString(), false, null);

buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("/**\n");
buf.append(" */\n");
buf.append("public record Person(String name, int age) {\n");
buf.append(" /** (non-Javadoc)\n");
buf.append(" * @see java.lang.Record#toString()\n");
buf.append(" */\n");
buf.append(" @Override\n");
buf.append(" public String toString() {\n");
buf.append(" return name + \" (\" + age + \")\";\n");
buf.append(" }\n");
buf.append("}\n");
Expected e1 = new Expected("Add Javadoc comment", buf.toString());
assertCodeActions(cu, e1);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -3775,11 +3775,17 @@ public class E {
String x;
int y;

/** (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
return Objects.hash(x);
}

/** (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
Expand All @@ -3798,6 +3804,7 @@ public void foo(Integer a) {
}
""";


String after2 = """
package test1;
public class E {
Expand Down Expand Up @@ -3862,11 +3869,17 @@ public class E {
String x;
int y;

/** (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
return Objects.hash(x, Integer.valueOf(y));
}

/** (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,15 @@ public void testResolveCodeAction_SourceActions() throws Exception {
buf.append(" private void hello() {\n");
buf.append(" }\n");
buf.append("\n");
buf.append(" /** (non-Javadoc)\n");
buf.append(" * @see java.lang.Object#toString()\n");
buf.append(" */\n");
buf.append(" @Override\n");
buf.append(" public String toString() {\n");
buf.append(" return \"E []\";\n");
buf.append(" }\n");
buf.append("}\n");

assertEquals(buf.toString(), actual);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@ public void testGenerateToString_customizedSettings() throws ValidateEditExcepti
+ " int id;\r\n"
+ " List<String> aList;\r\n"
+ " String[] arrays;\r\n"
+ " /** (non-Javadoc)\r\n"
+ " * @see java.lang.Object#toString()\r\n"
+ " */\r\n"
+ " @Override\r\n"
+ " public String toString() {\r\n"
+ " final int maxLen = 10;\r\n"
Expand All @@ -257,6 +260,7 @@ public void testGenerateToString_customizedSettings() throws ValidateEditExcepti
+ " return builder.toString();\r\n"
+ " }\r\n"
+ "}";

/* @formatter:on */

compareSource(expected, unit.getSource());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,9 @@ public void testGenerateHashCodeEquals_generateComments() throws ValidateEditExc
" double rate;\r\n" +
" Cloneable[] anArray;\r\n" +
" List<String> aList;\r\n" +
" /** (non-Javadoc)\r\n" +
" * @see java.lang.Object#hashCode()\r\n" +
" */\r\n" +
" @Override\r\n" +
" public int hashCode() {\r\n" +
" final int prime = 31;\r\n" +
Expand All @@ -431,6 +434,9 @@ public void testGenerateHashCodeEquals_generateComments() throws ValidateEditExc
" result = prime * result + ((aList == null) ? 0 : aList.hashCode());\r\n" +
" return result;\r\n" +
" }\r\n" +
" /** (non-Javadoc)\r\n" +
" * @see java.lang.Object#equals(java.lang.Object)\r\n" +
" */\r\n" +
" @Override\r\n" +
" public boolean equals(Object obj) {\r\n" +
" if (this == obj)\r\n" +
Expand Down Expand Up @@ -459,6 +465,7 @@ public void testGenerateHashCodeEquals_generateComments() throws ValidateEditExc
" return true;\r\n" +
" }\r\n" +
"}";

/* @formatter:on */

compareSource(expected, unit.getSource());
Expand Down
Loading