net.wpm.codegen.ExpressionCallStaticSelf 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.Type;
import org.objectweb.asm.commons.GeneratorAdapter;
import org.objectweb.asm.commons.Method;
import net.wpm.codegen.Context;
import net.wpm.codegen.Expression;
import net.wpm.codegen.ExpressionCallStaticSelf;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import static java.lang.String.format;
import static net.wpm.codegen.Expressions.self;
import static net.wpm.codegen.Utils.argsToString;
import static net.wpm.codegen.Utils.exceptionInGeneratedClass;
import static net.wpm.codegen.Utils.getJavaType;
public class ExpressionCallStaticSelf implements Expression {
private final Expression owner;
private final String methodName;
private final List arguments = new ArrayList<>();
public ExpressionCallStaticSelf(String methodName, Expression... expressions) {
this.owner = self();
this.methodName = methodName;
this.arguments.addAll(Arrays.asList(expressions));
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ExpressionCallStaticSelf that = (ExpressionCallStaticSelf) o;
if (owner != null ? !owner.equals(that.owner) : that.owner != null) return false;
if (methodName != null ? !methodName.equals(that.methodName) : that.methodName != null) return false;
return !(arguments != null ? !arguments.equals(that.arguments) : that.arguments != null);
}
@Override
public int hashCode() {
int result = owner != null ? owner.hashCode() : 0;
result = 31 * result + (methodName != null ? methodName.hashCode() : 0);
result = 31 * result + (arguments != null ? arguments.hashCode() : 0);
return result;
}
@Override
public Type type(Context ctx) {
List argumentTypes = new ArrayList<>();
for (Expression argument : arguments) {
argumentTypes.add(argument.type(ctx));
}
Set methods = ctx.getStaticMethods();
for (Method m : methods) {
if (m.getName().equals(methodName)) {
if (m.getArgumentTypes().length == argumentTypes.size()) {
Type[] methodTypes = m.getArgumentTypes();
boolean isSame = true;
for (int i = 0; i < argumentTypes.size(); i++) {
if (!methodTypes[i].equals(argumentTypes.get(i))) {
isSame = false;
break;
}
}
if (isSame) {
return m.getReturnType();
}
}
}
}
throw new RuntimeException(format("No method %s.%s(%s). %s",
owner.type(ctx).getClassName(),
methodName,
(!argumentTypes.isEmpty() ? argsToString(argumentClasses(ctx, arguments)) : ""),
exceptionInGeneratedClass(ctx)));
}
private static List> argumentClasses(Context ctx, List expressions) {
List> classList = new ArrayList<>();
for (Expression expression : expressions) {
classList.add(getJavaType(ctx.getClassLoader(), expression.type(ctx)));
}
return classList;
}
@Override
public Type load(Context ctx) {
GeneratorAdapter g = ctx.getGeneratorAdapter();
Type ownerType = owner.type(ctx);
List argumentTypes = new ArrayList<>();
for (Expression argument : arguments) {
argument.load(ctx);
argumentTypes.add(argument.type(ctx));
}
Type returnType = type(ctx);
g.invokeStatic(ownerType, new Method(methodName, returnType, argumentTypes.toArray(new Type[]{})));
return returnType;
}
}