Skip to content
Permalink
Browse files
[JENKINS-38865] Move the AST classes from Groovy to Java to reduce co…
…mplexity of consumption (part 2)

- Fix build
  • Loading branch information
stephenc committed Oct 12, 2016
1 parent 7c3eea5 commit 88bf74287976e87a3a9dc019d320d16f48372323
Showing 5 changed files with 115 additions and 89 deletions.
@@ -5,7 +5,7 @@
import groovy.transform.ToString;

/**
* A single parameter definition, eventually corresponding to a {@link ParameterDefinition}
* A single parameter definition, eventually corresponding to a {@code ParameterDefinition}
*
* @author Andrew Bayer
*/
@@ -7,7 +7,7 @@
import org.jenkinsci.plugins.pipeline.modeldefinition.validator.ModelValidator;

/**
* Represents a block of "foo = 'bar'" assignments to environment variables, corresponding to {@link Environment}.
* Represents a block of "foo = 'bar'" assignments to environment variables, corresponding to {@code Environment}.
*
* @author Andrew Bayer
*/
@@ -1,90 +1,95 @@
package org.jenkinsci.plugins.pipeline.modeldefinition.ast;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import groovy.lang.Closure;
import groovy.transform.EqualsAndHashCode;
import groovy.transform.ToString;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.codehaus.groovy.runtime.DefaultGroovyMethods;
import org.jenkinsci.plugins.pipeline.modeldefinition.validator.ModelValidator;

/**
* A container for one or more {@link ModelASTJobProperty}s
*
* @author Andrew Bayer
*/
@ToString(includeSuper = true, includeSuperProperties = true)
@EqualsAndHashCode(callSuper = true)
@SuppressFBWarnings(value = "SE_NO_SERIALVERSIONID")
public final class ModelASTJobProperties extends ModelASTElement {
private List<ModelASTJobProperty> properties = new ArrayList<ModelASTJobProperty>();

public ModelASTJobProperties(Object sourceLocation) {
super(sourceLocation);
}

@Override
public JSONObject toJSON() {
final JSONArray a = new JSONArray();
DefaultGroovyMethods.each(properties, new Closure<Boolean>(this, this) {
public Boolean doCall(Object s) {
return a.add(((ModelASTJobProperty) s).toJSON());
}

});

for (ModelASTJobProperty property : properties) {
a.add(property.toJSON());
}
return new JSONObject().accumulate("properties", a);
}

@Override
public void validate(final ModelValidator validator) {
validator.validateElement(this);
DefaultGroovyMethods.each(properties, new Closure<Object>(this, this) {
public void doCall(Object s) {
((ModelASTJobProperty) s).validate(validator);
}

});
for (ModelASTJobProperty property : properties) {
property.validate(validator);
}
}

@Override
public String toGroovy() {
return "jobProperties {\n" + DefaultGroovyMethods
.join(DefaultGroovyMethods.collect(properties, new Closure<String>(this, this) {
public String doCall(ModelASTJobProperty it) {
return it.toGroovy();
}

public String doCall() {
return doCall(null);
}

}), "\n") + "\n}\n";
StringBuilder result = new StringBuilder("jobProperties {\n");
for (ModelASTJobProperty property : properties) {
result.append(property.toGroovy()).append("\n");
}
result.append("}\n");
return result.toString();
}

@Override
public void removeSourceLocation() {
super.removeSourceLocation();
DefaultGroovyMethods.each(properties, new Closure<Object>(this, this) {
public void doCall(ModelASTJobProperty it) {
it.removeSourceLocation();
}

public void doCall() {
doCall(null);
}

});
for (ModelASTJobProperty property : properties) {
property.removeSourceLocation();
}
}

public ArrayList<ModelASTJobProperty> getProperties() {
public List<ModelASTJobProperty> getProperties() {
return properties;
}

public void setProperties(List<ModelASTJobProperty> properties) {
this.properties = properties;
}

private List<ModelASTJobProperty> properties = new ArrayList<ModelASTJobProperty>();
@Override
public String toString() {
return "ModelASTJobProperties{" +
"properties=" + properties +
"}";
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
if (!super.equals(o)) {
return false;
}

ModelASTJobProperties that = (ModelASTJobProperties) o;

return getProperties() != null ? getProperties().equals(that.getProperties()) : that.getProperties() == null;

}

@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (getProperties() != null ? getProperties().hashCode() : 0);
return result;
}
}
@@ -1,7 +1,7 @@
package org.jenkinsci.plugins.pipeline.modeldefinition.ast;

/**
* A single job property, corresponding eventually to {@link JobProperty}
* A single job property, corresponding eventually to {@code JobProperty}
*
* @author Andrew Bayer
*/
@@ -2,7 +2,6 @@

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import groovy.lang.Closure;
import groovy.lang.GString;
import groovy.transform.EqualsAndHashCode;
import groovy.transform.ToString;
import java.util.LinkedHashMap;
@@ -23,23 +22,21 @@
@EqualsAndHashCode(callSuper = true)
@SuppressFBWarnings(value = "SE_NO_SERIALVERSIONID")
public final class ModelASTNamedArgumentList extends ModelASTArgumentList {
private Map<ModelASTKey, ModelASTValue> arguments = new LinkedHashMap<ModelASTKey, ModelASTValue>();

public ModelASTNamedArgumentList(Object sourceLocation) {
super(sourceLocation);
}

@Override
public JSONArray toJSON() {
final JSONArray a = new JSONArray();

DefaultGroovyMethods.each(arguments, new Closure<Boolean>(this, this) {
public Boolean doCall(Object k, Object v) {
JSONObject o = new JSONObject();
o.accumulate("key", ((ModelASTKey) k).toJSON());
o.accumulate("value", ((ModelASTValue) v).toJSON());
return a.add(o);
}

});
for (Map.Entry<ModelASTKey, ModelASTValue> entry: arguments.entrySet()) {
JSONObject o = new JSONObject();
o.accumulate("key", entry.getKey().toJSON());
o.accumulate("value", entry.getValue().toJSON());
a.add(o);
}
return a;
}

@@ -49,58 +46,82 @@ public Boolean doCall(Object k, Object v) {
* @param keyName The name of a key to check for.
* @return True if a {@link ModelASTKey} with that name is present in the map.
*/
public boolean containsKeyName(@Nonnull final String keyName) {
return DefaultGroovyMethods.any(arguments, new Closure<Boolean>(this, this) {
public Boolean doCall(Object k, Object v) {
return keyName.equals(((ModelASTKey) k).getKey());
}

});
public boolean containsKeyName(@Nonnull String keyName) {
for (ModelASTKey key: arguments.keySet()) {
if (keyName.equals(key.getKey())) return true;
}
return false;
}

@Override
public void validate(final ModelValidator validator) {
// Nothing to validate directly
DefaultGroovyMethods.each(arguments, new Closure<Object>(this, this) {
public void doCall(Object k, Object v) {
((ModelASTKey) k).validate(validator);
((ModelASTValue) v).validate(validator);
}

});

for (Map.Entry<ModelASTKey, ModelASTValue> entry : arguments.entrySet()) {
entry.getKey().validate(validator);
entry.getValue().validate(validator);
}
}

@Override
public String toGroovy() {
return DefaultGroovyMethods.join(DefaultGroovyMethods.collect(arguments, new Closure<GString>(this, this) {
public GString doCall(final Object k, final Object v) {
return ((ModelASTKey) k).toGroovy() + ": " + ((ModelASTValue) v).toGroovy();
StringBuilder result = new StringBuilder();
boolean first = true;
for (Map.Entry<ModelASTKey, ModelASTValue> entry : arguments.entrySet()) {
if (first) {
first = false;
} else {
result.append(", ");
}

}), ", ");
result.append(entry.getKey().toGroovy()).append(": ").append(entry.getValue().toGroovy());
}
return result.toString();
}

@Override
public void removeSourceLocation() {
super.removeSourceLocation();

DefaultGroovyMethods.each(arguments, new Closure<Object>(this, this) {
public void doCall(Object k, Object v) {
((ModelASTKey) k).removeSourceLocation();
((ModelASTValue) v).removeSourceLocation();
}

});
for (Map.Entry<ModelASTKey, ModelASTValue> entry : arguments.entrySet()) {
entry.getKey().removeSourceLocation();
entry.getValue().removeSourceLocation();
}
}

public LinkedHashMap<ModelASTKey, ModelASTValue> getArguments() {
public Map<ModelASTKey, ModelASTValue> getArguments() {
return arguments;
}

public void setArguments(Map<ModelASTKey, ModelASTValue> arguments) {
this.arguments = arguments;
}

private Map<ModelASTKey, ModelASTValue> arguments = new LinkedHashMap<ModelASTKey, ModelASTValue>();
@Override
public String toString() {
return "ModelASTNamedArgumentList{" +
"arguments=" + arguments +
"}";
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
if (!super.equals(o)) {
return false;
}

ModelASTNamedArgumentList that = (ModelASTNamedArgumentList) o;

return getArguments() != null ? getArguments().equals(that.getArguments()) : that.getArguments() == null;

}

@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (getArguments() != null ? getArguments().hashCode() : 0);
return result;
}
}

0 comments on commit 88bf742

Please sign in to comment.