Skip to content

Commit c892d17

Browse files
committed
[Xamarin.Android.Tools.Bytecode] Add EnclosingMethod, SourceFile
Context: dotnet#466 While looking at `class-parse --dump` output for various Kotlin-compiled `.class` files, providing support for the `EnclosingMethod` and `SourceFile` annotation blobs look to be useful. Add support for parsing the `EnclosingMethod` and `SourceFile` annotations.
1 parent be58159 commit c892d17

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

src/Xamarin.Android.Tools.Bytecode/AttributeInfo.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ public class AttributeInfo {
4040
public const string ConstantValue = "ConstantValue";
4141
public const string Deprecated = "Deprecated";
4242
public const string Exceptions = "Exceptions";
43+
public const string EnclosingMethod = "EnclosingMethod";
4344
public const string InnerClasses = "InnerClasses";
4445
public const string LocalVariableTable = "LocalVariableTable";
4546
public const string MethodParameters = "MethodParameters";
4647
public const string Signature = "Signature";
48+
public const string SourceFile = "SourceFile";
4749
public const string StackMapTable = "StackMapTable";
4850

4951
ushort nameIndex;
@@ -69,11 +71,13 @@ public string Name {
6971
{ typeof (CodeAttribute), Code },
7072
{ typeof (ConstantValueAttribute), ConstantValue },
7173
{ typeof (DeprecatedAttribute), Deprecated },
74+
{ typeof (EnclosingMethodAttribute), EnclosingMethod },
7275
{ typeof (ExceptionsAttribute), Exceptions },
7376
{ typeof (InnerClassesAttribute), InnerClasses },
7477
{ typeof (LocalVariableTableAttribute), LocalVariableTable },
7578
{ typeof (MethodParametersAttribute), MethodParameters },
7679
{ typeof (SignatureAttribute), Signature },
80+
{ typeof (SourceFileAttribute), SourceFile },
7781
{ typeof (StackMapTableAttribute), StackMapTable },
7882
};
7983

@@ -100,11 +104,13 @@ static AttributeInfo CreateAttribute (string name, ConstantPool constantPool, us
100104
case Code: return new CodeAttribute (constantPool, nameIndex, stream);
101105
case ConstantValue: return new ConstantValueAttribute (constantPool, nameIndex, stream);
102106
case Deprecated: return new DeprecatedAttribute (constantPool, nameIndex, stream);
107+
case EnclosingMethod: return new EnclosingMethodAttribute (constantPool, nameIndex, stream);
103108
case Exceptions: return new ExceptionsAttribute (constantPool, nameIndex, stream);
104109
case InnerClasses: return new InnerClassesAttribute (constantPool, nameIndex, stream);
105110
case LocalVariableTable: return new LocalVariableTableAttribute (constantPool, nameIndex, stream);
106111
case MethodParameters: return new MethodParametersAttribute (constantPool, nameIndex, stream);
107112
case Signature: return new SignatureAttribute (constantPool, nameIndex, stream);
113+
case SourceFile: return new SourceFileAttribute (constantPool, nameIndex, stream);
108114
case StackMapTable: return new StackMapTableAttribute (constantPool, nameIndex, stream);
109115
default: return new UnknownAttribute (constantPool, nameIndex, stream);
110116
}
@@ -221,6 +227,33 @@ public override string ToString ()
221227
}
222228
}
223229

230+
// https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.7.7
231+
public sealed class EnclosingMethodAttribute : AttributeInfo {
232+
233+
ushort classIndex, methodIndex;
234+
235+
public ConstantPoolClassItem Class {
236+
get {return (ConstantPoolClassItem) ConstantPool [classIndex];}
237+
}
238+
239+
public ConstantPoolNameAndTypeItem Method {
240+
get {return methodIndex == 0 ? null : (ConstantPoolNameAndTypeItem) ConstantPool [methodIndex];}
241+
}
242+
243+
public EnclosingMethodAttribute (ConstantPool constantPool, ushort nameIndex, Stream stream)
244+
: base (constantPool, nameIndex, stream)
245+
{
246+
var length = stream.ReadNetworkUInt32 ();
247+
classIndex = stream.ReadNetworkUInt16 ();
248+
methodIndex = stream.ReadNetworkUInt16 ();
249+
}
250+
251+
public override string ToString ()
252+
{
253+
return $"EnclosingMethod({Class}, {Method})";
254+
}
255+
}
256+
224257
// http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.7.5
225258
public sealed class ExceptionsAttribute : AttributeInfo {
226259

@@ -483,6 +516,29 @@ public override string ToString ()
483516
}
484517
}
485518

519+
// https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.7.10
520+
public sealed class SourceFileAttribute : AttributeInfo {
521+
522+
ushort sourceFileIndex;
523+
524+
public string FileName {
525+
get {return ((ConstantPoolUtf8Item) ConstantPool [sourceFileIndex]).Value;}
526+
}
527+
528+
public SourceFileAttribute (ConstantPool constantPool, ushort nameIndex, Stream stream)
529+
: base (constantPool, nameIndex, stream)
530+
{
531+
var length = stream.ReadNetworkUInt32 ();
532+
sourceFileIndex = stream.ReadNetworkUInt16 ();
533+
}
534+
535+
public override string ToString ()
536+
{
537+
return $"SourceFile('{FileName}')";
538+
}
539+
}
540+
541+
486542
// http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.7.4
487543
public sealed class StackMapTableAttribute : AttributeInfo {
488544

0 commit comments

Comments
 (0)