Skip to content
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

solves #7044, refactor inner to outer for record #8197

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private ElementHandle(final ElementKind kind, String... signatures) {
* Resolves an {@link Element} from the {@link ElementHandle}.
* @param compilationInfo representing the {@link javax.tools.JavaCompiler.CompilationTask}
* in which the {@link Element} should be resolved.
* @return resolved subclass of {@link Element} or null if the elment does not exist on
* @return resolved subclass of {@link Element} or null if the element does not exist on
* the classpath/sourcepath of {@link javax.tools.JavaCompiler.CompilationTask}.
*/
@SuppressWarnings ("unchecked") // NOI18N
Expand Down Expand Up @@ -143,19 +143,14 @@ private T resolveImpl (final ModuleElement module, final JavacTaskImpl jt) {
if (log.isLoggable(Level.FINE))
log.log(Level.FINE, "Resolving element kind: {0}", this.kind); // NOI18N
ElementKind simplifiedKind = this.kind;
if (simplifiedKind.name().equals("RECORD")) {
simplifiedKind = ElementKind.CLASS; //TODO: test
}
if (simplifiedKind.name().equals("RECORD_COMPONENT")) {
simplifiedKind = ElementKind.FIELD; //TODO: test
}
switch (simplifiedKind) {
case PACKAGE:
assert signatures.length == 1;
return (T) jt.getElements().getPackageElement(signatures[0]);
case CLASS:
case INTERFACE:
case ENUM:
case RECORD:
case ANNOTATION_TYPE: {
assert signatures.length == 1;
final Element type = getTypeElementByBinaryName (module, signatures[0], jt);
Expand Down Expand Up @@ -213,6 +208,7 @@ private T resolveImpl (final ModuleElement module, final JavacTaskImpl jt) {
}
case FIELD:
case ENUM_CONSTANT:
case RECORD_COMPONENT:
{
assert signatures.length == 3;
final Element type = getTypeElementByBinaryName (module, signatures[0], jt);
Expand Down
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ private static boolean isSupported(Element el) {
case ENUM_CONSTANT:
case RECORD:
//TODO: record component
case RECORD_COMPONENT:
return true;
case PARAMETER:
//only method and constructor parameters supported (not lambda):
Expand Down Expand Up @@ -869,4 +870,4 @@ public Void scan(Tree node, Void p) {
return result;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1221,6 +1221,7 @@ String template(ElementKind kind) {
case INTERFACE: return "Templates/Classes/Interface.java"; // NOI18N
case ANNOTATION_TYPE: return "Templates/Classes/AnnotationType.java"; // NOI18N
case ENUM: return "Templates/Classes/Enum.java"; // NOI18N
case RECORD: return "Templates/Classes/Record.java"; // NOI18N
case PACKAGE: return "Templates/Classes/package-info.java"; // NOI18N
default:
Logger.getLogger(WorkingCopy.class.getName()).log(Level.SEVERE, "Cannot resolve template for {0}", kind);
Expand Down Expand Up @@ -1248,6 +1249,9 @@ FileObject doCreateFromTemplate(CompilationUnitTree cut) throws IOException {
case ENUM:
kind = ElementKind.ENUM;
break;
case RECORD:
kind = ElementKind.RECORD;
break;
default:
Logger.getLogger(WorkingCopy.class.getName()).log(Level.SEVERE, "Cannot resolve template for {0}", cut.getTypeDecls().get(0).getKind());
kind = null;
Expand Down
Loading