OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [gnu/] [java/] [lang/] [reflect/] [GenericSignatureParser.java] - Blame information for rev 769

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 769 jeremybenn
/* GenericSignatureParser.java
2
   Copyright (C) 2005
3
   Free Software Foundation
4
 
5
This file is part of GNU Classpath.
6
 
7
GNU Classpath is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 2, or (at your option)
10
any later version.
11
 
12
GNU Classpath is distributed in the hope that it will be useful, but
13
WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
General Public License for more details.
16
 
17
You should have received a copy of the GNU General Public License
18
along with GNU Classpath; see the file COPYING.  If not, write to the
19
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20
02110-1301 USA.
21
 
22
Linking this library statically or dynamically with other modules is
23
making a combined work based on this library.  Thus, the terms and
24
conditions of the GNU General Public License cover the whole
25
combination.
26
 
27
As a special exception, the copyright holders of this library give you
28
permission to link this library with independent modules to produce an
29
executable, regardless of the license terms of these independent
30
modules, and to copy and distribute the resulting executable under
31
terms of your choice, provided that you also meet, for each linked
32
independent module, the terms and conditions of the license of that
33
module.  An independent module is a module which is not derived from
34
or based on this library.  If you modify this library, you may extend
35
this exception to your version of the library, but you are not
36
obligated to do so.  If you do not wish to do so, delete this
37
exception statement from your version. */
38
 
39
package gnu.java.lang.reflect;
40
 
41
import gnu.java.lang.CPStringBuilder;
42
 
43
import java.lang.reflect.Constructor;
44
import java.lang.reflect.GenericArrayType;
45
import java.lang.reflect.GenericDeclaration;
46
import java.lang.reflect.GenericSignatureFormatError;
47
import java.lang.reflect.MalformedParameterizedTypeException;
48
import java.lang.reflect.Method;
49
import java.lang.reflect.ParameterizedType;
50
import java.lang.reflect.Type;
51
import java.lang.reflect.TypeVariable;
52
import java.lang.reflect.WildcardType;
53
 
54
import java.util.ArrayList;
55
import java.util.Arrays;
56
 
57
final class TypeVariableImpl extends TypeImpl implements TypeVariable
58
{
59
    private GenericDeclaration decl;
60
    private Type[] bounds;
61
    private String name;
62
 
63
    TypeVariableImpl(GenericDeclaration decl, Type[] bounds, String name)
64
    {
65
        this.decl = decl;
66
        this.bounds = bounds;
67
        this.name = name;
68
    }
69
 
70
    Type resolve()
71
    {
72
        return this;
73
    }
74
 
75
    public Type[] getBounds()
76
    {
77
        resolve(bounds);
78
        return (Type[]) bounds.clone();
79
    }
80
 
81
    public GenericDeclaration getGenericDeclaration()
82
    {
83
        return decl;
84
    }
85
 
86
    public String getName()
87
    {
88
        return name;
89
    }
90
 
91
    public boolean equals(Object obj)
92
    {
93
        if (obj instanceof TypeVariableImpl)
94
        {
95
            TypeVariableImpl other = (TypeVariableImpl)obj;
96
            return decl.equals(other.decl) && name.equals(other.name);
97
        }
98
        return false;
99
    }
100
 
101
    public int hashCode()
102
    {
103
        return 0x5f4d5156 ^ decl.hashCode() ^ name.hashCode();
104
    }
105
 
106
    public String toString()
107
    {
108
        return name;
109
    }
110
}
111
 
112
final class ParameterizedTypeImpl extends TypeImpl implements ParameterizedType
113
{
114
    private String rawTypeName;
115
    private ClassLoader loader;
116
    private Class rawType;
117
    private Type owner;
118
    private Type[] typeArgs;
119
 
120
    ParameterizedTypeImpl(String rawTypeName, ClassLoader loader, Type owner,
121
        Type[] typeArgs)
122
    {
123
        this.rawTypeName = rawTypeName;
124
        this.loader = loader;
125
        this.owner = owner;
126
        this.typeArgs = typeArgs;
127
    }
128
 
129
    Type resolve()
130
    {
131
        if (rawType == null)
132
        {
133
            try
134
            {
135
                rawType = Class.forName(rawTypeName, false, loader);
136
            }
137
            catch (ClassNotFoundException x)
138
            {
139
                throw new TypeNotPresentException(rawTypeName, x);
140
            }
141
        }
142
        if (typeArgs == null)
143
        {
144
            if (owner == null)
145
            {
146
                return rawType;
147
            }
148
            typeArgs = new Type[0];
149
        }
150
        resolve(typeArgs);
151
        owner = resolve(owner);
152
        return this;
153
    }
154
 
155
    public Type[] getActualTypeArguments()
156
    {
157
      return (Type[]) typeArgs.clone();
158
    }
159
 
160
    public Type getRawType()
161
    {
162
        return rawType;
163
    }
164
 
165
    public Type getOwnerType()
166
    {
167
        return owner;
168
    }
169
 
170
    public boolean equals(Object obj)
171
    {
172
        if (obj instanceof ParameterizedTypeImpl)
173
        {
174
            ParameterizedTypeImpl other = (ParameterizedTypeImpl)obj;
175
            return rawType.equals(other.rawType)
176
                && ((owner == null && other.owner == null)
177
                    || owner.equals(other.owner))
178
                && Arrays.deepEquals(typeArgs, other.typeArgs);
179
        }
180
        return false;
181
    }
182
 
183
    public int hashCode()
184
    {
185
        int h = 0x58158970 ^ rawType.hashCode();
186
        if (owner != null)
187
        {
188
            h ^= Integer.reverse(owner.hashCode());
189
        }
190
        for (int i = 0; i < typeArgs.length; i++)
191
        {
192
            h ^= Integer.rotateLeft(typeArgs[i].hashCode(), i);
193
        }
194
        return h;
195
    }
196
 
197
    public String toString()
198
    {
199
        CPStringBuilder sb = new CPStringBuilder();
200
        if (owner != null)
201
        {
202
            sb.append(owner);
203
            sb.append('.');
204
            sb.append(rawType.getSimpleName());
205
        }
206
        else
207
        {
208
            sb.append(rawTypeName);
209
        }
210
        if (typeArgs.length > 0)
211
        {
212
            sb.append('<');
213
            for (int i = 0; i < typeArgs.length; i++)
214
            {
215
                if (i > 0)
216
                    sb.append(", ");
217
                if (typeArgs[i] instanceof Class)
218
                {
219
                    sb.append(((Class)typeArgs[i]).getName());
220
                }
221
                else
222
                {
223
                    sb.append(typeArgs[i]);
224
                }
225
            }
226
            sb.append('>');
227
        }
228
        return sb.toString();
229
    }
230
}
231
 
232
final class GenericArrayTypeImpl extends TypeImpl implements GenericArrayType
233
{
234
    private Type componentType;
235
 
236
    GenericArrayTypeImpl(Type componentType)
237
    {
238
        this.componentType = componentType;
239
    }
240
 
241
    Type resolve()
242
    {
243
        componentType = resolve(componentType);
244
        return this;
245
    }
246
 
247
    public Type getGenericComponentType()
248
    {
249
        return componentType;
250
    }
251
 
252
    public boolean equals(Object obj)
253
    {
254
        if (obj instanceof GenericArrayTypeImpl)
255
        {
256
            GenericArrayTypeImpl other = (GenericArrayTypeImpl)obj;
257
            return componentType.equals(other.componentType);
258
        }
259
        return false;
260
    }
261
 
262
    public int hashCode()
263
    {
264
        return 0x4be37a7f ^ componentType.hashCode();
265
    }
266
 
267
    public String toString()
268
    {
269
        return componentType + "[]";
270
    }
271
}
272
 
273
final class UnresolvedTypeVariable extends TypeImpl implements Type
274
{
275
    private GenericDeclaration decl;
276
    private String name;
277
 
278
    UnresolvedTypeVariable(GenericDeclaration decl, String name)
279
    {
280
        this.decl = decl;
281
        this.name = name;
282
    }
283
 
284
    Type resolve()
285
    {
286
        GenericDeclaration d = decl;
287
        while (d != null)
288
        {
289
            for (TypeVariable t : d.getTypeParameters())
290
            {
291
                if (t.getName().equals(name))
292
                {
293
                    return t;
294
                }
295
            }
296
            d = getParent(d);
297
        }
298
        throw new MalformedParameterizedTypeException();
299
    }
300
 
301
    private static GenericDeclaration getParent(GenericDeclaration d)
302
    {
303
        if (d instanceof Class)
304
        {
305
            Method m = ((Class)d).getEnclosingMethod();
306
            if (m != null)
307
            {
308
                return m;
309
            }
310
            Constructor c = ((Class)d).getEnclosingConstructor();
311
            if (c != null)
312
            {
313
                return c;
314
            }
315
            return ((Class)d).getEnclosingClass();
316
        }
317
        else if (d instanceof Method)
318
        {
319
            return ((Method)d).getDeclaringClass();
320
        }
321
        else if (d instanceof Constructor)
322
        {
323
            return ((Constructor)d).getDeclaringClass();
324
        }
325
        else
326
        {
327
            // TODO figure out what this represents
328
            throw new Error();
329
        }
330
    }
331
}
332
 
333
final class WildcardTypeImpl extends TypeImpl implements WildcardType
334
{
335
    private Type lower;
336
    private Type upper;
337
 
338
    WildcardTypeImpl(Type lower, Type upper)
339
    {
340
        this.lower = lower;
341
        this.upper = upper;
342
    }
343
 
344
    Type resolve()
345
    {
346
        upper = resolve(upper);
347
        lower = resolve(lower);
348
        return this;
349
    }
350
 
351
    public Type[] getUpperBounds()
352
    {
353
        if (upper == null)
354
        {
355
            return new Type[0];
356
        }
357
        return new Type[] { upper };
358
    }
359
 
360
    public Type[] getLowerBounds()
361
    {
362
        if (lower == null)
363
        {
364
            return new Type[0];
365
        }
366
        return new Type[] { lower };
367
    }
368
 
369
    public boolean equals(Object obj)
370
    {
371
        if (obj instanceof WildcardTypeImpl)
372
        {
373
            WildcardTypeImpl other = (WildcardTypeImpl)obj;
374
            return Arrays.deepEquals(getUpperBounds(), other.getUpperBounds())
375
                && Arrays.deepEquals(getLowerBounds(), other.getLowerBounds());
376
        }
377
        return false;
378
    }
379
 
380
    public int hashCode()
381
    {
382
        int h = 0x75d074fd;
383
        if (upper != null)
384
        {
385
            h ^= upper.hashCode();
386
        }
387
        if (lower != null)
388
        {
389
            h ^= lower.hashCode();
390
        }
391
        return h;
392
    }
393
 
394
    public String toString()
395
    {
396
        if (lower != null)
397
        {
398
            return "? super " + lower;
399
        }
400
        if (upper == java.lang.Object.class)
401
        {
402
            return "?";
403
        }
404
        return "? extends " + upper;
405
    }
406
}
407
 
408
class GenericSignatureParser
409
{
410
    private ClassLoader loader;
411
    private GenericDeclaration container;
412
    private String signature;
413
    private int pos;
414
 
415
    GenericSignatureParser(GenericDeclaration container, ClassLoader loader,
416
        String signature)
417
    {
418
        this.container = container;
419
        this.loader = loader;
420
        this.signature = signature;
421
    }
422
 
423
    TypeVariable[] readFormalTypeParameters()
424
    {
425
        consume('<');
426
        ArrayList<TypeVariable> params = new ArrayList<TypeVariable>();
427
        do
428
        {
429
            // TODO should we handle name clashes?
430
            params.add(readFormalTypeParameter());
431
        } while (peekChar() != '>');
432
        consume('>');
433
        TypeVariable[] list = new TypeVariable[params.size()];
434
        params.toArray(list);
435
        return list;
436
    }
437
 
438
    private TypeVariable readFormalTypeParameter()
439
    {
440
        String identifier = readIdentifier();
441
        consume(':');
442
        ArrayList<Type> bounds = new ArrayList<Type>();
443
        if (peekChar() != ':')
444
        {
445
            bounds.add(readFieldTypeSignature());
446
        }
447
        while (peekChar() == ':')
448
        {
449
            consume(':');
450
            bounds.add(readFieldTypeSignature());
451
        }
452
        Type[] b = new Type[bounds.size()];
453
        bounds.toArray(b);
454
        return new TypeVariableImpl(container, b, identifier);
455
    }
456
 
457
    Type readFieldTypeSignature()
458
    {
459
        switch (peekChar())
460
        {
461
            case 'L':
462
                return readClassTypeSignature();
463
            case '[':
464
                return readArrayTypeSignature();
465
            case 'T':
466
                return readTypeVariableSignature();
467
            default:
468
                throw new GenericSignatureFormatError();
469
        }
470
    }
471
 
472
    Type readClassTypeSignature()
473
    {
474
        consume('L');
475
        String className = "";
476
        for (;;)
477
        {
478
            String part = readIdentifier();
479
            if (peekChar() != '/')
480
            {
481
                className += part;
482
                break;
483
            }
484
            consume('/');
485
            className += part + ".";
486
        }
487
        Type[] typeArguments = null;
488
        if (peekChar() == '<')
489
        {
490
            typeArguments = readTypeArguments();
491
        }
492
        Type type = new ParameterizedTypeImpl(className, loader, null,
493
                                              typeArguments);
494
        while (peekChar() == '.')
495
        {
496
            consume('.');
497
            className += "$" + readIdentifier();
498
            typeArguments = null;
499
            if (peekChar() == '<')
500
            {
501
                typeArguments = readTypeArguments();
502
            }
503
            type = new ParameterizedTypeImpl(className, loader, type,
504
                                             typeArguments);
505
        }
506
        consume(';');
507
        return type;
508
    }
509
 
510
    private Type[] readTypeArguments()
511
    {
512
        consume('<');
513
        ArrayList<Type> list = new ArrayList<Type>();
514
        do
515
        {
516
            list.add(readTypeArgument());
517
        } while ((peekChar() != '>'));
518
        consume('>');
519
        Type[] arr = new Type[list.size()];
520
        list.toArray(arr);
521
        return arr;
522
    }
523
 
524
    private Type readTypeArgument()
525
    {
526
        char c = peekChar();
527
        if (c == '+')
528
        {
529
            consume('+');
530
            return new WildcardTypeImpl(null, readFieldTypeSignature());
531
        }
532
        else if (c == '-')
533
        {
534
            consume('-');
535
            return new WildcardTypeImpl(readFieldTypeSignature(),
536
                java.lang.Object.class);
537
        }
538
        else if (c == '*')
539
        {
540
            consume('*');
541
            return new WildcardTypeImpl(null, java.lang.Object.class);
542
        }
543
        else
544
        {
545
            return readFieldTypeSignature();
546
        }
547
    }
548
 
549
    Type readArrayTypeSignature()
550
    {
551
        consume('[');
552
        switch (peekChar())
553
        {
554
            case 'L':
555
            case '[':
556
            case 'T':
557
                return new GenericArrayTypeImpl(readFieldTypeSignature());
558
            case 'Z':
559
                consume('Z');
560
                return boolean[].class;
561
            case 'B':
562
                consume('B');
563
                return byte[].class;
564
            case 'S':
565
                consume('S');
566
                return short[].class;
567
            case 'C':
568
                consume('C');
569
                return char[].class;
570
            case 'I':
571
                consume('I');
572
                return int[].class;
573
            case 'F':
574
                consume('F');
575
                return float[].class;
576
            case 'J':
577
                consume('J');
578
                return long[].class;
579
            case 'D':
580
                consume('D');
581
                return double[].class;
582
            default:
583
                throw new GenericSignatureFormatError();
584
        }
585
    }
586
 
587
    Type readTypeVariableSignature()
588
    {
589
        consume('T');
590
        String identifier = readIdentifier();
591
        consume(';');
592
        return new UnresolvedTypeVariable(container, identifier);
593
    }
594
 
595
    private String readIdentifier()
596
    {
597
        int start = pos;
598
        char c;
599
        do
600
        {
601
            readChar();
602
            c = peekChar();
603
        } while (";:./<>-+*".indexOf(c) == -1);
604
        return signature.substring(start, pos);
605
    }
606
 
607
    final char peekChar()
608
    {
609
        if (pos == signature.length())
610
            return '\u0000';
611
        else
612
            return signature.charAt(pos);
613
    }
614
 
615
    final char readChar()
616
    {
617
        return signature.charAt(pos++);
618
    }
619
 
620
    final void consume(char c)
621
    {
622
        if (readChar() != c)
623
            throw new GenericSignatureFormatError();
624
    }
625
 
626
    final void end()
627
    {
628
        if (pos != signature.length())
629
            throw new GenericSignatureFormatError();
630
    }
631
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.