net.wpm.codegen.ExpressionSwitchForKey Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of records Show documentation
Show all versions of records Show documentation
C-Struct like features for Java 6+ to improve performance.
The newest version!
/*
* Copyright (C) 2015 SoftIndex LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.wpm.codegen;
import org.objectweb.asm.Label;
import org.objectweb.asm.Type;
import org.objectweb.asm.commons.GeneratorAdapter;
import net.wpm.codegen.Context;
import net.wpm.codegen.Expression;
import net.wpm.codegen.ExpressionSwitchForKey;
import net.wpm.codegen.Utils;
import net.wpm.codegen.VarLocal;
import net.wpm.codegen.Variable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import static net.wpm.codegen.Expressions.*;
import static net.wpm.codegen.Utils.newLocal;
import static org.objectweb.asm.Type.getType;
public class ExpressionSwitchForKey implements Expression {
private final Expression key;
private final Expression defaultExp;
private final List listKey = new ArrayList<>();
private final List listValue = new ArrayList<>();
ExpressionSwitchForKey(Expression key, List listKey, List listValue) {
this.key = key;
this.defaultExp = null;
this.listKey.addAll(listKey);
this.listValue.addAll(listValue);
}
ExpressionSwitchForKey(Expression key, Expression defalultExp, List listKey, List listValue) {
this.key = key;
this.defaultExp = defalultExp;
this.listKey.addAll(listKey);
this.listValue.addAll(listValue);
}
@Override
public Type type(Context ctx) {
if (listValue.size() != 0) {
return listValue.get(0).type(ctx);
} else {
return getType(Object.class);
}
}
@Override
public Type load(Context ctx) {
VarLocal varKey = newLocal(ctx, key.type(ctx));
key.load(ctx);
varKey.store(ctx);
Label labelExit = new Label();
GeneratorAdapter g = ctx.getGeneratorAdapter();
final boolean keyPrimitiveType = Utils.isPrimitiveType(key.type(ctx));
for (int i = 0; i < listKey.size(); i++) {
Label labelNext = new Label();
if (keyPrimitiveType) {
listKey.get(i).load(ctx);
varKey.load(ctx);
g.ifCmp(key.type(ctx), GeneratorAdapter.NE, labelNext);
} else {
call(listKey.get(i), "equals", varKey).load(ctx);
g.push(true);
g.ifCmp(Type.BOOLEAN_TYPE, GeneratorAdapter.NE, labelNext);
}
listValue.get(i).load(ctx);
g.goTo(labelExit);
g.mark(labelNext);
}
if (defaultExp != null) {
defaultExp.load(ctx);
} else {
final Variable sb = let(constructor(StringBuilder.class));
call(sb, "append", value("Key '")).load(ctx);
call(sb, "append", keyPrimitiveType ? varKey : call(key, "toString")).load(ctx);
call(sb, "append", value("' not in keyList: [")).load(ctx);
final Iterator iterator = listKey.iterator();
while (iterator.hasNext()) {
final Expression expression = iterator.next();
final boolean primitiveType = Utils.isPrimitiveType(expression.type(ctx));
call(sb, "append", primitiveType ? expression : call(expression, "toString")).load(ctx);
if (iterator.hasNext()) {
call(sb, "append", value(", ")).load(ctx);
}
}
call(sb, "append", value("]")).load(ctx);
constructor(IllegalArgumentException.class, call(sb, "toString")).load(ctx);
g.throwException();
}
g.mark(labelExit);
return type(ctx);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ExpressionSwitchForKey that = (ExpressionSwitchForKey) o;
if (key != null ? !key.equals(that.key) : that.key != null) return false;
if (listKey != null ? !listKey.equals(that.listKey) : that.listKey != null) return false;
return !(listValue != null ? !listValue.equals(that.listValue) : that.listValue != null);
}
@Override
public int hashCode() {
int result = key != null ? key.hashCode() : 0;
result = 31 * result + (listKey != null ? listKey.hashCode() : 0);
result = 31 * result + (listValue != null ? listValue.hashCode() : 0);
return result;
}
}