Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0cf59764e576227d306a0c4ba36961309957f8a8 (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
/***************************************************************************
 * Copyright (c) 2004 - 2007 Eike Stepper, Germany.
 * 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:
 *    Eike Stepper - initial API and implementation
 **************************************************************************/
package org.eclipse.emf.internal.cdo;

import org.eclipse.emf.cdo.CDOState;
import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.eresource.impl.CDOResourceImpl;
import org.eclipse.emf.cdo.internal.protocol.model.CDOClassImpl;
import org.eclipse.emf.cdo.internal.protocol.model.CDOFeatureImpl;
import org.eclipse.emf.cdo.internal.protocol.revision.CDORevisionImpl;
import org.eclipse.emf.cdo.protocol.CDOID;
import org.eclipse.emf.cdo.protocol.model.CDOType;
import org.eclipse.emf.cdo.protocol.revision.CDORevision;

import org.eclipse.net4j.internal.util.om.trace.ContextTracer;
import org.eclipse.net4j.util.ImplementationError;
import org.eclipse.net4j.util.ReflectUtil;
import org.eclipse.net4j.util.WrappedException;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.BasicEObjectImpl;
import org.eclipse.emf.ecore.impl.EAttributeImpl;
import org.eclipse.emf.ecore.impl.EClassImpl;
import org.eclipse.emf.ecore.impl.EDataTypeImpl;
import org.eclipse.emf.ecore.impl.EReferenceImpl;
import org.eclipse.emf.ecore.impl.EStructuralFeatureImpl;
import org.eclipse.emf.ecore.impl.ETypedElementImpl;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.InternalEList;
import org.eclipse.emf.internal.cdo.bundle.OM;
import org.eclipse.emf.internal.cdo.util.GenUtil;
import org.eclipse.emf.internal.cdo.util.ModelUtil;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.List;

/**
 * @author Eike Stepper
 */
public abstract class CDOLegacyImpl extends CDOWrapperImpl
{
  private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG_OBJECT, CDOLegacyImpl.class);

  protected CDOState state;

  protected CDOResourceImpl resource;

  protected CDORevisionImpl revision;

  public CDOLegacyImpl()
  {
    state = CDOState.TRANSIENT;
  }

  public CDOState cdoState()
  {
    return state;
  }

  public CDORevisionImpl cdoRevision()
  {
    return revision;
  }

  public CDOResourceImpl cdoResource()
  {
    return resource;
  }

  public CDOClassImpl cdoClass()
  {
    return CDOObjectImpl.getCDOClass(this);
  }

  @Override
  public CDOViewImpl cdoView()
  {
    if (view == null)
    {
      view = CDOObjectImpl.getCDOView(this);
    }

    return view;
  }

  public CDOState cdoInternalSetState(CDOState state)
  {
    if (this.state != state)
    {
      if (TRACER.isEnabled())
      {
        TRACER.format("Setting state {0} for {1}", state, this);
      }

      try
      {
        return this.state;
      }
      finally
      {
        this.state = state;
      }
    }
    else
    {
      // TODO Detect duplicate cdoInternalSetState() calls
      return null;
    }
  }

  public void cdoInternalSetRevision(CDORevision revision)
  {
    if (TRACER.isEnabled())
    {
      TRACER.format("Setting revision: {0}", revision);
    }

    this.revision = (CDORevisionImpl)revision;
  }

  public void cdoInternalSetResource(CDOResource resource)
  {
    if (TRACER.isEnabled())
    {
      TRACER.format("Setting resource: {0}", resource);
    }

    this.resource = (CDOResourceImpl)resource;
  }

  public void cdoInternalPostAttach()
  {
    // Do nothing
  }

  public void cdoInternalPreCommit()
  {
    transferInstanceToRevision();
  }

  public void cdoInternalPostLoad()
  {
    transferRevisionToInstance();
    cdoInternalSetState(CDOState.CLEAN);
  }

  @Override
  public String toString()
  {
    if (id == null)
    {
      return eClass().getName() + "?";
    }

    return eClass().getName() + "@" + id;
  }

  protected void transferInstanceToRevision()
  {
    CDOViewImpl view = cdoView();
    if (view == null)
    {
      throw new ImplementationError("view == null");
    }

    // Handle containment
    EObject container = instance.eContainer();
    if (container != null)
    {
      if (container instanceof CDOResource)
      {
        revision.setResourceID(((CDOResource)container).cdoID());
        revision.setContainerID(CDOID.NULL);
        revision.setContainingFeature(0);
      }
      else
      {
        revision.setResourceID(CDOID.NULL);
        CDOID containerID = view.provideCDOID(container);
        if (containerID.isNull())
        {
          throw new ImplementationError("containerID.isNull()");
        }

        int containerFeatureID = instance.eContainerFeatureID();// containER???
        revision.setContainerID(containerID);
        revision.setContainingFeature(containerFeatureID);
      }
    }

    // Handle values
    CDOClassImpl cdoClass = revision.getCDOClass();
    CDOFeatureImpl[] features = cdoClass.getAllFeatures();
    for (int i = 0; i < features.length; i++)
    {
      CDOFeatureImpl feature = features[i];
      Object instanceValue = getInstanceValue(instance, feature);
      if (feature.isMany())
      {
        List revisionList = revision.getList(feature); // TODO lazy?
        revisionList.clear();

        if (instanceValue != null)
        {
          if (instanceValue instanceof InternalEList)
          {
            InternalEList instanceList = (InternalEList)instanceValue;
            if (instanceList != null)
            {
              for (Iterator it = instanceList.basicIterator(); it.hasNext();)
              {
                Object instanceElement = it.next();
                if (instanceElement != null && feature.isReference())
                {
                  instanceElement = view.convertObjectToID(instanceElement);
                }

                revisionList.add(instanceElement);
              }
            }
          }
          else
          {
            throw new ImplementationError("Not an InternalEList: " + instanceValue.getClass().getName());
          }
        }
      }
      else
      {
        if (instanceValue != null && feature.isReference())
        {
          instanceValue = view.convertObjectToID(instanceValue);
        }

        revision.setValue(feature, instanceValue);
      }
    }
  }

  protected void transferRevisionToInstance()
  {
    CDOViewImpl view = cdoView();
    if (view == null)
    {
      throw new ImplementationError("view == null");
    }

    // Handle containment
    CDOID containerID = revision.getContainerID();
    if (containerID.isNull())
    {
      CDOID resourceID = revision.getResourceID();
      Resource.Internal resource = (Resource.Internal)view.lookupInstance(resourceID);
      transferResourceToInstance((BasicEObjectImpl)instance, resource);
    }
    else
    {
      int containingFeatureID = revision.getContainingFeatureID();
      InternalCDOObject container = view.lookupInstance(containerID);
      ((BasicEObjectImpl)instance).eBasicSetContainer(container, containingFeatureID, null);
    }

    // Handle values
    CDOClassImpl cdoClass = revision.getCDOClass();
    CDOFeatureImpl[] features = cdoClass.getAllFeatures();
    for (int i = 0; i < features.length; i++)
    {
      CDOFeatureImpl feature = features[i];
      Object value = revision.getValue(feature);
      if (feature.isMany())
      {
        InternalEList instanceList = (InternalEList)getInstanceValue(instance, feature);
        if (instanceList != null)
        {
          while (!instanceList.isEmpty())
          {
            Object toBeRemoved = instanceList.basicGet(0);
            instanceList.basicRemove(toBeRemoved, null);
          }

          List revisionList = (List)value;
          for (Object toBeAdded : revisionList)
          {
            if (feature.isReference())
            {
              toBeAdded = view.convertIDToObject(toBeAdded);
            }

            instanceList.basicAdd(toBeAdded, null);
          }
        }
      }
      else
      {
        if (feature.isReference())
        {
          value = view.convertIDToObject(value);
        }

        setInstanceValue(instance, feature, value);
      }
    }
  }

  protected void transferResourceToInstance(BasicEObjectImpl instance, Resource.Internal resource)
  {
    Method method = ReflectUtil.getMethod(BasicEObjectImpl.class, "eSetDirectResource", Resource.Internal.class);

    try
    {
      ReflectUtil.invokeMethod(method, instance, resource);
    }
    catch (InvocationTargetException ex)
    {
      throw WrappedException.wrap(ex);
    }
  }

  protected Object getInstanceValue(InternalEObject instance, CDOFeatureImpl feature)
  {
    // Class<?> targetClass = target.getClass();
    // String featureName = feature.getName();
    EStructuralFeature eFeature = ModelUtil.getEFeature(feature, cdoView().getSession().getPackageRegistry());
    Object value = instance.eGet(eFeature);
    return value;

    // // TODO BOOLEAN_OBJECT?
    // String methodName = GenUtil.getFeatureGetterName(featureName,
    // feature.getType() == CDOType.BOOLEAN);
    // Method method = getMethod(targetClass, methodName,
    // ReflectUtil.NO_PARAMETERS);
    //
    // String fieldName = featureName;// XXX safeName()
    // Field field = getField(targetClass, fieldName);
    // if (field == null && feature.getType() == CDOType.BOOLEAN)
    // {
    // if (targetClass.isAssignableFrom(EAttributeImpl.class) ||
    // targetClass.isAssignableFrom(EClassImpl.class)
    // || targetClass.isAssignableFrom(EDataTypeImpl.class) ||
    // targetClass.isAssignableFrom(EReferenceImpl.class)
    // || targetClass.isAssignableFrom(EStructuralFeatureImpl.class)
    // || targetClass.isAssignableFrom(ETypedElementImpl.class))
    // {
    // // *******************************************
    // // ID_EFLAG = 1 << 15;
    // // *******************************************
    // // ABSTRACT_EFLAG = 1 << 8;
    // // INTERFACE_EFLAG = 1 << 9;
    // // *******************************************
    // // SERIALIZABLE_EFLAG = 1 << 8;
    // // *******************************************
    // // CONTAINMENT_EFLAG = 1 << 15;
    // // RESOLVE_PROXIES_EFLAG = 1 << 16;
    // // *******************************************
    // // CHANGEABLE_EFLAG = 1 << 10;
    // // VOLATILE_EFLAG = 1 << 11;
    // // TRANSIENT_EFLAG = 1 << 12;
    // // UNSETTABLE_EFLAG = 1 << 13;
    // // DERIVED_EFLAG = 1 << 14;
    // // *******************************************
    // // ORDERED_EFLAG = 1 << 8;
    // // UNIQUE_EFLAG = 1 << 9;
    // // *******************************************
    //
    // String flagName = GenUtil.getFeatureUpperName(featureName) + "_EFLAG";
    // int flagsMask = getEFlagMask(targetClass, flagName);
    //
    // field = getField(targetClass, "eFlags");
    // int value = (Integer)getFiedValue(target, field);
    // return new Boolean((value & flagsMask) != 0);
    // }
    // }
    //
    // if (field == null)
    // {
    // throw new ImplementationError("Field not found: " + fieldName);
    // }
    //
    // return getFiedValue(target, field);
  }

  protected void setInstanceValue(InternalEObject instance, CDOFeatureImpl feature, Object value)
  {
    Class<?> instanceClass = instance.getClass();
    String featureName = feature.getName();
    String fieldName = featureName;// XXX safeName()
    Field field = ReflectUtil.getField(instanceClass, fieldName);
    if (field == null && feature.getType() == CDOType.BOOLEAN)
    {
      if (instanceClass.isAssignableFrom(EAttributeImpl.class) || instanceClass.isAssignableFrom(EClassImpl.class)
          || instanceClass.isAssignableFrom(EDataTypeImpl.class)
          || instanceClass.isAssignableFrom(EReferenceImpl.class)
          || instanceClass.isAssignableFrom(EStructuralFeatureImpl.class)
          || instanceClass.isAssignableFrom(ETypedElementImpl.class))
      {
        // *******************************************
        // ID_EFLAG = 1 << 15;
        // *******************************************
        // ABSTRACT_EFLAG = 1 << 8;
        // INTERFACE_EFLAG = 1 << 9;
        // *******************************************
        // SERIALIZABLE_EFLAG = 1 << 8;
        // *******************************************
        // CONTAINMENT_EFLAG = 1 << 15;
        // RESOLVE_PROXIES_EFLAG = 1 << 16;
        // *******************************************
        // CHANGEABLE_EFLAG = 1 << 10;
        // VOLATILE_EFLAG = 1 << 11;
        // TRANSIENT_EFLAG = 1 << 12;
        // UNSETTABLE_EFLAG = 1 << 13;
        // DERIVED_EFLAG = 1 << 14;
        // *******************************************
        // ORDERED_EFLAG = 1 << 8;
        // UNIQUE_EFLAG = 1 << 9;
        // *******************************************

        String flagName = GenUtil.getFeatureUpperName(featureName) + "_EFLAG";
        int flagsMask = getEFlagMask(instanceClass, flagName);

        field = ReflectUtil.getField(instanceClass, "eFlags");
        int flags = (Integer)ReflectUtil.getValue(field, instance);
        boolean on = (Boolean)value;
        if (on)
        {
          flags |= flagsMask; // Add EFlag
        }
        else
        {
          flags &= ~flagsMask; // Remove EFlag
        }

        ReflectUtil.setValue(field, instance, flags);
        return;
      }
    }

    if (field == null)
    {
      throw new ImplementationError("Field not found: " + fieldName);
    }

    ReflectUtil.setValue(field, instance, value);
  }

  protected static int getEFlagMask(Class<?> instanceClass, String flagName)
  {
    Field field = ReflectUtil.getField(instanceClass, flagName);
    if (!field.isAccessible())
    {
      field.setAccessible(true);
    }

    try
    {
      return (Integer)field.get(null);
    }
    catch (IllegalAccessException ex)
    {
      throw new ImplementationError(ex);
    }
  }
}

Back to the top