Java Code Examples for org.objectweb.asm.Type#getClassName()

The following examples show how to use org.objectweb.asm.Type#getClassName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClassDependenciesAnalyzer.java    From pushfish-android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private List<String> getClassDependencies(ClassRelevancyFilter filter, ClassReader reader) {
    List<String> out = new LinkedList<String>();
    char[] charBuffer = new char[reader.getMaxStringLength()];
    for (int i = 1; i < reader.getItemCount(); i++) {
        int itemOffset = reader.getItem(i);
        if (itemOffset > 0 && reader.readByte(itemOffset - 1) == 7) {
            // A CONSTANT_Class entry, read the class descriptor
            String classDescriptor = reader.readUTF8(itemOffset, charBuffer);
            Type type = Type.getObjectType(classDescriptor);
            while (type.getSort() == Type.ARRAY) {
                type = type.getElementType();
            }
            if (type.getSort() != Type.OBJECT) {
                // A primitive type
                continue;
            }
            String name = type.getClassName();
            if (filter.isRelevant(name)) {
                out.add(name);
            }
        }
    }
    return out;
}
 
Example 2
Source File: ClassDependenciesAnalyzer.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private List<String> getClassDependencies(ClassRelevancyFilter filter, ClassReader reader) {
    List<String> out = new LinkedList<String>();
    char[] charBuffer = new char[reader.getMaxStringLength()];
    for (int i = 1; i < reader.getItemCount(); i++) {
        int itemOffset = reader.getItem(i);
        if (itemOffset > 0 && reader.readByte(itemOffset - 1) == 7) {
            // A CONSTANT_Class entry, read the class descriptor
            String classDescriptor = reader.readUTF8(itemOffset, charBuffer);
            Type type = Type.getObjectType(classDescriptor);
            while (type.getSort() == Type.ARRAY) {
                type = type.getElementType();
            }
            if (type.getSort() != Type.OBJECT) {
                // A primitive type
                continue;
            }
            String name = type.getClassName();
            if (filter.isRelevant(name)) {
                out.add(name);
            }
        }
    }
    return out;
}
 
Example 3
Source File: ClassScanner.java    From forbidden-apis with Apache License 2.0 6 votes vote down vote up
String checkClassUse(Type type, String what, boolean deep, String origInternalName) {
  while (type.getSort() == Type.ARRAY) {
    type = type.getElementType(); // unwrap array
  }
  if (type.getSort() != Type.OBJECT) {
    return null; // we don't know this type, just pass!
  }
  final String printout = forbiddenSignatures.checkType(type);
  if (printout != null) {
    return String.format(Locale.ENGLISH, "Forbidden %s use: %s", what, printout);
  }
  if (deep && forbidNonPortableRuntime) {
    final String binaryClassName = type.getClassName();
    final ClassSignature c = lookup.lookupRelatedClass(type.getInternalName(), origInternalName);
    if (c != null && c.isRuntimeClass && !AsmUtils.isPortableRuntimeClass(binaryClassName)) {
      return String.format(Locale.ENGLISH,
        "Forbidden %s use: %s [non-portable or internal runtime class]",
        what, binaryClassName
      );
    }
  }
  return null;
}
 
Example 4
Source File: SignaturePrinter.java    From Mixin with MIT License 6 votes vote down vote up
/**
 * Get the source code name for the specified type
 * 
 * @param type Type to generate a friendly name for
 * @param box True to return the equivalent boxing type for primitives
 * @param fullyQualified fully-qualify class names 
 * @return String representation of the specified type, eg "int" for an
 *         integer primitive or "String" for java.lang.String
 */
public static String getTypeName(Type type, boolean box, boolean fullyQualified) {
    if (type == null) {
        return "{null?}";
    }
    switch (type.getSort()) {
        case Type.VOID:    return box ? "Void"      : "void";
        case Type.BOOLEAN: return box ? "Boolean"   : "boolean";
        case Type.CHAR:    return box ? "Character" : "char";
        case Type.BYTE:    return box ? "Byte"      : "byte";
        case Type.SHORT:   return box ? "Short"     : "short";
        case Type.INT:     return box ? "Integer"   : "int";
        case Type.FLOAT:   return box ? "Float"     : "float";
        case Type.LONG:    return box ? "Long"      : "long";
        case Type.DOUBLE:  return box ? "Double"    : "double";
        case Type.ARRAY:   return SignaturePrinter.getTypeName(type.getElementType(), box, fullyQualified) + SignaturePrinter.arraySuffix(type);
        case Type.OBJECT:
            String typeName = type.getClassName();
            if (!fullyQualified) {
                typeName = typeName.substring(typeName.lastIndexOf('.') + 1);
            }
            return typeName;
        default:
            return "Object";
    }
}
 
Example 5
Source File: ASMUtils.java    From radon with GNU General Public License v3.0 6 votes vote down vote up
public static int getVarOpcode(Type type, boolean store) {
    switch (type.getSort()) {
        case Type.BOOLEAN:
        case Type.CHAR:
        case Type.BYTE:
        case Type.SHORT:
        case Type.INT:
            return store ? Opcodes.ISTORE : Opcodes.ILOAD;
        case Type.FLOAT:
            return store ? Opcodes.FSTORE : Opcodes.FLOAD;
        case Type.LONG:
            return store ? Opcodes.LSTORE : Opcodes.LLOAD;
        case Type.DOUBLE:
            return store ? Opcodes.DSTORE : Opcodes.DLOAD;
        case Type.ARRAY:
        case Type.OBJECT:
            return store ? Opcodes.ASTORE : Opcodes.ALOAD;
        default:
            throw new AssertionError("Unknown type: " + type.getClassName());
    }
}
 
Example 6
Source File: ASMUtils.java    From radon with GNU General Public License v3.0 6 votes vote down vote up
public static int getReturnOpcode(Type type) {
    switch (type.getSort()) {
        case Type.BOOLEAN:
        case Type.CHAR:
        case Type.BYTE:
        case Type.SHORT:
        case Type.INT:
            return Opcodes.IRETURN;
        case Type.FLOAT:
            return Opcodes.FRETURN;
        case Type.LONG:
            return Opcodes.LRETURN;
        case Type.DOUBLE:
            return Opcodes.DRETURN;
        case Type.ARRAY:
        case Type.OBJECT:
            return Opcodes.ARETURN;
        case Type.VOID:
            return Opcodes.RETURN;
        default:
            throw new AssertionError("Unknown type sort: " + type.getClassName());
    }
}
 
Example 7
Source File: MethodInfo.java    From java-almanac with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
public String getParameterDeclaration(String sep, boolean simpleTypeNames) {
	final StringBuilder result = new StringBuilder();
	final Type[] arguments = Type.getArgumentTypes(getDesc());
	boolean comma = false;
	for (final Type arg : arguments) {
		if (comma) {
			result.append(sep);
		} else {
			comma = true;
		}
		String name = arg.getClassName();
		if (simpleTypeNames) {
			name = getSimpleTypeName(name);
		}
		name = name.replace('$', '.');
		result.append(name);
	}
	if ((getAccess() & Opcodes.ACC_VARARGS) != 0) {
		result.setLength(result.length() - 2);
		result.append("...");
	}
	return result.toString();
}
 
Example 8
Source File: ReflectiveFunctorFactory.java    From maple-ir with GNU General Public License v3.0 6 votes vote down vote up
@Override
public EvaluationFunctor<Number> negate(Type t) {
	String name = "NEG" + t.getClassName();
	
	if(cache.containsKey(name)) {
		return _get(name);
	}
	
	String desc = ("(" + t.getDescriptor() + ")" + t.getDescriptor());
	MethodNode m = makeBase(name, desc);
	
	InsnList insns = new InsnList();
	{
		insns.add(new VarInsnNode(TypeUtils.getVariableLoadOpcode(t), 0));
		insns.add(new InsnNode(TypeUtils.getNegateOpcode(t)));
		insns.add(new InsnNode(TypeUtils.getReturnOpcode(t)));
		m.node.instructions = insns;
	}
	
	return buildBridge(m);
}
 
Example 9
Source File: ReflectiveFunctorFactory.java    From maple-ir with GNU General Public License v3.0 6 votes vote down vote up
@Override
public EvaluationFunctor<Number> cast(Type from, Type to) {
	String name = "CASTFROM" + from.getClassName() + "TO" + to.getClassName();
	
	if(cache.containsKey(name)) {
		return _get(name);
	}

	String desc = ("(" + from.getDescriptor() + ")" + to.getDescriptor());
	MethodNode m = makeBase(name, desc);
	
	InsnList insns = new InsnList();
	{
		insns.add(new VarInsnNode(TypeUtils.getVariableLoadOpcode(from), 0));
		cast(insns, from, to);
		insns.add(new InsnNode(TypeUtils.getReturnOpcode(to)));
		m.node.instructions = insns;
	}
	
	return buildBridge(m);
}
 
Example 10
Source File: RefVerifier.java    From CodenameOne with GNU General Public License v2.0 6 votes vote down vote up
private void checkClassName(String className) {
	Type t = Type.getType(className);
	String name;

	switch (t.getSort()) {
	case Type.ARRAY:
		t = t.getElementType();
		if (t.getSort() != Type.OBJECT) {
			return;
		}

		// fall through to object processing
	case Type.OBJECT:
		name = t.getClassName();
		break;
	default:
		return;
	}
	
	checkSimpleClassName(name);
}
 
Example 11
Source File: DefaultClassDependenciesAnalyzer.java    From pushfish-android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private Set<String> getClassDependencies(ClassRelevancyFilter filter, ClassReader reader) {
    Set<String> out = new HashSet<String>();
    char[] charBuffer = new char[reader.getMaxStringLength()];
    for (int i = 1; i < reader.getItemCount(); i++) {
        int itemOffset = reader.getItem(i);
        if (itemOffset > 0 && reader.readByte(itemOffset - 1) == 7) {
            // A CONSTANT_Class entry, read the class descriptor
            String classDescriptor = reader.readUTF8(itemOffset, charBuffer);
            Type type = Type.getObjectType(classDescriptor);
            while (type.getSort() == Type.ARRAY) {
                type = type.getElementType();
            }
            if (type.getSort() != Type.OBJECT) {
                // A primitive type
                continue;
            }
            String name = type.getClassName();
            if (filter.isRelevant(name)) {
                out.add(name);
            }
        }
    }
    return out;
}
 
Example 12
Source File: TypeUtils.java    From cglib with Apache License 2.0 5 votes vote down vote up
public static String getClassName(Type type) {
    if (isPrimitive(type)) {
        return (String)rtransforms.get(type.getDescriptor());
    } else if (isArray(type)) {
        return getClassName(getComponentType(type)) + "[]";
    } else {
        return type.getClassName();
    }
}
 
Example 13
Source File: ConstantTracker.java    From zelixkiller with GNU General Public License v3.0 5 votes vote down vote up
@Override
public String toString() {
	Type t = type.getType();
	if (t == null)
		return "uninitialized";
	String typeName = type == BasicValue.REFERENCE_VALUE ? "a reference type" : t.getClassName();
	return this == NULL ? "null" : value == null ? "unknown value of " + typeName : value + " (" + typeName + ")";
}
 
Example 14
Source File: JavaUtil.java    From blazer with GNU General Public License v3.0 5 votes vote down vote up
public static String retrieveCanonicalNameFromClass(File classFile) throws FileNotFoundException, IOException {

        ClassNode classNode = new ClassNode();
        InputStream classFileInputStream = new FileInputStream(classFile);
        try {
            ClassReader classReader = new ClassReader(classFileInputStream);
            classReader.accept((ClassVisitor) classNode, 0);
        } finally {
            classFileInputStream.close();
        }

        Type classType = Type.getObjectType(classNode.name);
        return classType.getClassName();
    }
 
Example 15
Source File: ABICompilerMethodVisitor.java    From AVM with MIT License 5 votes vote down vote up
private void checkArgumentsAndReturnType() {
    for (Type type : Type.getArgumentTypes(this.methodDescriptor)) {
        if(!ABIUtils.isAllowedType(type)) {
            throw new ABICompilerException(
                type.getClassName() + " is not an allowed parameter type", methodName);
        }
    }
    Type returnType = Type.getReturnType(methodDescriptor);
    if(!ABIUtils.isAllowedType(returnType) && returnType != Type.VOID_TYPE) {
        throw new ABICompilerException(
            returnType.getClassName() + " is not an allowed return type", methodName);
    }
}
 
Example 16
Source File: CheckParserUsagesDT.java    From sql-layer with GNU Affero General Public License v3.0 5 votes vote down vote up
public static String typeToString(Type type) {
    String className = type.getClassName();
    if (type.getSort() == Type.OBJECT) {
        className = className.substring(className.lastIndexOf('.')+1);
    }
    return className;
}
 
Example 17
Source File: SimpleTypeTextifier.java    From es6draft with MIT License 5 votes vote down vote up
private String getDescriptor(Type type) {
    if (type.getSort() == Type.OBJECT) {
        String name = type.getInternalName();
        int index = name.lastIndexOf('/');
        return name.substring(index + 1);
    }
    if (type.getSort() == Type.ARRAY) {
        StringBuilder sb = new StringBuilder(getDescriptor(type.getElementType()));
        for (int dim = type.getDimensions(); dim > 0; --dim) {
            sb.append("[]");
        }
        return sb.toString();
    }
    return type.getClassName();
}
 
Example 18
Source File: SubClassCreator.java    From openpojo with Apache License 2.0 4 votes vote down vote up
private String getReturnTypeClass(String desc) {
  Type type = Type.getReturnType(desc);
  return type.getClassName();
}
 
Example 19
Source File: ReflectiveFunctorFactory.java    From maple-ir with GNU General Public License v3.0 4 votes vote down vote up
@Override
public EvaluationFunctor<Boolean> branch(Type lt, Type rt, ConditionalJumpStmt.ComparisonType type) {
	Type opType = TypeUtils.resolveBinOpType(lt, rt);
	String name = lt.getClassName() + type.name() + rt.getClassName() + "OPTYPE" + opType.getClassName() + "RETbool";

	String desc = "(" + lt.getDescriptor() + rt.getDescriptor() + ")Z";

	if(cache.containsKey(name)) {
		return _get(name);
	}

	MethodNode m = makeBase(name, desc);
	{
		InsnList insns = new InsnList();
		
		insns.add(new VarInsnNode(TypeUtils.getVariableLoadOpcode(lt), 0));
		cast(insns, lt, opType);
		insns.add(new VarInsnNode(TypeUtils.getVariableLoadOpcode(rt), lt.getSize()));
		cast(insns, rt, opType);
		

		LabelNode trueSuccessor = new LabelNode();
		
		if (opType == Type.INT_TYPE) {
			insns.add(new JumpInsnNode(Opcodes.IF_ICMPEQ + type.ordinal(), trueSuccessor));
		} else if (opType == Type.LONG_TYPE) {
			insns.add(new InsnNode(Opcodes.LCMP));
			insns.add(new JumpInsnNode(Opcodes.IFEQ + type.ordinal(), trueSuccessor));
		} else if (opType == Type.FLOAT_TYPE) {
			insns.add(new InsnNode((type == ConditionalJumpStmt.ComparisonType.LT || type == ConditionalJumpStmt.ComparisonType.LE) ? Opcodes.FCMPL : Opcodes.FCMPG));
			insns.add(new JumpInsnNode(Opcodes.IFEQ + type.ordinal(), trueSuccessor));
		} else if (opType == Type.DOUBLE_TYPE) {
			insns.add(new InsnNode((type == ConditionalJumpStmt.ComparisonType.LT || type == ConditionalJumpStmt.ComparisonType.LE) ? Opcodes.DCMPL : Opcodes.DCMPG));
			insns.add(new JumpInsnNode(Opcodes.IFEQ + type.ordinal(), trueSuccessor));
		} else {
			throw new IllegalArgumentException(opType.toString());
		}
		
		branchReturn(insns, trueSuccessor);
		
		m.node.instructions = insns;
	}
	
	return buildBridge(m);
}
 
Example 20
Source File: TypeNameUtil.java    From depan with Apache License 2.0 3 votes vote down vote up
/**
 * Convert class and field {@link Type} references to it's fully-qualified
 * name.  The class's implemented interfaces should get the fully-qualified
 * name via {@link #getFullyQualifiedInterfaceName(Type)}.
 * 
 * @param type {@code Type} used in a reference
 * @return fully qualified name for {@code Type} 
 */
public static String getFullyQualifiedTypeName(Type type) {
  if (type.getSort() == Type.ARRAY) {
    return Type.getType(type.toString().replaceAll("^\\[*", ""))
        .getClassName();
  }
  return type.getClassName();
}