2
Copyright (C) 2002-2011 Jeroen Frijters
4
This software is provided 'as-is', without any express or implied
5
warranty. In no event will the authors be held liable for any damages
6
arising from the use of this software.
8
Permission is granted to anyone to use this software for any purpose,
9
including commercial applications, and to alter it and redistribute it
10
freely, subject to the following restrictions:
12
1. The origin of this software must not be misrepresented; you must not
13
claim that you wrote the original software. If you use this software
14
in a product, an acknowledgment in the product documentation would be
15
appreciated but is not required.
16
2. Altered source versions must be plainly marked as such, and must not be
17
misrepresented as being the original software.
18
3. This notice may not be removed or altered from any source distribution.
25
#if STATIC_COMPILER || STUB_GENERATOR
26
using IKVM.Reflection;
27
using Type = IKVM.Reflection.Type;
29
using System.Reflection;
32
namespace IKVM.Attributes
34
[AttributeUsage(AttributeTargets.Module | AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Struct | AttributeTargets.Delegate)]
35
public sealed class SourceFileAttribute : Attribute
39
public SourceFileAttribute(string file)
44
public string SourceFile
53
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor)]
54
public sealed class LineNumberTableAttribute : Attribute
58
public LineNumberTableAttribute(ushort lineno)
60
LineNumberWriter w = new LineNumberWriter(1);
61
w.AddMapping(0, lineno);
65
public LineNumberTableAttribute(byte[] table)
70
public sealed class LineNumberWriter
72
private System.IO.MemoryStream stream;
73
private int prevILOffset;
74
private int prevLineNum;
77
public LineNumberWriter(int estimatedCount)
79
stream = new System.IO.MemoryStream(estimatedCount * 2);
82
public void AddMapping(int ilOffset, int linenumber)
86
if(ilOffset == 0 && linenumber != 0)
88
prevLineNum = linenumber;
90
WritePackedInteger(linenumber - (64 + 50));
95
prevLineNum = linenumber & ~3;
96
WritePackedInteger(((-prevLineNum / 4) - (64 + 50)));
100
bool lineno_overflow;
102
int deltaPC = ilOffset - prevILOffset;
103
if(deltaPC >= 0 && deltaPC < 31)
105
lead = (byte)deltaPC;
113
int deltaLineNo = linenumber - prevLineNum;
115
if(deltaLineNo >= -bias && deltaLineNo < 7 - bias)
117
lead |= (byte)((deltaLineNo + bias) << 5);
118
lineno_overflow = false;
122
lead |= (byte)(7 << 5);
123
lineno_overflow = true;
125
stream.WriteByte(lead);
128
WritePackedInteger(deltaPC - (64 + 31));
132
WritePackedInteger(deltaLineNo);
134
prevILOffset = ilOffset;
135
prevLineNum = linenumber;
155
public byte[] ToArray()
157
return stream.ToArray();
161
* packed integer format:
162
* ----------------------
165
* 00 - 7F Single byte integer (-64 - 63)
166
* 80 - BF Double byte integer (-8192 - 8191)
167
* C0 - DF Triple byte integer (-1048576 - 1048576)
169
* FF Five byte integer
171
private void WritePackedInteger(int val)
173
if(val >= -64 && val < 64)
176
stream.WriteByte((byte)val);
178
else if(val >= -8192 && val < 8192)
181
stream.WriteByte((byte)(0x80 + (val >> 8)));
182
stream.WriteByte((byte)val);
184
else if(val >= -1048576 && val < 1048576)
187
stream.WriteByte((byte)(0xC0 + (val >> 16)));
188
stream.WriteByte((byte)(val >> 8));
189
stream.WriteByte((byte)val);
193
stream.WriteByte(0xFF);
194
stream.WriteByte((byte)(val >> 24));
195
stream.WriteByte((byte)(val >> 16));
196
stream.WriteByte((byte)(val >> 8));
197
stream.WriteByte((byte)(val >> 0));
202
private int ReadPackedInteger(ref int position)
204
byte b = table[position++];
209
else if((b & 0xC0) == 0x80)
211
return ((b & 0x7F) << 8) + table[position++] - 8192;
213
else if((b & 0xE0) == 0xC0)
215
int val = ((b & 0x3F) << 16);
216
val += (table[position++] << 8);
217
val += table[position++];
218
return val - 1048576;
222
int val = table[position++] << 24;
223
val += table[position++] << 16;
224
val += table[position++] << 8;
225
val += table[position++] << 0;
230
throw new InvalidProgramException();
234
public int GetLineNumber(int ilOffset)
237
int prevILOffset = 0;
238
int prevLineNum = ReadPackedInteger(ref i) + (64 + 50);
246
prevLineNum = 4 * -prevLineNum;
249
while(i < table.Length)
251
byte lead = table[i++];
252
int deltaPC = lead & 31;
253
int deltaLineNo = (lead >> 5) - 2;
256
deltaPC = ReadPackedInteger(ref i) + (64 + 31);
260
deltaLineNo = ReadPackedInteger(ref i);
262
int currILOffset = prevILOffset + deltaPC;
263
if(currILOffset > ilOffset)
267
line = prevLineNum + deltaLineNo;
268
prevILOffset = currILOffset;
275
[AttributeUsage(AttributeTargets.Class)]
276
public sealed class ExceptionIsUnsafeForMappingAttribute : Attribute
280
[AttributeUsage(AttributeTargets.Interface)]
281
public sealed class RemappedInterfaceMethodAttribute : Attribute
284
private string mappedTo;
285
private string[] throws;
287
public RemappedInterfaceMethodAttribute(string name, string mappedTo, string[] throws)
290
this.mappedTo = mappedTo;
291
this.throws = throws;
302
public string MappedTo
310
public string[] Throws
319
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
320
public sealed class RemappedClassAttribute : Attribute
323
private Type remappedType;
326
public RemappedClassAttribute(string name, System.Type remappedType)
331
public RemappedClassAttribute(string name, Type remappedType)
334
this.remappedType = remappedType;
345
public Type RemappedType
354
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)]
355
public sealed class RemappedTypeAttribute : Attribute
360
public RemappedTypeAttribute(System.Type type)
365
public RemappedTypeAttribute(Type type)
379
[AttributeUsage(AttributeTargets.Module)]
380
public sealed class JavaModuleAttribute : Attribute
382
private string[] classMap;
383
private string[] jars;
385
public JavaModuleAttribute()
389
public JavaModuleAttribute(string[] classMap)
391
this.classMap = classMap;
394
public string[] GetClassMap()
402
set { jars = value; }
406
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface | AttributeTargets.Delegate | AttributeTargets.Enum | AttributeTargets.Assembly)]
407
public sealed class NoPackagePrefixAttribute : Attribute
411
[AttributeUsage(AttributeTargets.Struct)]
412
public sealed class GhostInterfaceAttribute : Attribute
416
// Whenever the VM or compiler generates a helper class/method/field, it should be marked
417
// with this custom attribute, so that it can be hidden from Java.
418
[AttributeUsage(AttributeTargets.All)]
419
public sealed class HideFromJavaAttribute : Attribute
423
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Field | AttributeTargets.Property)]
424
public sealed class HideFromReflectionAttribute : Attribute
429
public enum Modifiers : ushort
437
Synchronized = 0x0020,
451
AccessMask = Public | Private | Protected
454
[AttributeUsage(AttributeTargets.All)]
455
public sealed class ModifiersAttribute : Attribute
457
private Modifiers modifiers;
458
private bool isInternal;
460
public ModifiersAttribute(Modifiers modifiers)
462
this.modifiers = modifiers;
465
public ModifiersAttribute(Modifiers modifiers, bool isInternal)
467
this.modifiers = modifiers;
468
this.isInternal = isInternal;
471
public bool IsInternal
479
public Modifiers Modifiers
488
[AttributeUsage(AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Field)]
489
public sealed class NameSigAttribute : Attribute
494
public NameSigAttribute(string name, string sig)
517
[AttributeUsage(AttributeTargets.Constructor | AttributeTargets.Method)]
518
public sealed class ThrowsAttribute : Attribute
520
internal string[] classes;
521
internal Type[] types;
523
// this constructor is used by ikvmc, the other constructors are for use in other .NET languages
524
public ThrowsAttribute(string[] classes)
526
this.classes = classes;
529
public ThrowsAttribute(Type type)
530
: this(new Type[] { type })
534
public ThrowsAttribute(params Type[] types)
539
// dotted Java class names (e.g. java.lang.Throwable)
541
public string[] Classes
550
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)]
551
public sealed class ImplementsAttribute : Attribute
553
private string[] interfaces;
555
// NOTE this is not CLS compliant, so maybe we should have a couple of overloads
556
public ImplementsAttribute(string[] interfaces)
558
this.interfaces = interfaces;
561
public string[] Interfaces
570
// NOTE this attribute is also used by annotation attribute classes,
571
// to give them a different name in the Java world ($Proxy[Annotation]).
572
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)]
573
public sealed class InnerClassAttribute : Attribute
575
private string innerClassName;
576
private Modifiers modifiers;
578
public InnerClassAttribute(string innerClassName, Modifiers modifiers)
580
this.innerClassName = innerClassName;
581
this.modifiers = modifiers;
584
public string InnerClassName
588
return innerClassName;
592
public Modifiers Modifiers
601
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, AllowMultiple = true)]
602
public sealed class NonNestedInnerClassAttribute : Attribute
604
private string innerClassName;
606
public NonNestedInnerClassAttribute(string innerClassName)
608
this.innerClassName = innerClassName;
611
public string InnerClassName
615
return innerClassName;
620
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)]
621
public sealed class NonNestedOuterClassAttribute : Attribute
623
private string outerClassName;
625
public NonNestedOuterClassAttribute(string outerClassName)
627
this.outerClassName = outerClassName;
630
public string OuterClassName
634
return outerClassName;
639
[AttributeUsage(AttributeTargets.Assembly)]
640
public sealed class CustomAssemblyClassLoaderAttribute : Attribute
644
public CustomAssemblyClassLoaderAttribute(Type type)
658
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Method | AttributeTargets.Field)]
659
public sealed class SignatureAttribute : Attribute
661
private string signature;
663
public SignatureAttribute(string signature)
665
this.signature = signature;
668
public string Signature
677
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)]
678
public sealed class EnclosingMethodAttribute : Attribute
680
private string className;
681
private string methodName;
682
private string methodSig;
684
public EnclosingMethodAttribute(string className, string methodName, string methodSig)
686
this.className = className;
687
this.methodName = methodName;
688
this.methodSig = methodSig;
691
public string ClassName
699
public string MethodName
707
public string MethodSignature
716
[AttributeUsage(AttributeTargets.Method)]
717
public sealed class AnnotationDefaultAttribute : Attribute
719
public const byte TAG_ENUM = (byte)'e';
720
public const byte TAG_CLASS = (byte)'c';
721
public const byte TAG_ANNOTATION = (byte)'@';
722
public const byte TAG_ARRAY = (byte)'[';
723
public const byte TAG_ERROR = (byte)'?';
724
private object defaultValue;
726
// element_value encoding:
732
// new object[] { (byte)'e', "<EnumType>", "<enumvalue>" }
734
// new object[] { (byte)'c', "<Type>" }
736
// new object[] { (byte)'@', "<AnnotationType>", ("name", (element_value))* }
738
// new object[] { (byte)'[', (element_value)* }
740
// new object[] { (byte)'?', "<exceptionClass>", "<exceptionMessage>" }
741
public AnnotationDefaultAttribute(object defaultValue)
743
this.defaultValue = defaultValue;
755
[AttributeUsage(AttributeTargets.Interface)]
756
public sealed class AnnotationAttributeAttribute : Attribute
758
private string attributeType;
760
public AnnotationAttributeAttribute(string attributeType)
762
this.attributeType = attributeType;
765
public string AttributeType
769
return attributeType;
774
[AttributeUsage(AttributeTargets.Module)]
775
public sealed class PackageListAttribute : Attribute
777
private string[] packages;
779
public PackageListAttribute(string[] packages)
781
this.packages = packages;
784
public string[] GetPackages()
790
// used in custom modifier for access stubs
791
public static class AccessStub { }