Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0fd8b3986732d36396ed7aece8d0fabf6612fc26 (plain) (blame)
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
/**
 * Copyright (c) 2002-2007 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   IBM - Initial API and implementation
 */
package org.eclipse.emf.java.impl;


import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.codegen.merge.java.facade.FacadeVisitor;
import org.eclipse.emf.codegen.merge.java.facade.JAbstractType;
import org.eclipse.emf.codegen.merge.java.facade.JImport;
import org.eclipse.emf.codegen.merge.java.facade.JType;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;
import org.eclipse.emf.common.util.ECollections;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.EDataTypeUniqueEList;
import org.eclipse.emf.ecore.util.EObjectContainmentWithInverseEList;
import org.eclipse.emf.ecore.util.EObjectResolvingEList;
import org.eclipse.emf.ecore.util.InternalEList;
import org.eclipse.emf.java.JClass;
import org.eclipse.emf.java.JCompilationUnit;
import org.eclipse.emf.java.JPackage;
import org.eclipse.emf.java.JavaFactory;
import org.eclipse.emf.java.JavaPackage;
import org.eclipse.emf.java.util.JavaUtil;


/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>JCompilation Unit</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link org.eclipse.emf.java.impl.JCompilationUnitImpl#getImports <em>Imports</em>}</li>
 *   <li>{@link org.eclipse.emf.java.impl.JCompilationUnitImpl#getComment <em>Comment</em>}</li>
 *   <li>{@link org.eclipse.emf.java.impl.JCompilationUnitImpl#getTypes <em>Types</em>}</li>
 *   <li>{@link org.eclipse.emf.java.impl.JCompilationUnitImpl#getImportedPackages <em>Imported Packages</em>}</li>
 *   <li>{@link org.eclipse.emf.java.impl.JCompilationUnitImpl#getImportedTypes <em>Imported Types</em>}</li>
 *   <li>{@link org.eclipse.emf.java.impl.JCompilationUnitImpl#getPackage <em>Package</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class JCompilationUnitImpl extends JModelElementImpl implements JCompilationUnit
{
  /**
   * The cached value of the '{@link #getImports() <em>Imports</em>}' attribute list.
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @see #getImports()
   * @generated
   * @ordered
   */
  protected EList<String> imports;

  /**
   * The default value of the '{@link #getComment() <em>Comment</em>}' attribute.
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @see #getComment()
   * @generated
   * @ordered
   */
  protected static final String COMMENT_EDEFAULT = null;

  /**
   * The cached value of the '{@link #getComment() <em>Comment</em>}' attribute.
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @see #getComment()
   * @generated
   * @ordered
   */
  protected String comment = COMMENT_EDEFAULT;

  /**
   * The cached value of the '{@link #getTypes() <em>Types</em>}' containment reference list.
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @see #getTypes()
   * @generated
   * @ordered
   */
  protected EList<JClass> types;

  /**
   * The cached value of the '{@link #getImportedPackages() <em>Imported Packages</em>}' reference list.
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @see #getImportedPackages()
   * @generated
   * @ordered
   */
  protected EList<JPackage> importedPackages;

  /**
   * The cached value of the '{@link #getImportedTypes() <em>Imported Types</em>}' reference list.
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @see #getImportedTypes()
   * @generated
   * @ordered
   */
  protected EList<JClass> importedTypes;

  /**
   * The cached value of the '{@link #getPackage() <em>Package</em>}' reference.
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @see #getPackage()
   * @generated
   * @ordered
   */
  protected JPackage package_;

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  protected JCompilationUnitImpl()
  {
    super();
  }

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  @Override
  protected EClass eStaticClass()
  {
    return JavaPackage.Literals.JCOMPILATION_UNIT;
  }

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  public JPackage getPackage()
  {
    if (package_ != null && package_.eIsProxy())
    {
      InternalEObject oldPackage = (InternalEObject)package_;
      package_ = (JPackage)eResolveProxy(oldPackage);
      if (package_ != oldPackage)
      {
        if (eNotificationRequired())
          eNotify(new ENotificationImpl(this, Notification.RESOLVE, JavaPackage.JCOMPILATION_UNIT__PACKAGE, oldPackage, package_));
      }
    }
    return package_;
  }

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  public JPackage basicGetPackage()
  {
    return package_;
  }

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  public void setPackage(JPackage newPackage)
  {
    JPackage oldPackage = package_;
    package_ = newPackage;
    if (eNotificationRequired())
      eNotify(new ENotificationImpl(this, Notification.SET, JavaPackage.JCOMPILATION_UNIT__PACKAGE, oldPackage, package_));
  }

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  @SuppressWarnings("unchecked")
  @Override
  public NotificationChain eInverseAdd(InternalEObject otherEnd, int featureID, NotificationChain msgs)
  {
    switch (featureID)
    {
      case JavaPackage.JCOMPILATION_UNIT__TYPES:
        return ((InternalEList<InternalEObject>)(InternalEList<?>)getTypes()).basicAdd(otherEnd, msgs);
    }
    return super.eInverseAdd(otherEnd, featureID, msgs);
  }

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  @Override
  public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs)
  {
    switch (featureID)
    {
      case JavaPackage.JCOMPILATION_UNIT__TYPES:
        return ((InternalEList<?>)getTypes()).basicRemove(otherEnd, msgs);
    }
    return super.eInverseRemove(otherEnd, featureID, msgs);
  }

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  public EList<String> getImports()
  {
    if (imports == null)
    {
      imports = new EDataTypeUniqueEList<String>(String.class, this, JavaPackage.JCOMPILATION_UNIT__IMPORTS);
    }
    return imports;
  }

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  public String getComment()
  {
    return comment;
  }

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  public void setComment(String newComment)
  {
    String oldComment = comment;
    comment = newComment;
    if (eNotificationRequired())
      eNotify(new ENotificationImpl(this, Notification.SET, JavaPackage.JCOMPILATION_UNIT__COMMENT, oldComment, comment));
  }

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  public EList<JClass> getTypes()
  {
    if (types == null)
    {
      types = new EObjectContainmentWithInverseEList<JClass>(JClass.class, this, JavaPackage.JCOMPILATION_UNIT__TYPES, JavaPackage.JCLASS__UNIT);
    }
    return types;
  }

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  public EList<JPackage> getImportedPackages()
  {
    if (importedPackages == null)
    {
      importedPackages = new EObjectResolvingEList<JPackage>(JPackage.class, this, JavaPackage.JCOMPILATION_UNIT__IMPORTED_PACKAGES);
    }
    return importedPackages;
  }

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  public EList<JClass> getImportedTypes()
  {
    if (importedTypes == null)
    {
      importedTypes = new EObjectResolvingEList<JClass>(JClass.class, this, JavaPackage.JCOMPILATION_UNIT__IMPORTED_TYPES);
    }
    return importedTypes;
  }

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  @Override
  public Object eGet(int featureID, boolean resolve, boolean coreType)
  {
    switch (featureID)
    {
      case JavaPackage.JCOMPILATION_UNIT__IMPORTS:
        return getImports();
      case JavaPackage.JCOMPILATION_UNIT__COMMENT:
        return getComment();
      case JavaPackage.JCOMPILATION_UNIT__TYPES:
        return getTypes();
      case JavaPackage.JCOMPILATION_UNIT__IMPORTED_PACKAGES:
        return getImportedPackages();
      case JavaPackage.JCOMPILATION_UNIT__IMPORTED_TYPES:
        return getImportedTypes();
      case JavaPackage.JCOMPILATION_UNIT__PACKAGE:
        if (resolve) return getPackage();
        return basicGetPackage();
    }
    return super.eGet(featureID, resolve, coreType);
  }

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  @SuppressWarnings("unchecked")
  @Override
  public void eSet(int featureID, Object newValue)
  {
    switch (featureID)
    {
      case JavaPackage.JCOMPILATION_UNIT__IMPORTS:
        getImports().clear();
        getImports().addAll((Collection<? extends String>)newValue);
        return;
      case JavaPackage.JCOMPILATION_UNIT__COMMENT:
        setComment((String)newValue);
        return;
      case JavaPackage.JCOMPILATION_UNIT__TYPES:
        getTypes().clear();
        getTypes().addAll((Collection<? extends JClass>)newValue);
        return;
      case JavaPackage.JCOMPILATION_UNIT__IMPORTED_PACKAGES:
        getImportedPackages().clear();
        getImportedPackages().addAll((Collection<? extends JPackage>)newValue);
        return;
      case JavaPackage.JCOMPILATION_UNIT__IMPORTED_TYPES:
        getImportedTypes().clear();
        getImportedTypes().addAll((Collection<? extends JClass>)newValue);
        return;
      case JavaPackage.JCOMPILATION_UNIT__PACKAGE:
        setPackage((JPackage)newValue);
        return;
    }
    super.eSet(featureID, newValue);
  }

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  @Override
  public void eUnset(int featureID)
  {
    switch (featureID)
    {
      case JavaPackage.JCOMPILATION_UNIT__IMPORTS:
        getImports().clear();
        return;
      case JavaPackage.JCOMPILATION_UNIT__COMMENT:
        setComment(COMMENT_EDEFAULT);
        return;
      case JavaPackage.JCOMPILATION_UNIT__TYPES:
        getTypes().clear();
        return;
      case JavaPackage.JCOMPILATION_UNIT__IMPORTED_PACKAGES:
        getImportedPackages().clear();
        return;
      case JavaPackage.JCOMPILATION_UNIT__IMPORTED_TYPES:
        getImportedTypes().clear();
        return;
      case JavaPackage.JCOMPILATION_UNIT__PACKAGE:
        setPackage((JPackage)null);
        return;
    }
    super.eUnset(featureID);
  }

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  @Override
  public boolean eIsSet(int featureID)
  {
    switch (featureID)
    {
      case JavaPackage.JCOMPILATION_UNIT__IMPORTS:
        return imports != null && !imports.isEmpty();
      case JavaPackage.JCOMPILATION_UNIT__COMMENT:
        return COMMENT_EDEFAULT == null ? comment != null : !COMMENT_EDEFAULT.equals(comment);
      case JavaPackage.JCOMPILATION_UNIT__TYPES:
        return types != null && !types.isEmpty();
      case JavaPackage.JCOMPILATION_UNIT__IMPORTED_PACKAGES:
        return importedPackages != null && !importedPackages.isEmpty();
      case JavaPackage.JCOMPILATION_UNIT__IMPORTED_TYPES:
        return importedTypes != null && !importedTypes.isEmpty();
      case JavaPackage.JCOMPILATION_UNIT__PACKAGE:
        return package_ != null;
    }
    return super.eIsSet(featureID);
  }

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated
   */
  @Override
  public String toString()
  {
    if (eIsProxy()) return super.toString();

    StringBuffer result = new StringBuffer(super.toString());
    result.append(" (imports: ");
    result.append(imports);
    result.append(", comment: ");
    result.append(comment);
    result.append(')');
    return result.toString();
  }

  @Override
  protected void changeAttribute(Notification notification)
  {
    switch (notification.getFeatureID(JCompilationUnit.class))
    {
      case JavaPackage.JCOMPILATION_UNIT__JNODE:
      {
        JHelper.handleJNode(this);
        resolveIdentifiers();
        break;
      }
      case JavaPackage.JCOMPILATION_UNIT__IMPORTS:
      {
        List<JPackage> theImportedPackages = new ArrayList<JPackage>();
        List<JClass> theImportedTypes = new ArrayList<JClass>();
        for (String theImport : getImports())
        {
          if (theImport.endsWith(".*"))
          {
            theImportedPackages.add(JavaUtil.createJPackageProxy(theImport.substring(0, theImport.length() - 2)));
          }
          else
          {
            theImportedTypes.add(JavaUtil.createJClassProxy(theImport));
          }
        }

        ECollections.setEList(getImportedPackages(), theImportedPackages);
        ECollections.setEList(getImportedTypes(), theImportedTypes);

        break;
      }
    }
  }

  protected static class JHelper
  {
    protected static void handleJNode(final JCompilationUnit compilationUnit)
    {
      org.eclipse.emf.codegen.merge.java.facade.JCompilationUnit jCompilationUnit = (org.eclipse.emf.codegen.merge.java.facade.JCompilationUnit)compilationUnit.getJNode();
      if (jCompilationUnit != null)
      {
        compilationUnit.setName(jCompilationUnit.getName());
        compilationUnit.setComment(jCompilationUnit.getHeader());
        final Collection<String> theImports = new ArrayList<String>();
        final Collection<JClass> theTypes = new ArrayList<JClass>();
        
        FacadeVisitor facadeVisitor = new FacadeVisitor()
        {
          @Override
          protected boolean visit(org.eclipse.emf.codegen.merge.java.facade.JPackage jPackage)
          {
            compilationUnit.setPackage(JavaUtil.createJPackageProxy(jPackage.getName()));
            return false;
          }
          
          @Override
          protected boolean visit(JImport jImport)
          {
            theImports.add(jImport.getName());
            return false;
          }
          
          @Override
          protected boolean visit(JAbstractType abstractType)
          {
            if (abstractType instanceof JType)
            {
              JClass cls = JavaFactory.eINSTANCE.createJClass();
              cls.setJNode(abstractType);
              theTypes.add(cls);              
            }
            return false;
          }
        };
        facadeVisitor.start(jCompilationUnit);

        if (compilationUnit.getPackage() == null)
        {
          compilationUnit.setPackage(JavaUtil.createJPackageProxy(""));
        }

        compilationUnit.getImports().addAll(theImports);
        compilationUnit.getTypes().addAll(theTypes);
      }
    }
  }

  @Override
  public void resolveIdentifiers()
  {
    for (JClass jClass : getTypes())
    {
      getPackage().getTypes().add(jClass);
      
      JModelElementImpl jModelElement = (JModelElementImpl)jClass;
      jModelElement.resolveIdentifiers();
    }

    for (Iterator<JClass> i = getImportedTypes().iterator(); i.hasNext(); )
    {
      i.next();
    }
  }

  public JClass resolveJClass(String fullName)
  {
    ResourceSet resourceSet = eResource().getResourceSet();

    for (JClass jClass : getTypes())
    {
      String name = jClass.getName();
      if (JavaUtil.isPrefixOf(name, fullName))
      {
        if (fullName.length() > name.length())
        {
          return jClass.resolveJClass(fullName.substring(name.length() + 1));
        }
        else
        {
          return jClass;
        }
      }
    }

    for (String theImport : getImports())
    {
      if (theImport.endsWith(".*"))
      {
        JClass result = handlePackageImport(resourceSet, theImport.substring(0, theImport.length() - 2), fullName);
        if (result != null)
        {
          return result;
        }
      }
      else 
      {
        String className = fullName;
        int index = fullName.indexOf(".");
        if (index != -1)
        {
          className = fullName.substring(0, index);
        }
        String arrayIndices = "";
        int braceIndex = className.indexOf("[");
        if (braceIndex != -1)
        {
          arrayIndices = className.substring(braceIndex);
          className = className.substring(0, braceIndex);
        }
        if (theImport.endsWith(className))
        {
          int baseIndex = theImport.length() - className.length();
          if (baseIndex == 0 || theImport.charAt(baseIndex - 1) == '.')
          {
            if (index == -1)
            {
              return JavaUtil.createJClassProxy(theImport + arrayIndices);
            }
            else 
            {
              return 
                JavaUtil.createJClassProxy
                  (theImport.substring(0, baseIndex) + fullName.replace('.', '$') + arrayIndices);
            }
          }
        }
      }
    }

    JClass jClass = handlePackageImport(resourceSet, getPackage().getName(), fullName);
    if (jClass == null)
    {
      jClass = handlePackageImport(resourceSet, "java.lang", fullName);
      if (jClass == null)
      {
        for (int i = fullName.lastIndexOf("."); i != -1;  i = fullName.lastIndexOf(".", i - 1))
        {
          jClass = handlePackageImport(resourceSet, fullName.substring(0, i), fullName.substring(i + 1));
          if (jClass != null)
          {
            break;
          }
        }
      }
    }
    return jClass;
  }

  protected JClass handlePackageImport(ResourceSet resourceSet, String packageName, String fullName)
  {
    if (packageName == null)
    {
      packageName = "";
    } 
    else if (packageName.length() != 0)
    {
      packageName += '.';
    }
    String className = fullName;
    int index = fullName.indexOf(".");
    if (index != -1)
    {
      className = fullName.substring(0, index);
    }
    URI proxyURI = JavaUtil.createJClassProxyURI(packageName + className);
    JClass result = (JClass)resourceSet.getEObject(proxyURI, true);

    if (result != null && index != -1)
    {
      result = JavaUtil.createJClassProxy(packageName + fullName.replace('.','$'));
    }

    return result;
  }

} //JCompilationUnitImpl

Back to the top