Пример #1
0
        /// <summary>
        /// Gets the class name.
        /// </summary>
        /// <param name="jc">The java class.</param>
        /// <returns>The name of the java class.</returns>
        public static string GetName(this JavaClass jc)
        {
            Guard.NotNull(ref jc, nameof(jc));

            JavaConstantClass cc = jc.GetConstant <JavaConstantClass>(jc.ThisClassIndex);

            return(jc.GetConstant <JavaConstantUtf8>(cc.NameIndex).Value);
        }
Пример #2
0
        public void HelloWorld()
        {
            using Stream stream = Resource.Get("HelloWorld.class");
            JavaClass     jc = JavaClass.Create(stream);
            StringBuilder sb = new StringBuilder();

            sb.AppendLine($"class {jc.GetPackageName()}.{jc.GetName()} : {jc.SuperClassIndex}");
            foreach (JavaMethod method in jc.Methods)
            {
                sb.AppendLine();
                sb.AppendLine($"method {((JavaConstantUtf8)jc.ConstantPool[method.NameIndex]).Value}{((JavaConstantUtf8)jc.ConstantPool[method.DescriptorIndex]).Value}");
                byte[] code = GetCode(method).Code;
                for (int i = 0; i < code.Length; i++)
                {
                    JavaOpCode op      = (JavaOpCode)code[i];
                    string     printed = op.ToString();

                    switch (op)
                    {
                    case JavaOpCode.GetStatic:
                        JavaConstantFieldReference fieldRef    = (JavaConstantFieldReference)jc.ConstantPool[GetIndex(code[++i], code[++i])];
                        JavaConstantClass          c           = (JavaConstantClass)jc.ConstantPool[fieldRef.ClassIndex];
                        JavaConstantUtf8           className   = (JavaConstantUtf8)jc.ConstantPool[c.NameIndex];
                        JavaConstantNameAndType    nameAndType = (JavaConstantNameAndType)jc.ConstantPool[fieldRef.NameAndTypeIndex];
                        JavaConstantUtf8           fieldName   = (JavaConstantUtf8)jc.ConstantPool[nameAndType.NameIndex];
                        JavaConstantUtf8           descriptor  = (JavaConstantUtf8)jc.ConstantPool[nameAndType.DescriptorIndex];
                        printed = $"{printed} {className.Value}/{fieldName.Value} {descriptor.Value}";
                        break;

                    case JavaOpCode.InvokeVirtual:
                    case JavaOpCode.InvokeSpecial:
                        JavaConstantMethodReference methodRef    = (JavaConstantMethodReference)jc.ConstantPool[GetIndex(code[++i], code[++i])];
                        JavaConstantClass           c2           = (JavaConstantClass)jc.ConstantPool[methodRef.ClassIndex];
                        JavaConstantUtf8            className2   = (JavaConstantUtf8)jc.ConstantPool[c2.NameIndex];
                        JavaConstantNameAndType     nameAndType2 = (JavaConstantNameAndType)jc.ConstantPool[methodRef.NameAndTypeIndex];
                        JavaConstantUtf8            methodName   = (JavaConstantUtf8)jc.ConstantPool[nameAndType2.NameIndex];
                        JavaConstantUtf8            descriptor2  = (JavaConstantUtf8)jc.ConstantPool[nameAndType2.DescriptorIndex];
                        printed = $"{printed} {className2.Value}/{methodName.Value}{descriptor2.Value}";
                        break;

                    case JavaOpCode.BiPush:
                        printed = $"{printed} {code[++i]}";
                        break;

                    case JavaOpCode.Ldc:
                        JavaConstantString str    = (JavaConstantString)jc.ConstantPool[code[++i]];
                        JavaConstantUtf8   strVal = (JavaConstantUtf8)jc.ConstantPool[str.StringIndex];
                        printed = $"{printed} \"{strVal.Value}\"";
                        break;
                    }

                    sb.AppendLine(printed);
                }
            }

            throw new Exception(sb.ToString());
        }
        /// <summary>
        /// Gets the type reference from a module from a java class and constant pool index.
        /// </summary>
        /// <param name="module">The module.</param>
        /// <param name="jc">The java class.</param>
        /// <param name="classIndex">Index of the class.</param>
        /// <returns>The type reference.</returns>
        public static TypeReference GetJavaType(this ModuleDefinition module, JavaClass jc, ushort classIndex)
        {
            Guard.NotNull(ref module, nameof(module));
            Guard.NotNull(ref jc, nameof(jc));
            Guard.NotNull(ref classIndex, nameof(classIndex));

            JavaConstantClass constant = jc.GetConstant <JavaConstantClass>(classIndex);
            string            name     = jc.GetConstant <JavaConstantUtf8>(constant.NameIndex).Value;

            return(module.GetJavaType(name));
        }
Пример #4
0
        /// <summary>
        /// Gets the name of the super class of the given class.
        /// </summary>
        /// <param name="jc">The java class.</param>
        /// <returns>The name of the super class.</returns>
        public static string GetSuperName(this JavaClass jc)
        {
            Guard.NotNull(ref jc, nameof(jc));

            if (jc.SuperClassIndex == 0)
            {
                return("java/lang/Object");
            }

            JavaConstantClass super = jc.GetConstant <JavaConstantClass>(jc.SuperClassIndex);

            return(jc.GetConstant <JavaConstantUtf8>(super.NameIndex).Value);
        }