-
Notifications
You must be signed in to change notification settings - Fork 893
solves #7044, refactor inner to outer for record #8197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6ce0d8b
solves #7044, refacotor inner to outer for record
homberghp 445977f
dropped commented out lines.
homberghp 5ef6c4a
implemented suggestions by mbien based on git conversation.
homberghp d730cfc
applied changes requested by mbien on 2025-01-30
homberghp fa1f343
Merge branch 'apache:master' into issue7044
homberghp fb9f1a3
java/refactoring.java/test/unit/src/org/netbeans/modules/refactoring/…
homberghp 91aa481
add tests for inner record.
homberghp 1185f08
test base with flag sideBySideCompare
homberghp 2a9d0b2
added test for varargs and implements
homberghp f3054f8
Merge branch 'issue7044' of github.com:homberghp/netbeans into issue7044
homberghp b2740ac
provide partial solution for #7044
homberghp cf2c6c1
Merge remote-tracking branch 'origin/master' into issue7044
homberghp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
java/java.source.base/src/org/netbeans/api/java/source/RecordUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
/* | ||
* 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.api.java.source; | ||
|
||
import com.sun.source.tree.ClassTree; | ||
import com.sun.source.tree.MethodTree; | ||
import com.sun.source.tree.Tree; | ||
import com.sun.source.tree.Tree.Kind; | ||
import com.sun.source.tree.VariableTree; | ||
import com.sun.tools.javac.tree.JCTree; | ||
//import java.util.List; | ||
//import com.sun.tools.javac.util.List; | ||
import java.util.List; | ||
|
||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import javax.lang.model.element.Modifier; | ||
import org.netbeans.api.annotations.common.NonNull; | ||
|
||
/** | ||
* Utilities specific for record types. | ||
* | ||
* @author homberghp | ||
*/ | ||
public class RecordUtils { | ||
|
||
public static boolean isConStructor(Tree m) { | ||
|
||
if (m.getKind() != Kind.METHOD) { | ||
return false; | ||
} | ||
MethodTree met = (MethodTree) m; | ||
if (met.getModifiers().getFlags().contains(Modifier.STATIC)) { | ||
return false; | ||
} | ||
return met.getReturnType() == null; | ||
} | ||
|
||
/** | ||
* Count the record components of a record. | ||
* | ||
* @param record to be counted for its components. | ||
* @return the count | ||
*/ | ||
public static int countComponents(ClassTree record) { | ||
return (int) record.getMembers() | ||
.stream() | ||
.filter(RecordUtils::isNormalField) | ||
.count(); | ||
} | ||
|
||
public static List<String> componentNames(ClassTree record) { | ||
List<String> result = components(record).stream() | ||
.map(m -> ((VariableTree) m).getName().toString()) | ||
.toList(); | ||
return result; | ||
} | ||
|
||
public static List<JCTree> components(ClassTree record) { | ||
|
||
List<JCTree> result = record.getMembers() | ||
.stream() | ||
.filter(m -> m.getKind() == Kind.VARIABLE) | ||
.map(VariableTree.class::cast) | ||
.filter(RecordUtils::isNormalField) | ||
.map(JCTree.class::cast) | ||
.toList(); | ||
return result; | ||
} | ||
|
||
public static Set<String> parameterNames(MethodTree method) { | ||
Set<String> result = method.getParameters() | ||
.stream() | ||
.map(m -> ((VariableTree) m).getName().toString()) | ||
.collect(Collectors.toSet()); | ||
return result; | ||
} | ||
|
||
public static boolean isNormalField(Tree t) { | ||
return t.getKind() == Kind.VARIABLE && !((VariableTree) t).getModifiers().getFlags().contains(Modifier.STATIC); | ||
} | ||
|
||
/** | ||
* A member is a normal field when not a class, (or enum ...) not a method | ||
* and not static. For record that should be a record component then. | ||
* | ||
* @param t | ||
* @return true if fields of the class/record are of same amount, order and | ||
* types | ||
*/ | ||
public static boolean isRecordComponent(JCTree t) { | ||
if (t.getKind() == Kind.CLASS) { | ||
return false; | ||
} | ||
if (t.getKind() == Kind.METHOD) { | ||
return false; | ||
} | ||
if (t.getKind() == Kind.VARIABLE && t instanceof VariableTree vt) { | ||
return !vt.getModifiers().getFlags().contains(Modifier.STATIC); | ||
} | ||
return false; | ||
} | ||
|
||
public static boolean hasAllParameterNames(MethodTree method, Set<String> names) { | ||
Set<String> actualNames = method.getParameters().stream().map(m -> m.getName().toString()).collect(Collectors.toSet()); | ||
return names.size() == actualNames.size() && names.containsAll(actualNames); | ||
} | ||
|
||
/** | ||
* Get the canonical parameters of a record. | ||
* | ||
* Implementation detail: The method scans the tree and looks for a | ||
* constructor with a matching signature. returns 0 if not a record or no | ||
* constructor found. | ||
* | ||
* @param record tree presenting the record | ||
* @return the list of fields as declared in the record header. | ||
*/ | ||
public static List<JCTree.JCVariableDecl> canonicalParameters(JCTree.JCClassDecl node) { | ||
Set<String> expectedNames = node.getMembers().stream() | ||
.filter(m -> RecordUtils.isNormalField(m)) | ||
.map(m -> ((JCTree.JCVariableDecl) m).getName().toString()) | ||
.collect(Collectors.toSet()); | ||
List<JCTree.JCVariableDecl> result = node.getMembers().stream() | ||
.filter(m -> RecordUtils.isConStructor(m)) | ||
.map(JCTree.JCMethodDecl.class::cast) | ||
.filter(t -> RecordUtils.hasAllParameterNames(t, expectedNames)) | ||
.flatMap(m -> ((JCTree.JCMethodDecl) m).getParameters().stream()).toList(); | ||
return result; | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.