Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[all] Replace the attributesToString() function by the Xbase ToString…
…Builder.

close #748

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Sep 15, 2017
1 parent e1581d5 commit 37543e8
Show file tree
Hide file tree
Showing 16 changed files with 126 additions and 104 deletions.
Expand Up @@ -25,6 +25,7 @@
import java.util.UUID;

import org.eclipse.xtext.xbase.lib.Pure;
import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;

/**
* This class describes all the addresses used by the kernel to identify its
Expand Down Expand Up @@ -62,9 +63,11 @@ public Address(SpaceID spaceId, UUID agentId) {
@Override
@Pure
public String toString() {
return "Address [agentId=" + this.agentId //$NON-NLS-1$
+ ", spaceId=" + this.spaceId //$NON-NLS-1$
+ "]"; //$NON-NLS-1$
final ToStringBuilder builder = new ToStringBuilder(this);
builder.add("type", getClass().getSimpleName()); //$NON-NLS-1$
builder.add("agentId", this.agentId); //$NON-NLS-1$
builder.add("spaceId", this.spaceId); //$NON-NLS-1$
return builder.toString();
}

/**
Expand Down
Expand Up @@ -33,6 +33,7 @@
import org.eclipse.xtext.xbase.lib.Inline;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.eclipse.xtext.xbase.lib.Pure;
import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;

import io.sarl.lang.SARLVersion;
import io.sarl.lang.annotation.SarlSpecification;
Expand Down Expand Up @@ -124,22 +125,12 @@ public Agent(
this.skillProvider = skillProvider;
}

@Override
protected String attributesToString() {
final StringBuilder builder = new StringBuilder();
builder.append("id = "); //$NON-NLS-1$
builder.append(this.id);
builder.append(", parentID="); //$NON-NLS-1$
builder.append(this.parentID);
return builder.toString();
}

@Override
@Pure
public String toString() {
return getClass().getSimpleName()
+ " [" + attributesToString() //$NON-NLS-1$
+ "]"; //$NON-NLS-1$
protected void toString(ToStringBuilder builder) {
builder.add("type", getClass().getSimpleName()); //$NON-NLS-1$
builder.add("id", this.id); //$NON-NLS-1$
builder.add("parentID", this.parentID); //$NON-NLS-1$
}

/**
Expand Down
Expand Up @@ -24,6 +24,7 @@
import java.util.UUID;

import org.eclipse.xtext.xbase.lib.Pure;
import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;

import io.sarl.lang.util.ClearableReference;

Expand All @@ -37,13 +38,21 @@
*/
public abstract class AgentProtectedAPIObject extends SRESpecificDataContainer {

/**
* Returns a String representation of the Event E1 attributes only.
@Override
@Pure
public final String toString() {
final ToStringBuilder builder = new ToStringBuilder(this);
toString(builder);
return builder.toString();
}

/** fill the given builder with the string representation of this object.
*
* @return the string representation of the attributes of this Event.
* @param builder the string builder.
* @since 0.7
*/
@Pure
protected abstract String attributesToString();
protected abstract void toString(ToStringBuilder builder);

/** Replies the skill corresponding to the given capacity.
*
Expand Down
Expand Up @@ -26,6 +26,7 @@

import org.eclipse.xtext.xbase.lib.Inline;
import org.eclipse.xtext.xbase.lib.Pure;
import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;

import io.sarl.lang.util.ClearableReference;

Expand Down Expand Up @@ -57,19 +58,9 @@ public abstract class AgentTrait extends AgentProtectedAPIObject {

@Override
@Pure
protected String attributesToString() {
final StringBuilder result = new StringBuilder();
result.append("owner = "); //$NON-NLS-1$
result.append(getOwner());
return result.toString();
}

@Override
@Pure
public String toString() {
return getClass().getSimpleName()
+ " [" + attributesToString() //$NON-NLS-1$
+ "]"; //$NON-NLS-1$
protected void toString(ToStringBuilder builder) {
builder.add("type", getClass().getSimpleName()); //$NON-NLS-1$
builder.add("owner", getOwner()); //$NON-NLS-1$
}

/** Set the agent that has this trait.
Expand Down
29 changes: 14 additions & 15 deletions main/coreplugins/io.sarl.lang.core/src/io/sarl/lang/core/Event.java
Expand Up @@ -25,6 +25,7 @@
import java.util.UUID;

import org.eclipse.xtext.xbase.lib.Pure;
import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;

/** Elementary interaction unit inside an {@link EventSpace} An event is the
* specification of some occurrence in a Space that may potentially trigger
Expand Down Expand Up @@ -101,25 +102,23 @@ public void setSource(Address source) {
this.source = source;
}

/**
* Returns a String representation of the Event E1 attributes only.
*
* @return the string representation of the attributes of this Event.
*/
@Override
@Pure
protected String attributesToString() {
final StringBuilder result = new StringBuilder();
result.append("source = "); //$NON-NLS-1$
result.append(this.source);
return result.toString();
public final String toString() {
final ToStringBuilder builder = new ToStringBuilder(this);
toString(builder);
return builder.toString();
}

@Override
/** fill the given builder with the string representation of this object.
*
* @param builder the string builder.
* @since 0.7
*/
@Pure
public String toString() {
return getClass().getSimpleName()
+ " [" + attributesToString() //$NON-NLS-1$
+ "]"; //$NON-NLS-1$
protected void toString(ToStringBuilder builder) {
builder.add("type", getClass().getSimpleName()); //$NON-NLS-1$
builder.add("source", this.source); //$NON-NLS-1$
}

/** Replies if the event was emitted by an entity with the given address.
Expand Down
Expand Up @@ -25,6 +25,7 @@
import java.util.UUID;

import org.eclipse.xtext.xbase.lib.Pure;
import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;

/**
* Unique Identifier for a {@link Space}.
Expand Down Expand Up @@ -145,10 +146,12 @@ private boolean equalsID(SpaceID other) {
@Override
@Pure
public String toString() {
return "SpaceID [id=" + this.id //$NON-NLS-1$
+ ", contextID=" + this.contextID //$NON-NLS-1$
+ ", spaceSpec=" + this.spaceSpec //$NON-NLS-1$
+ "]"; //$NON-NLS-1$
final ToStringBuilder builder = new ToStringBuilder(this);
builder.add("type", getClass().getSimpleName()); //$NON-NLS-1$
builder.add("id", this.id); //$NON-NLS-1$
builder.add("contextID", this.contextID); //$NON-NLS-1$
builder.add("spaceSpec", this.spaceSpec); //$NON-NLS-1$
return builder.toString();
}

@Override
Expand Down
Expand Up @@ -123,6 +123,7 @@
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure2;
import org.eclipse.xtext.xbase.lib.Pure;
import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;
import org.eclipse.xtext.xbase.typesystem.InferredTypeIndicator;
import org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference;
import org.eclipse.xtext.xbase.typesystem.util.CommonTypeComputationServices;
Expand Down Expand Up @@ -2700,23 +2701,27 @@ protected void appendToStringFunctions(GenerationContext context, XtendTypeDecla
}

if (!declaredInstanceFields.isEmpty()) {
final JvmTypeReference voidType = this._typeReferenceBuilder.typeRef(Void.TYPE);
final JvmOperation op = SARLJvmModelInferrer.this.typeBuilder.toMethod(
source,
"attributesToString", //$NON-NLS-1$
SARLJvmModelInferrer.this._typeReferenceBuilder.typeRef(String.class), (it2) -> {
"toString", //$NON-NLS-1$
voidType, (it2) -> {
it2.setVisibility(JvmVisibility.PROTECTED);
SARLJvmModelInferrer.this.typeBuilder.setDocumentation(it2,
MessageFormat.format(Messages.SARLJvmModelInferrer_2,
target.getSimpleName()));
final JvmFormalParameter param = this.typesFactory.createJvmFormalParameter();
param.setName("builder"); //$NON-NLS-1$
param.setParameterType(SARLJvmModelInferrer.this._typeReferenceBuilder.typeRef(ToStringBuilder.class));
it2.getParameters().add(param);
setBody(it2, (it3) -> {
it3.append("StringBuilder result = new StringBuilder(" //$NON-NLS-1$
+ "super.attributesToString());").newLine(); //$NON-NLS-1$
it3.append("super.toString(builder);"); //$NON-NLS-1$
for (final JvmField attr : declaredInstanceFields) {
it3.append("result.append(\"" + attr.getSimpleName() //$NON-NLS-1$
+ " = \").append(this." //$NON-NLS-1$
+ attr.getSimpleName() + ");").newLine(); //$NON-NLS-1$
it3.newLine();
it3.append("builder.add(\"" + attr.getSimpleName() //$NON-NLS-1$
+ "\", this." //$NON-NLS-1$
+ attr.getSimpleName() + ");"); //$NON-NLS-1$
}
it3.append("return result.toString();"); //$NON-NLS-1$
});
});
if (op != null) {
Expand Down
Expand Up @@ -23,6 +23,9 @@

import java.util.UUID;

import org.eclipse.xtext.xbase.lib.Pure;
import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;

import io.sarl.core.DefaultContextInteractions;
import io.sarl.core.Lifecycle;
import io.sarl.lang.core.Address;
Expand Down Expand Up @@ -88,10 +91,12 @@ public int getInstallationOrder() {
}

@Override
protected String attributesToString() {
return super.attributesToString() + ", parentContext = " + this.parentContext //$NON-NLS-1$
+ ", defaultSpace = " + this.defaultSpace //$NON-NLS-1$
+ ", addressInDefaultspace = " + this.addressInParentDefaultSpace; //$NON-NLS-1$
@Pure
public void toString(ToStringBuilder builder) {
super.toString(builder);
builder.add("parentContext", this.parentContext); //$NON-NLS-1$
builder.add("defaultSpace", this.defaultSpace); //$NON-NLS-1$
builder.add("addressInDefaultspace", this.addressInParentDefaultSpace); //$NON-NLS-1$
}

@Override
Expand Down
Expand Up @@ -28,6 +28,8 @@

import com.google.common.collect.Sets;
import com.google.inject.Inject;
import org.eclipse.xtext.xbase.lib.Pure;
import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;

import io.janusproject.services.contextspace.ContextSpaceService;

Expand Down Expand Up @@ -111,8 +113,10 @@ public int getInstallationOrder() {
}

@Override
protected String attributesToString() {
return super.attributesToString() + ", contexts = " + this.contextRepository.toString(); //$NON-NLS-1$
@Pure
public void toString(ToStringBuilder builder) {
super.toString(builder);
builder.add("contexts", this.contextRepository.toString()); //$NON-NLS-1$
}

@Override
Expand Down
Expand Up @@ -26,6 +26,8 @@
import java.util.UUID;

import com.google.inject.Inject;
import org.eclipse.xtext.xbase.lib.Pure;
import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;

import io.janusproject.services.contextspace.ContextSpaceService;

Expand Down Expand Up @@ -118,8 +120,10 @@ synchronized void resetInnerContext() {
}

@Override
protected String attributesToString() {
return super.attributesToString() + ", addressInDefaultspace = " + this.agentAddressInInnerDefaultSpace; //$NON-NLS-1$
@Pure
public void toString(ToStringBuilder builder) {
super.toString(builder);
builder.add("addressInDefaultspace", this.agentAddressInInnerDefaultSpace); //$NON-NLS-1$
}

@Override
Expand Down
Expand Up @@ -33,6 +33,8 @@
import com.google.common.collect.Queues;
import com.google.inject.Inject;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.Pure;
import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;

import io.janusproject.kernel.bic.internaleventdispatching.AgentInternalEventsDispatcher;
import io.janusproject.services.logging.LogService;
Expand Down Expand Up @@ -124,9 +126,11 @@ public int getInstallationOrder() {
}

@Override
protected String attributesToString() {
return super.attributesToString() + ", state = " + getOwnerState() //$NON-NLS-1$
+ ", addressInDefaultspace = " + this.agentAddressInInnerDefaultSpace; //$NON-NLS-1$
@Pure
public void toString(ToStringBuilder builder) {
super.toString(builder);
builder.add("state", getOwnerState()); //$NON-NLS-1$
builder.add("addressInDefaultspace", this.agentAddressInInnerDefaultSpace); //$NON-NLS-1$
}

@Override
Expand Down
Expand Up @@ -39,6 +39,8 @@
import com.google.inject.Inject;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.eclipse.xtext.xbase.lib.Pure;
import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;

import io.janusproject.services.executor.EarlyExitException;
import io.janusproject.services.executor.ExecutorService;
Expand Down Expand Up @@ -112,8 +114,10 @@ public int getInstallationOrder() {
}

@Override
protected String attributesToString() {
return super.attributesToString() + ", tasks = " + this.tasks; //$NON-NLS-1$
@Pure
public void toString(ToStringBuilder builder) {
super.toString(builder);
builder.add("tasks", this.tasks); //$NON-NLS-1$
}

/**
Expand Down
Expand Up @@ -70,6 +70,7 @@ public void myAgentSpawnedCompile() throws Exception {
"import java.util.Objects;",
"import java.util.UUID;",
"import org.eclipse.xtext.xbase.lib.Pure;",
"import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;",
"",
"@SarlSpecification(\"" + SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING + "\")",
"@SarlElementType(" + SarlPackage.SARL_EVENT + ")",
Expand Down Expand Up @@ -119,10 +120,9 @@ public void myAgentSpawnedCompile() throws Exception {
" */",
" @SyntheticMember",
" @Pure",
" protected String attributesToString() {",
" StringBuilder result = new StringBuilder(super.attributesToString());",
" result.append(\"titi = \").append(this.titi);",
" return result.toString();",
" protected void toString(final ToStringBuilder builder) {",
" super.toString(builder);",
" builder.add(\"titi\", this.titi);",
" }",
" ",
" @SyntheticMember",
Expand Down
Expand Up @@ -252,6 +252,7 @@ public void generalGuardBehaviorUnit() throws Exception {
"import io.sarl.lang.core.Address;",
"import io.sarl.lang.core.Event;",
"import org.eclipse.xtext.xbase.lib.Pure;",
"import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;",
"",
"@SarlSpecification(\"" + SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING + "\")",
"@SarlElementType(" + SarlPackage.SARL_EVENT + ")",
Expand Down Expand Up @@ -300,10 +301,9 @@ public void generalGuardBehaviorUnit() throws Exception {
" */",
" @SyntheticMember",
" @Pure",
" protected String attributesToString() {",
" StringBuilder result = new StringBuilder(super.attributesToString());",
" result.append(\"i = \").append(this.i);",
" return result.toString();",
" protected void toString(final ToStringBuilder builder) {",
" super.toString(builder);",
" builder.add(\"i\", this.i);",
" }",
" ",
" @SyntheticMember",
Expand Down

0 comments on commit 37543e8

Please sign in to comment.