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

Better support for creation of Java Records #239

Merged
merged 1 commit into from
May 21, 2022
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
11 changes: 9 additions & 2 deletions api/src/main/java/org/jboss/forge/roaster/model/JavaRecord.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package org.jboss.forge.roaster.model;

import org.jboss.forge.roaster.Roaster;
import org.jboss.forge.roaster.model.source.JavaSource;

import java.util.List;

/**
* Represents a Record class (available as a preview feature in JDK 14). See {@link Roaster} for various options in
* generating {@link JavaRecord} instances
*/
public interface JavaRecord<O extends JavaRecord<O>> extends JavaSource<O>
public interface JavaRecord<O extends JavaRecord<O>> extends
JavaType<O>,
MethodHolder<O>
{
/**
* @return the list of {@link JavaRecordComponent} for this record
*/
List<JavaRecordComponent> getRecordComponents();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.jboss.forge.roaster.model;

/**
* Represents a Record component in a {@link JavaRecord}.
*/
public interface JavaRecordComponent<O extends JavaType<O>> extends Parameter<O>
{


}
56 changes: 50 additions & 6 deletions api/src/main/java/org/jboss/forge/roaster/model/MethodHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@

package org.jboss.forge.roaster.model;

import org.jboss.forge.roaster.model.source.MethodSource;
import org.jboss.forge.roaster.model.source.ParameterSource;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

/**
* Represents a {@link JavaType} that may declare methods.
Expand All @@ -20,32 +26,70 @@ public interface MethodHolder<O extends JavaType<O>> extends MemberHolder<O>
/**
* Return true if this {@link O} has a method with the given name and zero parameters; otherwise return false.
*/
boolean hasMethod(final Method<O, ?> name);
default boolean hasMethod(final Method<O, ?> name) {
return getMethods().contains(name);
}

/**
* Return true if this {@link O} has a method with signature matching the given method's signature.
*/
boolean hasMethodSignature(final Method<?, ?> method);
default boolean hasMethodSignature(final Method<?, ?> method) {
for (var local : getMethods())
{
if (local.getName().equals(method.getName()))
{
var localParams = local.getParameters().iterator();
for (Parameter<? extends JavaType<?>> methodParam : method.getParameters())
{
if (localParams.hasNext()
&& Objects.equals(localParams.next().getType().getName(), methodParam.getType().getName()))
{
continue;
}
return false;
}
return !localParams.hasNext();
}
}
return false;
}

/**
* Return true if this {@link O} has a method with the given name and zero parameters; otherwise return false.
*/
boolean hasMethodSignature(final String name);
default boolean hasMethodSignature(final String name) {
return hasMethodSignature(name, new String[] {});
}

/**
* Return true if this {@link O} has a method with the given name and signature types; otherwise return false.
*/
boolean hasMethodSignature(final String name, String... paramTypes);
default boolean hasMethodSignature(final String name, String... paramTypes) {
return getMethod(name, paramTypes) != null;
}

/**
* Return true if this {@link O} has a method with the given name and signature types; otherwise return false.
*/
boolean hasMethodSignature(final String name, Class<?>... paramTypes);
default boolean hasMethodSignature(final String name, Class<?>... paramTypes) {
final String[] types = paramTypes == null ? new String[0] :
Arrays.stream(paramTypes).map(Class::getName).toArray(String[]::new);
return hasMethodSignature(name, types);
}

/**
* Return the {@link Method} with the given name and zero parameters; otherwise return null.
*/
Method<O, ?> getMethod(final String name);
default Method<O, ?> getMethod(final String name) {
for (var method : getMethods())
{
if (method.getName().equals(name) && (method.getParameters().size() == 0))
{
return method;
}
}
return null;
}

/**
* Return the {@link Method} with the given name and signature types; otherwise return null.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.jboss.forge.roaster.model.source;

import org.jboss.forge.roaster.model.JavaRecordComponent;

public interface JavaRecordComponentSource extends
JavaRecordComponent<JavaRecordSource>,
ParameterSource<JavaRecordSource>
{
@Override
default boolean isFinal()
{
throw new UnsupportedOperationException("A record component is always final");
}

@Override
default ParameterSource<JavaRecordSource> setFinal(boolean finl)
{
throw new UnsupportedOperationException("A record component cannot be marked as final");
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
package org.jboss.forge.roaster.model.source;

import org.jboss.forge.roaster.model.JavaRecord;
import org.jboss.forge.roaster.model.JavaRecordComponent;

public interface JavaRecordSource extends
JavaSource<JavaRecordSource>,
JavaRecord<JavaRecordSource>,
MethodHolderSource<JavaRecordSource>
{
JavaRecordComponentSource addRecordComponent(String type, String name);
JavaRecordComponentSource addRecordComponent(Class<?> type, String name);

JavaRecordSource removeRecordComponent(String name);
JavaRecordSource removeRecordComponent(JavaRecordComponent recordComponent);

public interface JavaRecordSource extends JavaRecord<JavaRecordSource> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@

package org.jboss.forge.roaster.model.source;

import java.util.Arrays;
import java.util.List;

import org.jboss.forge.roaster.model.Method;
import org.jboss.forge.roaster.model.MethodHolder;
import org.jboss.forge.roaster.model.util.Methods;
import org.jboss.forge.roaster.model.util.Types;

/**
* Represents a {@link JavaSource} that may declare methods.
Expand All @@ -26,19 +28,59 @@ public interface MethodHolderSource<O extends JavaSource<O>> extends MethodHolde
* Return the {@link MethodSource} with the given name and zero parameters; otherwise return null.
*/
@Override
MethodSource<O> getMethod(final String name);
default MethodSource<O> getMethod(final String name) {
for (var method : getMethods())
{
if (method.getName().equals(name) && (method.getParameters().size() == 0))
{
return method;
}
}
return null;
}

/**
* Return the {@link MethodSource} with the given name and signature types; otherwise return null.
*/
@Override
MethodSource<O> getMethod(final String name, String... paramTypes);
default MethodSource<O> getMethod(final String name, String... paramTypes) {
for (MethodSource<O> local : getMethods())
{
if (local.getName().equals(name))
{
List<ParameterSource<O>> localParams = local.getParameters();
if (paramTypes != null)
{
if (localParams.size() == paramTypes.length)
{
boolean matches = true;
for (int i = 0; i < localParams.size(); i++)
{
ParameterSource<O> localParam = localParams.get(i);
String type = paramTypes[i];
if (!Types.areEquivalent(localParam.getType().getName(), type))
{
matches = false;
}
}
if (matches)
return local;
}
}
}
}
return null;
}

/**
* Return the {@link MethodSource} with the given name and signature types; otherwise return null.
*/
@Override
MethodSource<O> getMethod(final String name, Class<?>... paramTypes);
default MethodSource<O> getMethod(final String name, Class<?>... paramTypes) {
final String[] types = paramTypes == null ? new String[0] :
Arrays.stream(paramTypes).map(Class::getName).toArray(String[]::new);
return getMethod(name, types);
}

/**
* Get a {@link List} of all {@link MethodSource}s declared by this {@link O} instance, if any; otherwise, return an
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.eclipse.jdt.core.dom.BodyDeclaration;
import org.eclipse.jdt.core.dom.EnumDeclaration;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.MethodRef;
import org.eclipse.jdt.core.dom.RecordDeclaration;
import org.eclipse.jdt.core.dom.TypeDeclaration;

/**
Expand Down Expand Up @@ -66,6 +68,14 @@ public boolean visit(AnonymousClassDeclaration node)
return false;
}

@Override
public boolean visit(RecordDeclaration node)
{
parent = node;
addMethods(node);
return false;
}

/**
* Returns the found method declarations.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public FieldSource<O> addField(final String declaration)
}

@SuppressWarnings("unchecked")
private void addField(Field<O> field)
protected void addField(Field<O> field)
{
List<Object> bodyDeclarations = getDeclaration().bodyDeclarations();
int idx = 0;
Expand Down Expand Up @@ -209,125 +209,6 @@ public O removeField(final Field<O> field)
return (O) this;
}

@Override
public boolean hasMethod(final Method<O, ?> method)
{
return getMethods().contains(method);
}

@Override
public boolean hasMethodSignature(final String name)
{
return hasMethodSignature(name, new String[] {});
}

@Override
public boolean hasMethodSignature(final String name, final String... paramTypes)
{
return getMethod(name, paramTypes) != null;
}

@Override
public boolean hasMethodSignature(final String name, Class<?>... paramTypes)
{
if (paramTypes == null)
{
paramTypes = new Class<?>[] {};
}

String[] types = new String[paramTypes.length];
for (int i = 0; i < paramTypes.length; i++)
{
types[i] = paramTypes[i].getName();
}

return hasMethodSignature(name, types);
}

@Override
public MethodSource<O> getMethod(final String name)
{
for (MethodSource<O> method : getMethods())
{
if (method.getName().equals(name) && (method.getParameters().size() == 0))
{
return method;
}
}
return null;
}

@Override
public MethodSource<O> getMethod(final String name, final String... paramTypes)
{
for (MethodSource<O> local : getMethods())
{
if (local.getName().equals(name))
{
List<ParameterSource<O>> localParams = local.getParameters();
if (paramTypes != null)
{
if (localParams.size() == paramTypes.length)
{
boolean matches = true;
for (int i = 0; i < localParams.size(); i++)
{
ParameterSource<O> localParam = localParams.get(i);
String type = paramTypes[i];
if (!Types.areEquivalent(localParam.getType().getName(), type))
{
matches = false;
}
}
if (matches)
return local;
}
}
}
}
return null;
}

@Override
public MethodSource<O> getMethod(final String name, Class<?>... paramTypes)
{
if (paramTypes == null)
{
paramTypes = new Class<?>[] {};
}

String[] types = new String[paramTypes.length];
for (int i = 0; i < paramTypes.length; i++)
{
types[i] = paramTypes[i].getName();
}

return getMethod(name, types);
}

@Override
public boolean hasMethodSignature(final Method<?, ?> method)
{
for (MethodSource<O> local : getMethods())
{
if (local.getName().equals(method.getName()))
{
Iterator<ParameterSource<O>> localParams = local.getParameters().iterator();
for (Parameter<? extends JavaType<?>> methodParam : method.getParameters())
{
if (localParams.hasNext()
&& Objects.equals(localParams.next().getType().getName(), methodParam.getType().getName()))
{
continue;
}
return false;
}
return !localParams.hasNext();
}
}
return false;
}

@Override
@SuppressWarnings("unchecked")
public O removeMethod(final Method<O, ?> method)
Expand Down
Loading