mbien commented on code in PR #8197:
URL: https://github.com/apache/netbeans/pull/8197#discussion_r1929843676
##########
java/java.source.base/src/org/netbeans/modules/java/source/base/SourceLevelUtils.java:
##########
@@ -34,6 +34,13 @@ public class SourceLevelUtils {
public static final Source JDK1_9 = Source.lookup("9");
public static final Source JDK14 = Source.lookup("14");
public static final Source JDK15 = Source.lookup("15");
+ public static final Source JDK16 = Source.lookup("16");
+ public static final Source JDK17 = Source.lookup("17");
+ // for next release:
+// public static final Source JDK18 = Source.lookup("18");
+// public static final Source JDK19 = Source.lookup("19");
+// public static final Source JDK20 = Source.lookup("20");
+// public static final Source JDK21 = Source.lookup("21");
Review Comment:
doesn't look like this change is needed JDK16/JDK17 isn't used.
##########
java/refactoring.java/test/unit/src/org/netbeans/modules/refactoring/java/test/RefactoringTestBase.java:
##########
@@ -109,33 +171,30 @@ protected void verifyContent(FileObject sourceRoot,
File... files) throws Except
while (!todo.isEmpty()) {
FileObject file = todo.remove(0);
- if (file.isData()) {
+ if (file.isData()) { // normal file
content.put(FileUtil.getRelativePath(sourceRoot, file),
copyFileToString(FileUtil.toFile(file)));
- } else {
+ } else { //folder?
Review Comment:
yes that is a folder, remove question mark :)
##########
java/java.source.base/test/unit/src/org/netbeans/modules/java/source/save/RecordFormattingTest.java:
##########
Review Comment:
this test can be removed since it is already in:
https://github.com/apache/netbeans/blob/445977fa502ef748c7773caa2334be1fe378b12d/java/java.source.base/test/unit/src/org/netbeans/modules/java/source/save/FormatingTest.java#L6688
##########
java/java.source.base/src/org/netbeans/modules/java/source/pretty/VeryPretty.java:
##########
@@ -640,9 +641,17 @@ public String getMethodHeader(MethodTree t, String s) {
print(tree.name);
s = replace(s, NAME);
}
- print('(');
- wrapTrees(tree.params, WrapStyle.WRAP_NEVER, out.col);
- print(')');
+ boolean isRecord = enclClass.getKind()== Kind.RECORD;
+ // return type null makes this method a constructor
+ boolean isCandidateCompactCtor = isRecord && null == t.getReturnType();
+ if (isCandidateCompactCtor) {
+ System.err.println("spotted compact "+ t.toString());
+ }
Review Comment:
no err/out println in production code
##########
java/refactoring.java/test/unit/src/org/netbeans/modules/refactoring/java/test/RefactoringTestBase.java:
##########
@@ -400,7 +463,8 @@ protected static boolean problemIsFatal(List<Problem>
problems) {
return false;
}
- private static final int RETRIES = 3;
+// private static final int RETRIES = 3;
+ private static final int RETRIES = 1;
Review Comment:
interesting. I didn't know that some tests had retry loops implemented.
please don't change the settings here. This would have to be looked at
separately. We have have retry scripts in CI already for well known test suites.
IMO: problematic tests have to be isolated and run separately, only this
makes it observable from the outside how bad something is without hiding a
problem. But this is beyond the scope of this PR.
##########
java/java.source.base/src/org/netbeans/modules/java/source/pretty/VeryPretty.java:
##########
@@ -977,6 +986,9 @@ private void printEnumConstants(java.util.List<? extends
JCTree> defs, boolean f
@Override
public void visitMethodDef(JCMethodDecl tree) {
+ boolean methodIsConstructor = null==tree.getReturnType();
+ boolean enclosingIsRecord= enclClass.getKind() == Kind.RECORD;
+ boolean paramsIsComponents= paramsIsComponents(tree.getParameters());
Review Comment:
nitpick:
```java
boolean methodIsConstructor = tree.getReturnType() == null;
boolean enclosingIsRecord = enclClass.getKind() == Kind.RECORD;
boolean paramsIsComponents =
paramsIsComponents(tree.getParameters());
```
##########
java/refactoring.java/test/unit/src/org/netbeans/modules/refactoring/java/test/RefactoringTestBase.java:
##########
@@ -76,15 +78,46 @@ public class RefactoringTestBase extends NbTestCase {
public RefactoringTestBase(String name) {
super(name);
- sourcelevel = "1.6";
+ sourcelevel = "17";//"1.6";//??
Review Comment:
17 is probably not a good default. Lets not change this value in the base
class.
##########
java/refactoring.java/test/unit/src/org/netbeans/modules/refactoring/java/test/InnerOuterRecordTest.java:
##########
@@ -0,0 +1,713 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.modules.refactoring.java.test;
+
+import com.sun.source.tree.ClassTree;
+import com.sun.source.tree.CompilationUnitTree;
+import com.sun.source.tree.Tree;
+import com.sun.source.util.TreePath;
+import java.io.IOException;
+import java.time.LocalDate;
+import java.util.Arrays;
+import java.util.LinkedList;
+import java.util.List;
+import javax.lang.model.SourceVersion;
+import javax.lang.model.element.Name;
+import org.junit.FixMethodOrder;
+import org.junit.runners.MethodSorters;
+import org.netbeans.api.java.source.CompilationController;
+import org.netbeans.api.java.source.JavaSource;
+import org.netbeans.api.java.source.Task;
+import org.netbeans.api.java.source.TreePathHandle;
+import org.netbeans.modules.refactoring.api.Problem;
+import org.netbeans.modules.refactoring.api.RefactoringSession;
+import org.netbeans.modules.refactoring.java.api.InnerToOuterRefactoring;
+import static
org.netbeans.modules.refactoring.java.test.RefactoringTestBase.addAllProblems;
+import org.openide.util.Exceptions;
+
+/**
+ * Test inner to outer refactoring for test.
+ *
+ * In the input files, and the expected outcomes, the indentation does not
+ * really matter as far as the tests are concerned because the indentation is
+ * stripped away before the remaining source lines are compared to the expected
+ * lines.
+ *
+ * @author homberghp {@code <[email protected])>}
+ */
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
+public class InnerOuterRecordTest extends RefactoringTestBase {
+
+ public InnerOuterRecordTest(String name) {
+ super(name);
+ //ensure we are running on at least 16.
+ try {
+ SourceVersion.valueOf("RELEASE_16"); //NOI18N
+ } catch (IllegalArgumentException ex) {
+ //OK, no RELEASE_16, skip test
+ throw new RuntimeException("need at least Java 16 for record");
+ }
+ }
+
+ // for reference
+ public void test0_259004() throws Exception {
+ String source
+ = """
+ package t;
+
+ import java.util.function.Consumer;
+
+ public class A {
+
+ public static void main(String[] args) {
+ Consumer<F> c = f -> {};
+ }
+
+ public static final class F {}
+ }""";
Review Comment:
recommend to align the `"""` tokens since it influences
indentation/whitespace prefixing, example:
```java
String source =
"""
package t;
import java.util.function.Consumer;
public class A {
public static void main(String[] args) {
Consumer<F> c = f -> {};
}
public static final class F {}
}
""";
```
i think it does also look nicer when its less noisy.
##########
java/java.source.base/src/org/netbeans/api/java/source/TreePathHandle.java:
##########
@@ -322,6 +322,7 @@ private static boolean isSupported(Element el) {
case ENUM_CONSTANT:
case RECORD:
//TODO: record component
+ case RECORD_COMPONENT:
Review Comment:
note to self: check if this todo can be removed
##########
java/refactoring.java/src/org/netbeans/modules/refactoring/java/plugins/InnerToOuterTransformer.java:
##########
@@ -582,44 +586,80 @@ private ClassTree refactorInnerClass(ClassTree
innerClass) {
}
rewrite(modifiersTree, newModifiersTree);
+ // create constructor which refers with member to old outer.
if (referenceName != null) {
for (Tree member:newInnerClass.getMembers()) {
if (member.getKind() == Tree.Kind.METHOD) {
- MethodTree m = (MethodTree) member;
- if (m.getName().contentEquals("<init>") ||
m.getReturnType() == null) {
- VariableTree parameter =
make.Variable(make.Modifiers(EnumSet.of(Modifier.FINAL)),
refactoring.getReferenceName(), make.Type(outerType), null);
- MethodTree newConstructor = hasVarArgs(m) ?
- make.insertMethodParameter(m,
m.getParameters().size() - 1, parameter) :
- make.addMethodParameter(m, parameter);
-
+ MethodTree ctor = (MethodTree) member;
+ if (ctor.getName().contentEquals("<init>") ||
ctor.getReturnType() == null) {
+ VariableTree parameter =
make.Variable(make.Modifiers(EnumSet.of(Modifier.FINAL)), referenceName,
make.Type(outerType), null);
+ MethodTree newConstructor = hasVarArgs(ctor) ?
+ make.insertMethodParameter(ctor,
ctor.getParameters().size() - 1, parameter) :
+ make.addMethodParameter(ctor, parameter);
+
AssignmentTree assign =
make.Assignment(make.Identifier("this."+referenceName),
make.Identifier(referenceName)); // NOI18N
BlockTree block =
make.insertBlockStatement(newConstructor.getBody(), 1,
make.ExpressionStatement(assign));
Set<Modifier> modifiers =
EnumSet.noneOf(Modifier.class);
modifiers.addAll(newConstructor.getModifiers().getFlags());
modifiers.remove(Modifier.PRIVATE);
newConstructor = make.Constructor(
make.Modifiers(modifiers,newConstructor.getModifiers().getAnnotations()),
- newConstructor.getTypeParameters(),
+ newConstructor.getTypeParameters(),
newConstructor.getParameters(),
newConstructor.getThrows(),
block);
- newInnerClass = make.removeClassMember(newInnerClass,
m);
- genUtils.copyComments(m, newConstructor, true);
- genUtils.copyComments(m, newConstructor, false);
+ newInnerClass = make.removeClassMember(newInnerClass,
ctor);
+ genUtils.copyComments(ctor, newConstructor, true);
+ genUtils.copyComments(ctor, newConstructor, false);
newInnerClass =
genUtils.insertClassMember(newInnerClass, newConstructor);
}
}
}
}
-
+
if(innerClass != newInnerClass) {
genUtils.copyComments(innerClass, newInnerClass, true);
genUtils.copyComments(innerClass, newInnerClass, false);
}
return newInnerClass;
}
+ private ClassTree refactorInnerRecord(ClassTree innerRecord) {
+ refactoring.setInnerIsRecord(true);
+ ClassTree newInnerRecord = innerRecord;
+ List<? extends Tree> members = newInnerRecord.getMembers();
+ int insertAt=0;
+ for (Tree member : members) {
+ Tree.Kind kind = member.getKind();
+// System.out.println("member = " + kind + "" + member);
+
+ switch (kind) {
+ // The fields, except the static, should loose private final
modifiers
+ // so that they appear without these modifiers in the record
header
+ case VARIABLE:
+ VariableTree vt = (VariableTree) member;
+ ModifiersTree mt = vt.getModifiers();
+ if (mt.getFlags().contains(Modifier.STATIC)) {
+ continue;
+ }
+ var mtype = vt.getType();
+ var mName = vt.getName();
+ var mods = make.Modifiers(EnumSet.noneOf(Modifier.class));
+ var newMember = make.RecordComponent(mods, mName, mtype);
+ newInnerRecord = make.removeClassMember(newInnerRecord,
member);
+ newInnerRecord = make.insertClassMember(newInnerRecord,
insertAt, newMember);
Review Comment:
i am not a fan of `var` since it hides the type only so that the IDE can
render it next to it again as inline hint (-> nothing is gained and it makes
review more difficult). But I think i can figure out what the types are here so
I let it slide ;)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists