
org.glowroot.agent.weaving.ImmutableCatchHandler Maven / Gradle / Ivy
package org.glowroot.agent.weaving;
import org.glowroot.agent.shaded.fasterxml.jackson.annotation.JsonCreator;
import org.glowroot.agent.shaded.fasterxml.jackson.annotation.JsonProperty;
import org.glowroot.agent.shaded.google.common.base.MoreObjects;
import org.glowroot.agent.shaded.google.common.base.Preconditions;
import org.glowroot.agent.shaded.google.common.collect.ImmutableList;
import org.glowroot.agent.shaded.google.common.collect.Lists;
import java.util.List;
import javax.annotation.Generated;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
import org.glowroot.agent.shaded.objectweb.asm.Label;
/**
* Immutable implementation of {@link WeavingMethodVisitor.CatchHandler}.
*
* Use builder to create immutable instances:
* {@code ImmutableCatchHandler.builder()}.
* Use static factory method to create immutable instances:
* {@code ImmutableCatchHandler.of()}.
*/
@SuppressWarnings("all")
@ParametersAreNonnullByDefault
@Generated({"Immutables.generator", "WeavingMethodVisitor.CatchHandler"})
@Immutable
final class ImmutableCatchHandler
implements WeavingMethodVisitor.CatchHandler {
private final Label catchStartLabel;
private final ImmutableList advisors;
private ImmutableCatchHandler(
Label catchStartLabel,
Iterable extends Advice> advisors) {
this.catchStartLabel = Preconditions.checkNotNull(catchStartLabel);
this.advisors = ImmutableList.copyOf(advisors);
}
private ImmutableCatchHandler(
ImmutableCatchHandler original,
Label catchStartLabel,
ImmutableList advisors) {
this.catchStartLabel = catchStartLabel;
this.advisors = advisors;
}
/**
* @return value of {@code catchStartLabel} attribute
*/
@JsonProperty
@Override
public Label catchStartLabel() {
return catchStartLabel;
}
/**
* @return value of {@code advisors} attribute
*/
@JsonProperty
@Override
public ImmutableList advisors() {
return advisors;
}
/**
* Copy current immutable object by setting value for {@link WeavingMethodVisitor.CatchHandler#catchStartLabel() catchStartLabel}.
* Shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param value new value for catchStartLabel
* @return modified copy of the {@code this} object
*/
public final ImmutableCatchHandler withCatchStartLabel(Label value) {
if (this.catchStartLabel == value) return this;
Label newValue = Preconditions.checkNotNull(value);
return new ImmutableCatchHandler(this, newValue, this.advisors);
}
/**
* Copy current immutable object with elements that replace content of {@link WeavingMethodVisitor.CatchHandler#advisors() advisors}.
* @param elements elements to set
* @return modified copy of {@code this} object
*/
public final ImmutableCatchHandler withAdvisors(Advice... elements) {
ImmutableList newValue = ImmutableList.copyOf(elements);
return new ImmutableCatchHandler(this, this.catchStartLabel, newValue);
}
/**
* Copy current immutable object with elements that replace content of {@link WeavingMethodVisitor.CatchHandler#advisors() advisors}.
* Shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param elements iterable of advisors elements to set
* @return modified copy of {@code this} object
*/
public final ImmutableCatchHandler withAdvisors(Iterable extends Advice> elements) {
if (this.advisors == elements) return this;
ImmutableList newValue = ImmutableList.copyOf(elements);
return new ImmutableCatchHandler(this, this.catchStartLabel, newValue);
}
/**
* This instance is equal to instances of {@code ImmutableCatchHandler} with equal attribute values.
* @return {@code true} if {@code this} is equal to {@code another} instance
*/
@Override
public boolean equals(@Nullable Object another) {
if (this == another) return true;
return another instanceof ImmutableCatchHandler
&& equalTo((ImmutableCatchHandler) another);
}
private boolean equalTo(ImmutableCatchHandler another) {
return catchStartLabel.equals(another.catchStartLabel)
&& advisors.equals(another.advisors);
}
/**
* Computes hash code from attributes: {@code catchStartLabel}, {@code advisors}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 31;
h = h * 17 + catchStartLabel.hashCode();
h = h * 17 + advisors.hashCode();
return h;
}
/**
* Prints immutable value {@code CatchHandler...} with attribute values,
* excluding any non-generated and auxiliary attributes.
* @return string representation of value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("CatchHandler")
.add("catchStartLabel", catchStartLabel)
.add("advisors", advisors)
.toString();
}
/**
* Simple representation of this value type suitable Jackson binding
* @deprecated Do not use this type directly, it exists only for Jackson-binding infrastructure
*/
@Deprecated
static final class Json {
@JsonProperty
@Nullable Label catchStartLabel;
@JsonProperty
@Nullable List advisors;
}
/**
* @param json JSON-bindable data structure
* @return immutable value type
* @deprecated Do not use this method directly, it exists only for Jackson-binding infrastructure
*/
@Deprecated
@JsonCreator
static ImmutableCatchHandler fromJson(Json json) {
ImmutableCatchHandler.Builder builder = ImmutableCatchHandler.builder();
if (json.catchStartLabel != null) {
builder.catchStartLabel(json.catchStartLabel);
}
if (json.advisors != null) {
builder.addAllAdvisors(json.advisors);
}
return builder.build();
}
/**
* Construct new immutable {@code CatchHandler} instance.
* @param catchStartLabel value for {@code catchStartLabel}
* @param advisors value for {@code advisors}
* @return immutable CatchHandler instance
*/
public static ImmutableCatchHandler of(Label catchStartLabel, List advisors) {
return of(catchStartLabel, (Iterable extends Advice>) advisors);
}
/**
* Construct new immutable {@code CatchHandler} instance.
* @param catchStartLabel value for {@code catchStartLabel}
* @param advisors value for {@code advisors}
* @return immutable CatchHandler instance
*/
public static ImmutableCatchHandler of(Label catchStartLabel, Iterable extends Advice> advisors) {
return new ImmutableCatchHandler(catchStartLabel, advisors);
}
/**
* Creates immutable copy of {@link WeavingMethodVisitor.CatchHandler}.
* Uses accessors to get values to initialize immutable instance.
* If an instance is already immutable, it is returned as is.
* @param instance instance to copy
* @return copied immutable CatchHandler instance
*/
static ImmutableCatchHandler copyOf(WeavingMethodVisitor.CatchHandler instance) {
if (instance instanceof ImmutableCatchHandler) {
return (ImmutableCatchHandler) instance;
}
return ImmutableCatchHandler.builder()
.copyFrom(instance)
.build();
}
/**
* Creates builder for {@link org.glowroot.agent.weaving.ImmutableCatchHandler ImmutableCatchHandler}.
* @return new ImmutableCatchHandler builder
*/
static ImmutableCatchHandler.Builder builder() {
return new ImmutableCatchHandler.Builder();
}
/**
* Builds instances of {@link org.glowroot.agent.weaving.ImmutableCatchHandler ImmutableCatchHandler}.
* Initialize attributes and then invoke {@link #build()} method to create
* immutable instance.
* {@code Builder} is not thread safe and generally should not be stored in field or collection,
* but used immediately to create instances.
*/
@NotThreadSafe
static final class Builder {
private static final long INIT_BIT_CATCH_START_LABEL = 0x1L;
private long initBits = 0x1;
private @Nullable Label catchStartLabel;
private ImmutableList.Builder advisorsBuilder = ImmutableList.builder();
private Builder() {}
/**
* Fill builder with attribute values from provided {@link WeavingMethodVisitor.CatchHandler} instance.
* Regular attribute values will be replaced with ones of an instance.
* Instance's absent optional values will not replace present values.
* Collection elements and entries will be added, not replaced.
* @param instance instance to copy values from
* @return {@code this} builder for chained invocation
*/
public final Builder copyFrom(WeavingMethodVisitor.CatchHandler instance) {
Preconditions.checkNotNull(instance);
catchStartLabel(instance.catchStartLabel());
addAllAdvisors(instance.advisors());
return this;
}
/**
* Initializes value for {@link WeavingMethodVisitor.CatchHandler#catchStartLabel() catchStartLabel}.
* @param catchStartLabel value for catchStartLabel
* @return {@code this} builder for chained invocation
*/
public final Builder catchStartLabel(Label catchStartLabel) {
this.catchStartLabel = Preconditions.checkNotNull(catchStartLabel);
initBits &= ~INIT_BIT_CATCH_START_LABEL;
return this;
}
/**
* Adds one element to {@link WeavingMethodVisitor.CatchHandler#advisors() advisors} list.
* @param element advisors element
* @return {@code this} builder for chained invocation
*/
public final Builder addAdvisors(Advice element) {
advisorsBuilder.add(element);
return this;
}
/**
* Adds elements to {@link WeavingMethodVisitor.CatchHandler#advisors() advisors} list.
* @param elements array of advisors elements
* @return {@code this} builder for chained invocation
*/
public final Builder addAdvisors(Advice... elements) {
advisorsBuilder.add(elements);
return this;
}
/**
* Sets or replaces all elements for {@link WeavingMethodVisitor.CatchHandler#advisors() advisors} list.
* @param elements iterable of advisors elements
* @return {@code this} builder for chained invocation
*/
public final Builder advisors(Iterable extends Advice> elements) {
advisorsBuilder = ImmutableList.builder();
return addAllAdvisors(elements);
}
/**
* Adds elements to {@link WeavingMethodVisitor.CatchHandler#advisors() advisors} list.
* @param elements iterable of advisors elements
* @return {@code this} builder for chained invocation
*/
public final Builder addAllAdvisors(Iterable extends Advice> elements) {
advisorsBuilder.addAll(elements);
return this;
}
/**
* Builds new {@link org.glowroot.agent.weaving.ImmutableCatchHandler ImmutableCatchHandler}.
* @return immutable instance of CatchHandler
* @throws exception {@code java.lang.IllegalStateException} if any required attributes are missing
*/
public ImmutableCatchHandler build()
throws IllegalStateException {
checkRequiredAttributes();
return new ImmutableCatchHandler(null, catchStartLabel, advisorsBuilder.build());
}
private boolean catchStartLabelIsSet() {
return (initBits & INIT_BIT_CATCH_START_LABEL) == 0;
}
private void checkRequiredAttributes() throws IllegalStateException {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
}
private String formatRequiredAttributesMessage() {
List attributes = Lists.newArrayList();
if (!catchStartLabelIsSet()) attributes.add("catchStartLabel");
return "Cannot build CatchHandler, some of required attributes are not set " + attributes;
}
}
}