summaryrefslogtreecommitdiffstats
path: root/jack/src/com/android/jack/ir/ast/JDefinedClassOrInterface.java
blob: 60d91cea1985bc74949f564328595a4d65a56957 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/*
 * Copyright 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.android.jack.ir.ast;


import com.android.jack.Jack;
import com.android.jack.ir.JNodeInternalError;
import com.android.jack.ir.sourceinfo.SourceInfo;
import com.android.jack.load.ClassOrInterfaceLoader;
import com.android.jack.load.NopClassOrInterfaceLoader;
import com.android.jack.lookup.JMethodIdLookupException;
import com.android.jack.lookup.JMethodLookupException;
import com.android.jack.lookup.JMethodWithReturnLookupException;
import com.android.jack.util.AnnotationUtils;
import com.android.jack.util.NamingTools;
import com.android.sched.item.Description;
import com.android.sched.marker.Marker;
import com.android.sched.util.location.HasLocation;
import com.android.sched.util.location.Location;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

/**
 * Base class for any reference type.
 */
@Description("Declared type")
public abstract class JDefinedClassOrInterface extends JDefinedReferenceType
  implements JClassOrInterface, Annotable, CanBeAbstract, CanBeFinal, HasLocation, HasModifier {

  protected ArrayList<JField> fields = new ArrayList<JField>();

  protected ArrayList<JMethod> methods = new ArrayList<JMethod>();

  /**
   * The type which originally enclosed this type. Null if this class was a
   * top-level type. Note that all classes are converted to top-level types in
   * {@code JackIrBuilder}; this information is for tracking purposes.
   */
  private JClassOrInterface enclosingType;

  /**
   * List of inner types i.e. the types whose enclosed by this type.
   */
  @Nonnull
  private final List<JClassOrInterface> inners = new ArrayList<JClassOrInterface>();

  /**
   * True if this type is defined in the bootclasspath or the classpath but not in a sourcepath.
   */
  private boolean isExternal = true;

  /**
   * This type's modifier.
   */
  private int modifier;

  @Nonnull
  protected final List<JAnnotation> annotations = new ArrayList<JAnnotation>();

  @Nonnull
  private JPackage enclosingPackage;

  @Nonnull
  protected List<JMethodId> phantomMethods = new ArrayList<JMethodId>();

  @Nonnull
  protected List<JFieldId> phantomFields = new ArrayList<JFieldId>();

  @Nonnull
  protected ClassOrInterfaceLoader loader;

  @Nonnull
  private final Location location;

  public JDefinedClassOrInterface(@Nonnull SourceInfo info, @Nonnull String name, int modifier,
      @Nonnull JPackage enclosingPackage, @Nonnull ClassOrInterfaceLoader loader) {
    super(info, name);
    assert NamingTools.isIdentifier(name) || "package-info".equals(name);
    assert JModifier.isTypeModifier(modifier);
    assert JModifier.isValidTypeModifier(modifier);
    this.modifier = modifier;
    this.enclosingPackage = enclosingPackage;
    this.enclosingPackage.addType(this);
    this.loader = loader;
    location = loader.getLocation(this);
    updateParents(enclosingPackage);
  }

  public void setModifier(int modifier) {
    this.modifier = modifier;
  }

  @Nonnull
  public Collection<JClassOrInterface> getHierarchy() {
    HashSet<JClassOrInterface> hierarchy = new HashSet<JClassOrInterface>();

    for (JInterface jInterface : getImplements()) {
      hierarchy.add(jInterface);
      if (jInterface instanceof JDefinedInterface) {
        hierarchy.addAll(((JDefinedInterface) jInterface).getHierarchy());
      }
    }
    JClass superClass = getSuperClass();
    if (superClass != null) {
      hierarchy.add(superClass);
      if (superClass instanceof JDefinedClass) {
        hierarchy.addAll(((JDefinedClass) superClass).getHierarchy());
      }
    }
    return hierarchy;
  }


  /**
   * Adds a field to this type.
   */
  public void addField(@Nonnull JField field) {
    assert field.getEnclosingType() == this;
    assert getPhantomField(field.getName(), field.getType(), field.getId().getKind()) == null;
    fields.add(field);
  }

  @Override
  @CheckForNull
  public <T extends Marker> T getMarker(@Nonnull Class<T> cls) {
    loader.ensureMarker(this, cls);
    return super.getMarker(cls);
  }

  @Override
  @Nonnull
  public Collection<Marker> getAllMarkers() {
    loader.ensureMarkers(this);
    return super.getAllMarkers();
  }

  @Override
  public <T extends Marker> boolean containsMarker(@Nonnull Class<T> cls) {
    loader.ensureMarker(this, cls);
    return super.containsMarker(cls);
  }

  @Override
  public <T extends Marker> T removeMarker(@Nonnull Class<T> cls) {
    loader.ensureMarker(this, cls);
    return super.removeMarker(cls);
  }

  /**
   * Adds an implemented interface to this type.
   */
  public void addImplements(JInterface superInterface) {
    superInterfaces.add(superInterface);
  }

  public void setImplements(@Nonnull List<JInterface> superInterfaces) {
    this.superInterfaces = superInterfaces;
  }

  @Override
  @Nonnull
  public List<JInterface> getImplements() {
    loader.ensureHierarchy(this);
    return super.getImplements();
  }

  @Override
  public void setEnclosingPackage(@CheckForNull JPackage enclosingPackage) {
    assert enclosingPackage != null;
    this.enclosingPackage = enclosingPackage;
    updateParents(enclosingPackage);
    assert Jack.getSession().getPhantomLookup().check(this);
  }

  /**
   * Adds a method to this type.
   */
  public void addMethod(JMethod method) {
    assert method.getEnclosingType() == this;
    assert getPhantomMethod(method.getName(), method.getMethodId().getParamTypes(),
        method.getMethodId().getKind()) == null;
    methods.add(method);
  }

  /**
   * Returns the type which encloses this type.
   *
   * @return The enclosing type. May be {@code null}.
   */
  public JClassOrInterface getEnclosingType() {
    loader.ensureEnclosing(this);
    return enclosingType;
  }

  public JSession getSession() {
    return enclosingPackage.getSession();
  }

  /**
   * Returns this type's fields;does not include fields defined in a super type
   * unless they are overridden by this type.
   */
  public List<JField> getFields() {
    loader.ensureFields(this);
    return fields;
  }

  @Nonnull
  public List<JField> getFields(@Nonnull String fieldName) {
    loader.ensureFields(this, fieldName);
    List<JField> fieldsFound = new ArrayList<JField>();
    for (JField field : getFields()) {
      if (field.getName().equals(fieldName)) {
        fieldsFound.add(field);
      }
    }
    return fieldsFound;
  }

  @Override
  @Nonnull
  public JPackage getEnclosingPackage() {
    return enclosingPackage;
  }

  /**
   * Returns this type's declared methods; does not include methods defined in a
   * super type unless they are overridden by this type.
   */
  public List<JMethod> getMethods() {
    loader.ensureMethods(this);
    return methods;
  }

  /**
   * Returns the {@link JMethod} with the signature {@code signature} declared for this type.
   *
   * @return Returns the matching method if any, throws a {@link JMethodLookupException} otherwise.
   */
  @Nonnull
  public JMethod getMethod(@Nonnull String name, @Nonnull JType returnType,
      @Nonnull List<? extends JType> args) throws JMethodLookupException {
    loader.ensureMethod(this, name, args, returnType);
    for (JMethod m : methods) {
      if (m.getMethodId().equals(name, args) && m.getType().isSameType(returnType)) {
        // Only one method can be found due to the fact that we also use return type to filter
        return m;
      }
    }
    throw new JMethodWithReturnLookupException(this, name, args, returnType);
  }

  /**
   * Returns the {@link JMethod} with the signature {@code signature} declared for this type.
   *
   * @return Returns the matching method if any, throws a {@link JMethodLookupException} otherwise.
   */
  @Nonnull
  public JMethod getMethod(@Nonnull String name, @Nonnull JType returnType,
      @Nonnull JType... args) throws JMethodLookupException {
    return (getMethod(name, returnType, Arrays.asList(args)));
  }

  /**
   * Returns this type's super class, or <code>null</code> if this type is
   * {@link Object} or an interface.
   */
  @CheckForNull
  public JClass getSuperClass() {
    return null;
  }

  @Override
  public boolean isExternal() {
    return isExternal;
  }

  /**
   * Sets the type which encloses this types.
   *
   * @param enclosingType May be {@code null}.
   */
  public void setEnclosingType(JClassOrInterface enclosingType) {
    this.enclosingType = enclosingType;
  }

  public void setExternal(boolean isExternal) {
    this.isExternal = isExternal;
  }

  @Override
  public int getModifier() {
    loader.ensureModifier(this);
    return modifier;
  }

  public boolean isPublic() {
    return JModifier.isPublic(getModifier());
  }

  public boolean isProtected() {
    return JModifier.isProtected(getModifier());
  }

  public boolean isPrivate() {
    return JModifier.isPrivate(getModifier());
  }

  public boolean isStatic() {
    return JModifier.isStatic(getModifier());
  }

  public boolean isStrictfp() {
    return JModifier.isStrictfp(getModifier());
  }

  @Override
  public boolean isAbstract() {
    return JModifier.isAbstract(getModifier());
  }

  public void setAbstract() {
    modifier = getModifier() | JModifier.ABSTRACT;
  }

  @Override
  public boolean isFinal() {
    return JModifier.isFinal(getModifier());
  }

  public void setFinal() {
    modifier = getModifier() | JModifier.FINAL;
  }

  @Override
  public void addAnnotation(@Nonnull JAnnotation annotation) {
    annotations.add(annotation);
  }

  @Override
  @Nonnull
  public List<JAnnotation> getAnnotations(@Nonnull JAnnotationType annotationType) {
    loader.ensureAnnotation(this, annotationType);
    return Jack.getUnmodifiableCollections().getUnmodifiableList(
        AnnotationUtils.getAnnotation(annotations, annotationType));
  }

  @Override
  @Nonnull
  public Collection<JAnnotation> getAnnotations() {
    loader.ensureAnnotations(this);
    return Jack.getUnmodifiableCollections().getUnmodifiableCollection(annotations);
  }

  @Override
  @Nonnull
  public Collection<JAnnotationType> getAnnotationTypes() {
    loader.ensureAnnotations(this);
    return Jack.getUnmodifiableCollections().getUnmodifiableCollection(
        AnnotationUtils.getAnnotationTypes(annotations));
  }

  @Nonnull
  public List<JClassOrInterface> getMemberTypes() {
    loader.ensureInners(this);
    return Jack.getUnmodifiableCollections().getUnmodifiableList(inners);
  }

  public void addMemberType(@Nonnull JClassOrInterface jDeclaredType) {
    inners.add(jDeclaredType);
  }

  public void removeMemberType(@Nonnull JClassOrInterface jDeclaredType) {
    int index = inners.indexOf(jDeclaredType);
    if (index != -1) {
      inners.remove(index);
    }
  }

  @Override
  protected void transform(@Nonnull JNode existingNode, @CheckForNull JNode newNode,
      @Nonnull Transformation transformation) throws UnsupportedOperationException {
    if (!transform(inners, existingNode, (JClassOrInterface) newNode, transformation)) {
      if (!transform(annotations, existingNode, (JAnnotation) newNode, transformation)) {
        super.transform(existingNode, newNode, transformation);
      }
    }
  }

  @Nonnull
  @Override
  public JMethodId getMethodId(@Nonnull String name, @Nonnull List<? extends JType> argsType,
      @Nonnull MethodKind kind)
      throws JMethodLookupException {
    assert !(name.contains("(") || name.contains(")"));
    loader.ensureMethods(this);
    for (JMethod method : methods) {
      JMethodId id = method.getMethodId();
      if (id.equals(name, argsType, kind)) {
        return id;
      }
    }

    for (JInterface jType : getImplements()) {
      try {
        return jType.getMethodId(name, argsType, kind);
      } catch (JMethodLookupException e) {
        // search next
      }
    }
    JClass superClass = getSuperClass();
    if (superClass != null) {
      try {
        return superClass.getMethodId(name, argsType, kind);
      } catch (JMethodLookupException e) {
        // let the following exception be thrown
      }
    }

    throw new JMethodIdLookupException(this, name, argsType);
  }

  @Nonnull
  @Override
  public JMethodId getOrCreateMethodId(@Nonnull String name,
      @Nonnull List<? extends JType> argsType,
      @Nonnull MethodKind kind) {
    try {
      return getMethodId(name, argsType, kind);
    } catch (JMethodLookupException e) {
      synchronized (phantomMethods) {
        JMethodId id = getPhantomMethod(name, argsType, kind);

        if (id == null) {
          id = new JMethodId(name, argsType, kind);
          phantomMethods.add(id);
        }

        return id;
      }
    }
  }

  @Override
  @Nonnull
  public JFieldId getOrCreateFieldId(@Nonnull String name, @Nonnull JType type,
      @Nonnull FieldKind kind) {
    try {
      return getFieldId(name, type, kind);
    } catch (JFieldLookupException e) {
      synchronized (phantomFields) {
        JFieldId id = getPhantomField(name, type, kind);
        if (id == null) {
          id = new JFieldId(name, type, kind);
          phantomFields.add(id);
        }
        return id;
      }
    }
  }

  @Override
  @Nonnull
  public JFieldId getFieldId(
      @Nonnull String name, @Nonnull JType type,
      @Nonnull FieldKind kind) throws JFieldLookupException {
    loader.ensureFields(this);
    for (JField field : fields) {
      JFieldId id = field.getId();
      if (id.equals(name, type, kind)) {
        return id;
      }
    }

    for (JInterface jType : getImplements()) {
      try {
        return jType.getFieldId(name, type, kind);
      } catch (JFieldLookupException e) {
        // search next
      }
    }
    JClass superClass = getSuperClass();
    if (superClass != null) {
      try {
        return superClass.getFieldId(name, type, kind);
      } catch (JFieldLookupException e) {
        // let the following exception be thrown
      }
    }

    throw new JFieldLookupException(this, name, type);
  }

  @CheckForNull
  private JMethodId getPhantomMethod(@Nonnull String name, @Nonnull List<? extends JType> argsType,
      @Nonnull MethodKind kind) {
    synchronized (phantomMethods) {
      for (JMethodId id : phantomMethods) {
        if (id.equals(name, argsType, kind)) {
          return id;
        }
      }
    }
    return null;
  }

  @CheckForNull
  private JFieldId getPhantomField(@Nonnull String name, @Nonnull JType type,
      @Nonnull FieldKind kind) {
    synchronized (phantomFields) {
      for (JFieldId id : phantomFields) {
        if (id.equals(name, type, kind)) {
          return id;
        }
      }
    }
    return null;
  }

  @Nonnull
  public ClassOrInterfaceLoader getLoader() {
    return loader;
  }

  @Override
  @CheckForNull
  public JPrimitiveType getWrappedType() {
    return getWrappedType(this);
  }

  @Override
  @Nonnull
  public Location getLocation() {
    return location;
  }

  public void removeLoader() {
    loader = NopClassOrInterfaceLoader.INSTANCE;
  }

  @Override
  public final boolean isSameType(@Nonnull JType type) {
    if (type instanceof HasEnclosingPackage) {
      return this.getEnclosingPackage() == ((HasEnclosingPackage) type).getEnclosingPackage()
          && name.equals(type.getName());
    } else {
      return false;
    }
  }

  @Override
  public void checkValidity() {
    if (parent == null || parent != enclosingPackage) {
      throw new JNodeInternalError(this, "Invalid parent or enclosing package");
    }

  }
}