Example #1
0
        private void WriteClass(Class javaClass)
        {
            Messages.Verbose("Writing class {0}...", javaClass.ThisClass);

            string classFileName = javaClass.ThisClass + ".class";
            string outputPath    = Path.Combine(Program.Out, classFileName);
            string outputDir     = Path.GetDirectoryName(outputPath);

            Directory.CreateDirectory(outputDir);

            if (Program.Dump)
            {
                try
                {
                    string dumpFileName = Path.Combine(Program.Out, javaClass.ThisClass + ".jbc");

                    using (FileStream output = new FileStream(dumpFileName, FileMode.Create))
                        Java.Dumper.Dump(javaClass, output);

                    if (Program.DebugBytecode)
                    {
                        Java.Attribute sourceAttr = javaClass.Attributes.Where(A => A is Java.Attributes.SourceFile).FirstOrDefault();
                        if (sourceAttr != null)
                        {
                            javaClass.Attributes.Remove(sourceAttr);
                        }

                        sourceAttr = new Java.Attributes.SourceFile(Path.GetFileName(dumpFileName));
                        javaClass.Attributes.Add(sourceAttr);
                    }
                }
                catch (Exception)
                {
                    //TODO: Error message
                }
            }

            try
            {
                using (FileStream outputStream = new FileStream(outputPath, FileMode.Create))
                    javaClass.Write(outputStream);
            }
            catch (Exception e)
            {
                Messages.Message(MessageCode.CantWriteJavaClassFile, classFileName, e.Message);
            }
        }
Example #2
0
        public static Attribute ReadAttribute(BinaryReader Reader, ConstantPool Pool)
        {
            string Name   = ((Constants.Utf8)Pool[Reader.ReadUInt16BE()]).Value;
            uint   Length = Reader.ReadUInt32BE();
            long   Next   = (long)Reader.BaseStream.Position - (long)(Length - 6);

            Attribute Result = null;

            if (Name == "AnnotationDefault")
            {
                Result = new Attributes.AnnotationDefault();
            }
            else if (Name == "Code")
            {
                Result = new Attributes.Code();
            }
            else if (Name == "ConstantValue")
            {
                Result = new Attributes.ConstantValue();
            }
            else if (Name == "Deprecated")
            {
                Result = new Attributes.Deprecated();
            }
            else if (Name == "Exceptions")
            {
                Result = new Attributes.Exceptions();
            }
            else if (Name == "InnerClasses")
            {
                Result = new Attributes.InnerClasses();
            }
            else if (Name == "LineNumberTable")
            {
                Result = new Attributes.LineNumberTable();
            }
            else if (Name == "LocalVariableTable")
            {
                Result = new Attributes.LocalVariableTable();
            }
            else if (Name == "RuntimeVisibleAnnotations")
            {
                Result = new Attributes.RuntimeVisibleAnnotations();
            }
            else if (Name == "RuntimeInvisibleAnnotations")
            {
                Result = new Attributes.RuntimeInvisibleAnnotations();
            }
            else if (Name == "RuntimeVisibleParameterAnnotations")
            {
                Result = new Attributes.RuntimeVisibleParameterAnnotations(Reader.ReadByte());
            }
            else if (Name == "RuntimeInvisibleParameterAnnotations")
            {
                Result = new Attributes.RuntimeInvisibleParameterAnnotations(Reader.ReadByte());
            }
            else if (Name == "Signature")
            {
                Result = new Attributes.Signature();
            }
            else if (Name == "SourceFile")
            {
                Result = new Attributes.SourceFile();
            }
            else if (Name == "Synthetic")
            {
                Result = new Attributes.Synthetic();
            }
            else
            {
                Result      = new Attributes.RawAttribute();
                Result.Name = Name;
            }

            Result.Read(Length, Reader, Pool);
            return(Result);
        }