Commit 5a066ba0 authored by Lubomir Bulej's avatar Lubomir Bulej

ArgProcessorKind: include supported secondary types, available through secondaryTypes().

ArgProcessorKind: renamed getASMType() to primaryType().
WeavingCode: accomodate the changes in ArgProcessorKind.
parent 1d052927
package ch.usi.dag.disl.processor;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Set;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.MethodNode;
import ch.usi.dag.disl.annotation.ProcessAlso;
import ch.usi.dag.disl.exception.DiSLFatalException;
public enum ArgProcessorKind {
BOOLEAN(Type.BOOLEAN_TYPE),
BYTE(Type.BYTE_TYPE),
CHAR(Type.CHAR_TYPE),
DOUBLE(Type.DOUBLE_TYPE),
FLOAT(Type.FLOAT_TYPE),
INT(Type.INT_TYPE),
LONG(Type.LONG_TYPE),
SHORT(Type.SHORT_TYPE),
OBJECT(Type.getType(Object.class));
private Type asmType;
private ArgProcessorKind(Type asmType) {
this.asmType = asmType;
}
public Type getASMType() {
return asmType;
}
public static ArgProcessorKind valueOf(Type type) {
if(type == null) {
throw new DiSLFatalException("Conversion from null not defined");
}
if(Type.BOOLEAN_TYPE.equals(type)) {
return BOOLEAN;
}
if(Type.BYTE_TYPE.equals(type)) {
return BYTE;
}
if(Type.CHAR_TYPE.equals(type)) {
return CHAR;
}
if(Type.DOUBLE_TYPE.equals(type)) {
return DOUBLE;
}
if(Type.FLOAT_TYPE.equals(type)) {
return FLOAT;
}
if(Type.INT_TYPE.equals(type)) {
return INT;
}
if(Type.LONG_TYPE.equals(type)) {
return LONG;
}
if(Type.SHORT_TYPE.equals(type)) {
return SHORT;
}
if(Type.OBJECT == type.getSort()) {
return OBJECT;
}
// process arrays as objects
if(Type.ARRAY == type.getSort()) {
return OBJECT;
}
throw new DiSLFatalException("Conversion from " + type.getClassName()
+ " not defined");
}
public static ArgProcessorKind valueOf(ProcessAlso.Type type) {
if(type == null) {
throw new DiSLFatalException("Conversion from null not defined");
}
switch(type) {
case BOOLEAN:
return BOOLEAN;
case BYTE:
return BYTE;
case SHORT:
return SHORT;
default:
throw new DiSLFatalException("Conversion from " + type.toString()
+ " not defined");
}
}
BOOLEAN (Type.BOOLEAN_TYPE),
BYTE (Type.BYTE_TYPE) {
@Override
public EnumSet <ArgProcessorKind> secondaryTypes () {
return EnumSet.of (ArgProcessorKind.BOOLEAN);
}
},
CHAR (Type.CHAR_TYPE),
DOUBLE (Type.DOUBLE_TYPE),
FLOAT (Type.FLOAT_TYPE),
INT (Type.INT_TYPE) {
@Override
public Set <ArgProcessorKind> secondaryTypes () {
return EnumSet.of (BOOLEAN, BYTE, SHORT);
}
},
LONG (Type.LONG_TYPE),
SHORT (Type.SHORT_TYPE) {
@Override
public Set <ArgProcessorKind> secondaryTypes () {
return EnumSet.of (BOOLEAN, BYTE);
}
},
OBJECT (Type.getType (Object.class));
private final Type __primaryType;
//
private ArgProcessorKind (final Type primaryType) {
__primaryType = primaryType;
}
public Type primaryType () {
return __primaryType;
}
public Set <ArgProcessorKind> secondaryTypes () {
return Collections.emptySet ();
}
public static ArgProcessorKind valueOf (final Type type) {
if (type == null) {
throw new DiSLFatalException ("conversion from <null> not defined");
}
//
// Try to find a primitive type match first.
//
for (final ArgProcessorKind kind : values ()) {
if (kind.__primaryType.equals (type)) {
return kind;
}
}
//
// Handle objects and arrays based on the sort.
//
final int sort = type.getSort ();
if (sort == Type.OBJECT || sort == Type.ARRAY) {
return OBJECT;
}
throw new DiSLFatalException (
"conversion from %s not defined", type.getClassName ()
);
}
public static ArgProcessorKind valueOf (final ProcessAlso.Type type) {
if (type == null) {
throw new DiSLFatalException ("conversion from <null> not defined");
}
switch (type) {
case BOOLEAN:
return BOOLEAN;
case BYTE:
return BYTE;
case SHORT:
return SHORT;
default:
throw new DiSLFatalException (
"conversion from %s not defined", type.toString ()
);
}
}
public static ArgProcessorKind forMethod (final MethodNode method) {
final Type [] argTypes = Type.getArgumentTypes (method.desc);
if (argTypes.length > 0) {
return valueOf (argTypes [0]);
} else {
return null;
}
}
}
......@@ -923,7 +923,7 @@ public class WeavingCode {
final int position = processorMethod.getArgPos ();
final int totalCount = processorMethod.getArgsCount ();
final Type type = processorMethod.getArgType ().getASMType ();
final Type type = processorMethod.getArgType ().primaryType ();
final InsnList insns = code.getInstructions();
rewriteArgumentContextCalls (position, totalCount, type, insns);
......@@ -953,7 +953,7 @@ public class WeavingCode {
final int index = pmi.getArgPos ();
final int total = pmi.getArgsCount ();
final Type type = pmi.getArgType ().getASMType ();
final Type type = pmi.getArgType ().primaryType ();
final InsnList insns = code.getInstructions ();
rewriteArgumentContextCalls (index, total, type, insns);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment