Skip to content

Commit

Permalink
Code generator record updates.
Browse files Browse the repository at this point in the history
 - javac generates final methods for records unless they are already
   in the code
 - code can't override them, but it can replace them using the abstract
   method impl generator
 - made equals, hashcode and toString generators record aware
 - disabled getter/setter and property generator for records
  • Loading branch information
mbien committed Apr 26, 2024
1 parent c7b27a5 commit 2e166d1
Show file tree
Hide file tree
Showing 24 changed files with 78 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.sun.source.util.Trees;
import java.io.IOException;
import java.util.Collections;
import java.util.EnumSet;
import java.util.LinkedList;
import java.util.List;
import javax.lang.model.element.Element;
Expand Down Expand Up @@ -92,10 +93,12 @@ public AddPropertyCodeGenerator(JTextComponent component, String className, List
this.vcsName = vcsName;
}

@Override
public String getDisplayName() {
return NbBundle.getMessage(AddPropertyCodeGenerator.class, "DN_AddProperty");
}

@Override
public void invoke() {
Object o = component.getDocument().getProperty(Document.StreamDescriptionProperty);

Expand Down Expand Up @@ -216,6 +219,7 @@ public void run(CompilationController parameter) throws Exception {
r.lock();
try {
NbDocument.runAtomicAsUser((StyledDocument) doc, new Runnable() {
@Override
public void run() {
try {
GuardedSectionManager manager = GuardedSectionManager.getInstance((StyledDocument) doc);
Expand Down Expand Up @@ -256,6 +260,7 @@ public void run() {
// code insertion to document passed
try {
JavaSource.forFileObject(file).runModificationTask(new Task<WorkingCopy>() {
@Override
public void run(WorkingCopy workingCopy) throws Exception {
workingCopy.toPhase(Phase.RESOLVED);

Expand Down Expand Up @@ -352,11 +357,17 @@ public Void visitClass(ClassTree node, Void p) {

public static final class Factory implements CodeGenerator.Factory {

private static final EnumSet<Tree.Kind> TREE_KINDS = EnumSet.copyOf(TreeUtilities.CLASS_TREE_KINDS);
static {
TREE_KINDS.remove(Tree.Kind.RECORD); // no fields in records
}

@Override
public List<? extends CodeGenerator> create(Lookup context) {
JTextComponent component = context.lookup(JTextComponent.class);
CompilationController cc = context.lookup(CompilationController.class);
TreePath path = context.lookup(TreePath.class);
while (path != null && !TreeUtilities.CLASS_TREE_KINDS.contains(path.getLeaf().getKind())) {
while (path != null && !TREE_KINDS.contains(path.getLeaf().getKind())) {
path = path.getParentPath();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public String getDisplayName() {
}

static EqualsHashCodeGenerator createEqualsHashCodeGenerator(JTextComponent component, CompilationController cc, Element el) throws IOException {
if (el.getKind() != ElementKind.CLASS) {
if (el.getKind() != ElementKind.CLASS && el.getKind() != ElementKind.RECORD) {
return null;
}
//#125114: ignore anonymous innerclasses:
Expand Down Expand Up @@ -410,9 +410,7 @@ public static void generateEqualsAndHashCode(WorkingCopy wc, TreePath path, Iter
if (!dt.getTypeArguments().isEmpty()) {
WildcardType wt = wc.getTypes().getWildcardType(null, null);
TypeMirror[] typeArgs = new TypeMirror[dt.getTypeArguments().size()];
for (int i = 0; i < typeArgs.length; i++) {
typeArgs[i] = wt;
}
Arrays.fill(typeArgs, wt);
dt = dt.getEnclosingType().getKind() == TypeKind.DECLARED
? wc.getTypes().getDeclaredType((DeclaredType)dt.getEnclosingType(), te, typeArgs)
: wc.getTypes().getDeclaredType(te, typeArgs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@
import javax.lang.model.type.TypeMirror;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;

import org.netbeans.api.annotations.common.NonNull;
import org.netbeans.api.java.source.CodeStyle;
import org.netbeans.api.java.source.CompilationInfo;
import org.netbeans.api.java.source.ElementUtilities;
import org.netbeans.api.java.source.GeneratorUtilities;
Expand All @@ -58,7 +56,6 @@
import org.netbeans.editor.Utilities;
import org.openide.DialogDescriptor;
import org.openide.ErrorManager;
import org.openide.filesystems.FileObject;
import org.openide.util.NbBundle;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.netbeans.modules.java.editor.codegen;

import com.sun.source.tree.Tree;
import java.awt.Dialog;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
Expand Down Expand Up @@ -62,6 +63,11 @@
*/
public class GetterSetterGenerator implements CodeGenerator {

private static final EnumSet<Tree.Kind> TREE_KINDS = EnumSet.copyOf(TreeUtilities.CLASS_TREE_KINDS);
static {
TREE_KINDS.remove(Tree.Kind.RECORD); // no getters/setters for records
}

public static class Factory implements CodeGenerator.Factory {

private static final String ERROR = "<error>"; //NOI18N
Expand All @@ -76,7 +82,7 @@ public List<? extends CodeGenerator> create(Lookup context) {
}
CodeStyle codeStyle = CodeStyle.getDefault(component.getDocument());
TreePath path = context.lookup(TreePath.class);
path = controller.getTreeUtilities().getPathElementOfKind(TreeUtilities.CLASS_TREE_KINDS, path);
path = controller.getTreeUtilities().getPathElementOfKind(TREE_KINDS, path);
if (path == null) {
return ret;
}
Expand Down Expand Up @@ -214,7 +220,7 @@ public void run(WorkingCopy copy) throws IOException {
copy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
Element e = description.getElementHandle().resolve(copy);
TreePath path = e != null ? copy.getTrees().getPath(e) : copy.getTreeUtilities().pathFor(caretOffset);
path = copy.getTreeUtilities().getPathElementOfKind(TreeUtilities.CLASS_TREE_KINDS, path);
path = copy.getTreeUtilities().getPathElementOfKind(TREE_KINDS, path);
if (path == null) {
String message = NbBundle.getMessage(GetterSetterGenerator.class, "ERR_CannotFindOriginalClass"); //NOI18N
org.netbeans.editor.Utilities.setStatusBoldText(component, message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.BiFunction;
import java.util.function.Function;

import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
Expand Down Expand Up @@ -193,6 +192,7 @@ public List<ElementHandle<? extends Element>> get() throws InterruptedExceptio
}
}

@Override
public void run() {
List<ElementHandle<? extends Element>> tmp;
if (testOverrideMethodsSelection != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,13 @@ static ToStringGenerator createToStringGenerator(JTextComponent component, Compi
for (Element element : typeElement.getEnclosedElements()) {
switch (element.getKind()) {
case METHOD:
if (element.getSimpleName().contentEquals("toString") && ((ExecutableElement) element).getParameters().isEmpty()) { //NOI18N
if (element.getSimpleName().contentEquals("toString") && ((ExecutableElement) element).getParameters().isEmpty() //NOI18N
&& !controller.getElementUtilities().isSynthetic(element)) { // e.g record
return null;
}
break;
case FIELD:
// case RECORD_COMPONENT: // record components will show up as fields for some reason
if (!ERROR.contentEquals(element.getSimpleName()) && !element.getModifiers().contains(Modifier.STATIC)) {
descriptions.add(ElementNode.Description.create(controller, element, null, true, true));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public static class Description {
private List<Description> subs;
private String htmlHeader;
private boolean isSelected;
private boolean isSelectable;
private final boolean isSelectable;

public static Description create(List<Description> subs) {
return new Description("<root>", null, null, subs, null, false, false); // NOI18N
Expand All @@ -168,6 +168,7 @@ public static Description create(CompilationInfo info, Element element, List<Des
case ANNOTATION_TYPE:
case CLASS:
case ENUM:
case RECORD:
case INTERFACE:
htmlHeader = createHtmlHeader(deprecated, (TypeElement)element);
break;
Expand Down Expand Up @@ -462,6 +463,7 @@ private static String print( TypeMirror tm ) {

private static class DescriptionComparator implements Comparator<Description> {

@Override
public int compare(Description d1, Description d2) {

if ( k2i(d1.elementHandle.getKind()) != k2i(d2.elementHandle.getKind()) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

package org.netbeans.modules.java.editor.overridden;

import java.awt.Point;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
Expand Down Expand Up @@ -58,7 +56,6 @@
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileStateInvalidException;
import org.openide.filesystems.URLMapper;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;
Expand Down Expand Up @@ -553,22 +550,7 @@ private static Map<URL, List<URL>> getDependencies(boolean binary) {
Method dependenciesMethod = clazz.getDeclaredMethod(method);

return (Map<URL, List<URL>>) dependenciesMethod.invoke(instance);
} catch (IllegalAccessException ex) {
Logger.getLogger(GoToImplementation.class.getName()).log(Level.FINE, null, ex);
return null;
} catch (IllegalArgumentException ex) {
Logger.getLogger(GoToImplementation.class.getName()).log(Level.FINE, null, ex);
return null;
} catch (InvocationTargetException ex) {
Logger.getLogger(GoToImplementation.class.getName()).log(Level.FINE, null, ex);
return null;
} catch (NoSuchMethodException ex) {
Logger.getLogger(GoToImplementation.class.getName()).log(Level.FINE, null, ex);
return null;
} catch (SecurityException ex) {
Logger.getLogger(GoToImplementation.class.getName()).log(Level.FINE, null, ex);
return null;
} catch (ClassCastException ex) {
} catch (ReflectiveOperationException | IllegalArgumentException | SecurityException | ClassCastException ex) {
Logger.getLogger(GoToImplementation.class.getName()).log(Level.FINE, null, ex);
return null;
}
Expand Down Expand Up @@ -604,22 +586,7 @@ private static Map<URL, List<URL>> getRootPeers() {
Method peersMethod = clazz.getDeclaredMethod(method);

return (Map<URL, List<URL>>) peersMethod.invoke(instance);
} catch (IllegalAccessException ex) {
Logger.getLogger(GoToImplementation.class.getName()).log(Level.FINE, null, ex);
return null;
} catch (IllegalArgumentException ex) {
Logger.getLogger(GoToImplementation.class.getName()).log(Level.FINE, null, ex);
return null;
} catch (InvocationTargetException ex) {
Logger.getLogger(GoToImplementation.class.getName()).log(Level.FINE, null, ex);
return null;
} catch (NoSuchMethodException ex) {
Logger.getLogger(GoToImplementation.class.getName()).log(Level.FINE, null, ex);
return null;
} catch (SecurityException ex) {
Logger.getLogger(GoToImplementation.class.getName()).log(Level.FINE, null, ex);
return null;
} catch (ClassCastException ex) {
} catch (ReflectiveOperationException | IllegalArgumentException | SecurityException | ClassCastException ex) {
Logger.getLogger(GoToImplementation.class.getName()).log(Level.FINE, null, ex);
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class Test<T extends Number> extends AbstractList<T> {

@Override
public T get(int arg0) {
throw new UnsupportedOperationException("Not supported yet.");
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class Test extends AbstractList<String> {

@Override
public String get(int arg0) {
throw new UnsupportedOperationException("Not supported yet.");
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class Test<T extends Number> extends AbstractList<T> {

@Override
public T get(int arg0) {
throw new UnsupportedOperationException("Not supported yet.");
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class Test extends AbstractList<String> {

@Override
public String get(int arg0) {
throw new UnsupportedOperationException("Not supported yet.");
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class ImplementAbstractMethods11 {
a(new Comparator() {

public int compare(Object t, Object t1) {
throw new UnsupportedOperationException("Not supported yet.");
return 0;
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class ImplementAbstractMethods4 extends AbstractClass2 {
}
@Override
public String[] test(int[] x, String[] y, String... args) throws IOException, BadLocationException {
throw new UnsupportedOperationException("Not supported yet.");
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class ImplementAbstractMethods5 extends AbstractClass3 {
}
@Override
public String[] test(Map<String, List<String>> l) throws IOException, BadLocationException {
throw new UnsupportedOperationException("Not supported yet.");
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ public class ImplementAbstractMethods7 extends ImplementAbstractMethods7Usee2

@Override
protected boolean resolve() {
throw new UnsupportedOperationException("Not supported yet.");
return false;
}

public String doStuff() {
throw new UnsupportedOperationException("Not supported yet.");
return null;
}

public int resolveStuff(JButton button) {
throw new UnsupportedOperationException("Not supported yet.");
return 0;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ public class ImplementDefaultMethods1 implements java.util.Iterator<String> {

@Override
public boolean hasNext() {
throw new UnsupportedOperationException("Not supported yet.");
return false;
}

@Override
public String next() {
throw new UnsupportedOperationException("Not supported yet.");
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ public class ImplementDefaultMethods2 implements java.util.Iterator {

@Override
public boolean hasNext() {
throw new UnsupportedOperationException("Not supported yet.");
return false;
}

@Override
public Object next() {
throw new UnsupportedOperationException("Not supported yet.");
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ public class ImplementEnumMethods {
ONE {
@Override
public String kuk() {
throw new UnsupportedOperationException("Not supported yet.");
return null;
}
}, TWO {
@Override
public String kuk() {
throw new UnsupportedOperationException("Not supported yet.");
return null;
}
}, THREE {
@Override
public String kuk() {
throw new UnsupportedOperationException("Not supported yet.");
return null;
}
};

Expand Down
Loading

0 comments on commit 2e166d1

Please sign in to comment.