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

The following examples show how to use org.objectweb.asm.Type#getInternalName() . 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: WeavingMethodVisitor.java    From glowroot with Apache License 2.0 6 votes vote down vote up
private static Object convert(Type type) {
    switch (type.getSort()) {
        case Type.BOOLEAN:
        case Type.CHAR:
        case Type.BYTE:
        case Type.SHORT:
        case Type.INT:
            return INTEGER;
        case Type.FLOAT:
            return FLOAT;
        case Type.LONG:
            return LONG;
        case Type.DOUBLE:
            return DOUBLE;
        case Type.ARRAY:
            return type.getDescriptor();
        case Type.OBJECT:
            return type.getInternalName();
        case Type.METHOD:
            return type.getDescriptor();
        default:
            throw new IllegalStateException("Unexpected type: " + type.getDescriptor());
    }
}
 
Example 2
Source File: Transformer.java    From Kettle with GNU General Public License v3.0 6 votes vote down vote up
public static void remapVirtual(AbstractInsnNode insn) {
    MethodInsnNode method = (MethodInsnNode) insn;

    if (!(("java/lang/Class".equals(method.owner) && ("getField".equals(method.name)
            || "getDeclaredField".equals(method.name)
            || "getMethod".equals(method.name)
            || "getDeclaredMethod".equals(method.name)
            || "getSimpleName".equals(method.name))
    )
            || ("getName".equals(method.name) && ("java/lang/reflect/Field".equals(method.owner)
            || "java/lang/reflect/Method".equals(method.owner)))
            || ("java/lang/ClassLoader".equals(method.owner) && "loadClass".equals(method.name)))) {
        return;
    }

    Type returnType = Type.getReturnType(method.desc);

    ArrayList<Type> args = new ArrayList<>();
    args.add(Type.getObjectType(method.owner));
    args.addAll(Arrays.asList(Type.getArgumentTypes(method.desc)));

    method.setOpcode(Opcodes.INVOKESTATIC);
    method.owner = Type.getInternalName(RemappedMethods.class);
    method.desc = Type.getMethodDescriptor(returnType, args.toArray(new Type[args.size()]));
}
 
Example 3
Source File: MethodAssignableValue.java    From yql-plus with Apache License 2.0 5 votes vote down vote up
public MethodAssignableValue(Method method, TypeWidget type, BytecodeExpression target) {
    this.type = type;
    this.target = target;
    this.ownerName = Type.getInternalName(method.getDeclaringClass());
    this.name = method.getName();
    this.op = method.getDeclaringClass().isInterface() ? Opcodes.INVOKEINTERFACE : Opcodes.INVOKEVIRTUAL;
    this.desc = Type.getMethodDescriptor(method);
}
 
Example 4
Source File: MethodGenerator.java    From SonarPet with GNU General Public License v3.0 5 votes vote down vote up
public void invokeInterface(String name, Type ownerType, Type returnType, Type... parameterTypes) {
    super.visitMethodInsn(
            INVOKEINTERFACE,
            ownerType.getInternalName(),
            name,
            Type.getMethodDescriptor(returnType, parameterTypes),
            true
    );
}
 
Example 5
Source File: UtilMethods.java    From rembulan with Apache License 2.0 5 votes vote down vote up
public static AbstractInsnNode String_compareTo() {
	return new MethodInsnNode(
			INVOKEVIRTUAL,
			Type.getInternalName(String.class),
			"compareTo",
			Type.getMethodDescriptor(
					Type.INT_TYPE,
					Type.getType(String.class)),
			false);
}
 
Example 6
Source File: LambdaDesugaring.java    From bazel with Apache License 2.0 5 votes vote down vote up
private static String[] toInternalNames(Class<?>[] classes) {
  String[] result = new String[classes.length];
  for (int i = 0; i < classes.length; ++i) {
    result[i] = Type.getInternalName(classes[i]);
  }
  return result;
}
 
Example 7
Source File: TypeList.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public String[] toInternalNames() {
    String[] internalNames = new String[types.size()];
    int i = 0;
    for (Class<?> type : types) {
        internalNames[i++] = Type.getInternalName(type);
    }
    return internalNames.length == 0
            ? NO_INTERFACES
            : internalNames;
}
 
Example 8
Source File: ConversionMethods.java    From rembulan with Apache License 2.0 5 votes vote down vote up
public static AbstractInsnNode floatValueOf() {
	return new MethodInsnNode(
			INVOKESTATIC,
			Type.getInternalName(Conversions.class),
			"floatValueOf",
			Type.getMethodDescriptor(
					Type.getType(Double.class),
					Type.getType(Object.class)),
			false);
}
 
Example 9
Source File: TableMethods.java    From rembulan with Apache License 2.0 5 votes vote down vote up
public static AbstractInsnNode rawset_int() {
	return new MethodInsnNode(
			INVOKEVIRTUAL,
			Type.getInternalName(Table.class),
			"rawset",
			Type.getMethodDescriptor(
					Type.VOID_TYPE,
					Type.LONG_TYPE,
					Type.getType(Object.class)),
			false);
}
 
Example 10
Source File: SynchronizationGenerators.java    From coroutines with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Generates instruction to exit a monitor (top item on the stack) and remove it from the {@link LockState} object sitting in the
 * lockstate variable.
 * @param markerType debug marker type
 * @param lockVars variables for lock/synchpoint functionality
 * @return instructions to exit a monitor and remove it from the {@link LockState} object
 * @throws NullPointerException if any argument is {@code null}
 * @throws IllegalArgumentException if lock variables aren't set (the method doesn't contain any monitorenter/monitorexit instructions)
 */
public static InsnList exitMonitorAndDelete(MarkerType markerType, LockVariables lockVars) {
    Validate.notNull(markerType);
    Validate.notNull(lockVars);

    Variable lockStateVar = lockVars.getLockStateVar();
    Validate.isTrue(lockStateVar != null);

    Type clsType = Type.getType(LOCKSTATE_EXIT_METHOD.getDeclaringClass());
    Type methodType = Type.getType(LOCKSTATE_EXIT_METHOD);
    String clsInternalName = clsType.getInternalName();
    String methodDesc = methodType.getDescriptor();
    String methodName = LOCKSTATE_EXIT_METHOD.getName();
    
    // NOTE: This removes the lock AFTER unlocking.
    return merge(
            debugMarker(markerType, "Exiting monitor and unstoring"),
                                                                     // [obj]
            new InsnNode(Opcodes.DUP),                               // [obj, obj]
            new InsnNode(Opcodes.MONITOREXIT),                       // [obj]
            new VarInsnNode(Opcodes.ALOAD, lockStateVar.getIndex()), // [obj, lockState]
            new InsnNode(Opcodes.SWAP),                              // [lockState, obj]
            new MethodInsnNode(Opcodes.INVOKEVIRTUAL,                // []
                    clsInternalName,
                    methodName,
                    methodDesc,
                    false)
    );
}
 
Example 11
Source File: PluginClassRenamer.java    From glowroot with Apache License 2.0 5 votes vote down vote up
private @PolyNull Type hack(@PolyNull Type type) {
    if (type == null) {
        return null;
    }
    String internalName = type.getInternalName();
    if (collocate(internalName)) {
        return Type.getObjectType(internalName + "_");
    } else {
        return type;
    }
}
 
Example 12
Source File: Utils.java    From OpenPeripheral with MIT License 5 votes vote down vote up
public static String[] getInterfaces(Set<Class<?>> exposedInterfaces) {
	String[] result = new String[exposedInterfaces.size()];
	int i = 0;
	for (Class<?> cls : exposedInterfaces)
		result[i++] = Type.getInternalName(cls);

	return result;
}
 
Example 13
Source File: EventSubscriber.java    From Hyperium with GNU Lesser General Public License v3.0 4 votes vote down vote up
private Class<?> createHandler(Method callback) {
    // ClassName$methodName_EventClass_XXX
    String name = objName + "$" + callback.getName() + "_" + callback.getParameters()[0].getType().getSimpleName() + "_" + (ID++);
    String eventType = Type.getInternalName(callback.getParameterTypes()[0]);

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    MethodVisitor mv;
    String desc = name.replace(".", "/");
    String instanceClassName = instance.getClass().getName().replace(".", "/");

    cw.visit(V1_6, ACC_PUBLIC | ACC_SUPER, desc, null, "java/lang/Object", new String[]{ "cc/hyperium/event/EventSubscriber$EventHandler" });

    cw.visitSource(".dynamic", null);
    {
        cw.visitField(ACC_PUBLIC, "instance", "Ljava/lang/Object;", null, null).visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(Ljava/lang/Object;)V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitFieldInsn(PUTFIELD, desc, "instance", "Ljava/lang/Object;");
        mv.visitInsn(RETURN);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "handle", "(Ljava/lang/Object;)V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, desc, "instance", "Ljava/lang/Object;");
        mv.visitTypeInsn(CHECKCAST, instanceClassName);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitTypeInsn(CHECKCAST, eventType);
        mv.visitMethodInsn(INVOKEVIRTUAL, instanceClassName, callback.getName(), Type.getMethodDescriptor(callback), false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }
    cw.visitEnd();

    byte[] handlerClassBytes = cw.toByteArray();
    return LOADER.define(name, handlerClassBytes);
}
 
Example 14
Source File: ScottyMethodAdapter.java    From copper-engine with Apache License 2.0 4 votes vote down vote up
void recreateLocals(StackInfo info) {
    if (info.localsSize() == 0)
        return;
    visitVarInsn(ALOAD, 0);
    visitFieldInsn(GETFIELD, currentClassName, "__stack", "Ljava/util/Stack;");
    visitVarInsn(ALOAD, 0);
    visitFieldInsn(GETFIELD, currentClassName, "__stackPosition", "I");
    visitMethodInsn(INVOKEVIRTUAL, "java/util/Stack", "get", "(I)Ljava/lang/Object;");
    visitTypeInsn(CHECKCAST, "org/copperengine/core/StackEntry");
    visitFieldInsn(GETFIELD, "org/copperengine/core/StackEntry", "locals", "[Ljava/lang/Object;");
    for (int i = 0; i < info.localsSize(); ++i) {
        Type t = info.getLocal(i);
        if (t != null) {
            if (t != StackInfo.AconstNullType) {
                super.visitInsn(DUP);
                super.visitIntInsn(SIPUSH, i);
                super.visitInsn(AALOAD);
            } else {
                super.visitInsn(ACONST_NULL);
            }
            if (t == Type.BOOLEAN_TYPE || t == Type.BYTE_TYPE || t == Type.SHORT_TYPE || t == Type.INT_TYPE || t == Type.CHAR_TYPE) {
                super.visitTypeInsn(CHECKCAST, Type.getInternalName(Integer.class));
                super.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Integer", "intValue", "()I", false);
                super.visitVarInsn(ISTORE, i);
            } else if (t == Type.FLOAT_TYPE) {
                super.visitTypeInsn(CHECKCAST, Type.getInternalName(Float.class));
                super.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Float", "floatValue", "()F", false);
                super.visitVarInsn(FSTORE, i);
            } else if (t == Type.LONG_TYPE) {
                super.visitTypeInsn(CHECKCAST, Type.getInternalName(Long.class));
                super.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Long", "longValue", "()J", false);
                super.visitVarInsn(LSTORE, i);
            } else if (t == Type.DOUBLE_TYPE) {
                super.visitTypeInsn(CHECKCAST, Type.getInternalName(Double.class));
                super.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Double", "doubleValue", "()D", false);
                super.visitVarInsn(DSTORE, i);
            } else {
                if (!t.getInternalName().equals(Type.getInternalName(Object.class)) && t != StackInfo.AconstNullType)
                    super.visitTypeInsn(CHECKCAST, t.getInternalName());
                super.visitVarInsn(ASTORE, i);
            }
        }
    }
    visitInsn(POP);
}
 
Example 15
Source File: BlockSpaceTransform.java    From OpenModsLib with MIT License 4 votes vote down vote up
private static void createTransformMethod(MethodVisitor mv, boolean invert) {
	// 0 - this (unused)
	// 1 - orientation
	// 2,3 - x
	// 4,5 - y
	// 6,7 - z
	mv.visitCode();

	mv.visitVarInsn(Opcodes.ALOAD, 1);

	final String enumType = Type.getInternalName(Enum.class);
	mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, enumType, "ordinal", Type.getMethodDescriptor(Type.INT_TYPE), false);

	final Orientation[] orientations = Orientation.values();

	final Label defaultLabel = new Label();
	final Label[] targets = new Label[orientations.length];
	for (int i = 0; i < orientations.length; i++)
		targets[i] = new Label();

	mv.visitTableSwitchInsn(0, orientations.length - 1, defaultLabel, targets);

	{
		mv.visitLabel(defaultLabel);
		final String excType = Type.getInternalName(IllegalArgumentException.class);
		final Type stringType = Type.getType(String.class);

		mv.visitTypeInsn(Opcodes.NEW, excType);
		mv.visitInsn(Opcodes.DUP);
		mv.visitVarInsn(Opcodes.ALOAD, 1);
		mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Object.class), "toString", Type.getMethodDescriptor(stringType), false);
		mv.visitMethodInsn(Opcodes.INVOKESPECIAL, excType, "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, stringType), false);
		mv.visitInsn(Opcodes.ATHROW);
	}

	{
		final String createVectorDesc = Type.getMethodDescriptor(Type.getType(Vec3d.class), Type.DOUBLE_TYPE, Type.DOUBLE_TYPE, Type.DOUBLE_TYPE);
		final String selfType = Type.getInternalName(BlockSpaceTransform.class);
		for (int i = 0; i < orientations.length; i++) {
			mv.visitLabel(targets[i]);
			final Orientation orientation = orientations[i];
			final Matrix3f mat = orientation.getLocalToWorldMatrix();
			if (invert) mat.invert();

			createRowVectorMultiplication(mv, mat.m00, mat.m01, mat.m02);
			createRowVectorMultiplication(mv, mat.m10, mat.m11, mat.m12);
			createRowVectorMultiplication(mv, mat.m20, mat.m21, mat.m22);

			mv.visitMethodInsn(Opcodes.INVOKESTATIC, selfType, "createVector", createVectorDesc, false);
			mv.visitInsn(Opcodes.ARETURN);
		}
	}

	mv.visitMaxs(0, 0);
	mv.visitEnd();
}
 
Example 16
Source File: RetroWeaver.java    From CodenameOne with GNU General Public License v2.0 4 votes vote down vote up
public void visitLdcInsn(final Object cst) {
	if (cst instanceof Type) {
		/**
		 * Fix class literals. The 1.5 VM has had its ldc* instructions updated so
		 * that it knows how to deal with CONSTANT_Class in addition to the other
		 * types. So, we have to search for uses of ldc* that point to a
		 * CONSTANT_Class and replace them with synthetic field access the way
		 * it was generated in 1.4.
		 */

		// LDC or LDC_W with a class as argument

		Type t = (Type) cst;
		String fieldName = getClassLiteralFieldName(t);

		classLiteralCalls.add(fieldName);

		mv.visitFieldInsn(GETSTATIC, className, fieldName, CLASS_FIELD_DESC);
		Label nonNullLabel = new Label();
		mv.visitJumpInsn(IFNONNULL, nonNullLabel);
		String s;
		if (t.getSort() == Type.OBJECT) {
			s = t.getInternalName();
		} else {
			s = t.getDescriptor();
		}
		
		/* convert retroweaver runtime classes:
		 * 		Enum into net.sourceforge.retroweaver.runtime.Enum_
		 *		concurrent classes into their backport equivalent
		 *		...
		 */
		s = NameTranslator.getGeneralTranslator().getClassMirrorTranslationDescriptor(s);
		s = NameTranslator.getStringBuilderTranslator().getClassMirrorTranslationDescriptor(s);
		s = NameTranslator.getHarmonyTranslator().getClassMirrorTranslationDescriptor(s);

		//generateClassCall(mv, s);
            
            mv.visitLdcInsn(s.replace('/', '.'));
            if (isInterface) {
                Label beginTry = new Label();
                Label endTry = new Label();
                Label catchBlock = new Label();
                mv.visitTryCatchBlock(beginTry, endTry, catchBlock, "java/lang/Exception");
                mv.visitLabel(beginTry);
                mv.visitMethodInsn(INVOKESTATIC, "java/lang/Class", "forName", "(Ljava/lang/String;)Ljava/lang/Class;");        
                mv.visitFieldInsn(PUTSTATIC, className, fieldName, CLASS_FIELD_DESC);
                mv.visitLabel(endTry);
                mv.visitJumpInsn(GOTO, nonNullLabel);
                mv.visitLabel(catchBlock);
                mv.visitInsn(POP);
            } else {
                mv.visitMethodInsn(INVOKESTATIC, className, "class$", "(Ljava/lang/String;)Ljava/lang/Class;");
                mv.visitFieldInsn(PUTSTATIC, className, fieldName, CLASS_FIELD_DESC);
            }
            
		mv.visitLabel(nonNullLabel);
		mv.visitFieldInsn(GETSTATIC, className, fieldName, CLASS_FIELD_DESC);
            
            
            /*
        Label l0 = new Label();
        Label l1 = new Label();
        Label l2 = new Label();
        mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception");
        mv.visitLabel(l0);

        String s;
        if (t.getSort() == Type.OBJECT) {
                s = t.getInternalName();
        } else {
                s = t.getDescriptor();
        }
        s = NameTranslator.getGeneralTranslator().getClassMirrorTranslationDescriptor(s);
        s = NameTranslator.getStringBuilderTranslator().getClassMirrorTranslationDescriptor(s);
        s = NameTranslator.getHarmonyTranslator().getClassMirrorTranslationDescriptor(s);

        
        mv.visitLdcInsn(s.replace('/', '.'));
        mv.visitMethodInsn(INVOKESTATIC, "java/lang/Class", "forName", "(Ljava/lang/String;)Ljava/lang/Class;");
        mv.visitLabel(l1);
        mv.visitLabel(l2);

             */
	} else {
		super.visitLdcInsn(cst);
	}
}
 
Example 17
Source File: MethodHandleFieldAssembler.java    From spring-boot-netty with MIT License 4 votes vote down vote up
public static void assembleMethodHandleField(final ClassWriter cw, final Method targetMethod, final String invokerName) {
    final String desc = Type.getDescriptor(MethodHandle.class);
    cw.visitField(ACC_PRIVATE | ACC_FINAL + ACC_STATIC, "HANDLE", desc, null, null);
    cw.visitEnd();

    final MethodVisitor ctor = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
    final Label cs = createLabel();
    final Label ce = createLabel();

    ctor.visitCode();
    ctor.visitLabel(cs);

    final String clName = targetMethod.getDeclaringClass().getCanonicalName();
    final String targetMethodName = targetMethod.getName();
    ctor.visitLdcInsn(clName);
    ctor.visitLdcInsn(targetMethodName);

    final Parameter[] parameters = targetMethod.getParameters();
    ctor.visitLdcInsn(parameters.length);

    final Type classType = Type.getType(Class.class);
    final String descriptor = classType.getInternalName();
    ctor.visitTypeInsn(ANEWARRAY, descriptor);

    for (int i = 0; i < parameters.length; i++) {
        ctor.visitInsn(DUP);
        ctor.visitLdcInsn(i);

        final Class<?> paramClass = parameters[i].getType();
        if (paramClass.isPrimitive()) {
            final Class<?> wrapper = Primitives.wrap(paramClass);
            final String holderName = Type.getType(wrapper).getInternalName();

            ctor.visitFieldInsn(GETSTATIC, holderName, "TYPE", CL_DESCRIPTOR);
        } else {
            final Type parameterType = Type.getType(paramClass);
            ctor.visitLdcInsn(parameterType);
        }

        ctor.visitInsn(AASTORE);
    }

    ctor.visitMethodInsn(INVOKESTATIC, MHC_INTERNAL_NAME, "createUniversal",
            '(' + STR_DESCRIPTOR + STR_DESCRIPTOR + CLA_DESCRIPTOR + ')' + MH_DESCRIPTOR, false);
    ctor.visitFieldInsn(PUTSTATIC, invokerName, "HANDLE", MH_DESCRIPTOR);
    ctor.visitInsn(RETURN);

    ctor.visitLabel(ce);
    ctor.visitEnd();

    ctor.visitMaxs(0, 0);
}
 
Example 18
Source File: IncrementalVisitor.java    From AnoleFix with MIT License 4 votes vote down vote up
protected static String getRuntimeTypeName(Type type) {
    return "L" + type.getInternalName() + ";";
}
 
Example 19
Source File: ASMUtils.java    From rembulan with Apache License 2.0 4 votes vote down vote up
public static FrameNode frameSame1(Class clazz) {
	return new FrameNode(F_SAME1, 0, null, 1, new Object[] { Type.getInternalName(clazz) });
}
 
Example 20
Source File: DescriptorUtils.java    From bazel with Apache License 2.0 2 votes vote down vote up
/**
 * Convert class to its binary name.
 *
 * @param clazz a class
 * @return class binary name i.e. "java/lang/Object"
 */
public static String classToBinaryName(Class<?> clazz) {
  return Type.getInternalName(clazz);
}