File: MetaData.java

package info (click to toggle)
emma-coverage 2.0.5312%2Bdfsg-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,944 kB
  • ctags: 3,626
  • sloc: java: 23,006; xml: 414; makefile: 63
file content (289 lines) | stat: -rw-r--r-- 9,148 bytes parent folder | download | duplicates (3)
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
/* Copyright (C) 2003 Vladimir Roubtsov. All rights reserved.
 * 
 * This program and the accompanying materials are made available under
 * the terms of the Common Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/cpl-v10.html
 * 
 * $Id: MetaData.java,v 1.1.1.1.2.2 2004/07/16 23:32:29 vlad_r Exp $
 */
package com.vladium.emma.data;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;

import com.vladium.logging.Logger;
import com.vladium.util.asserts.$assert;

// ----------------------------------------------------------------------------
/*
 * Average mem size/class entry: 6166 bytes [1.4.1, rt.jar], 5764 bytes [1.3.1, rt.jar]
 */
/**
 * @author Vlad Roubtsov, (C) 2003
 */
final class MetaData implements IMetaData, Cloneable
{
    // public: ................................................................
    
    // TODO: MT-safety model
    
    // TODO: no duplicate detection is done here at the moment
    // [may require supporting fast lookup for already added descriptors]
    
    public IMetaData shallowCopy ()
    {
        final MetaData _clone;
        try
        {
            _clone = (MetaData) super.clone ();
        }
        catch (CloneNotSupportedException cnse)
        {
            throw new Error (cnse.toString ());
        }
        
        final HashMap _classMap;
        
        synchronized (lock ())
        {
            _classMap = (HashMap) m_classMap.clone ();
        }
        
        // [m_packagesWarned is not cloned by design]
        
        _clone.m_classMap = _classMap;
        
        return _clone;  
    }
    
    public CoverageOptions getOptions ()
    {
        return m_options;
    }
    
    public int size ()
    {
        return m_classMap.size (); 
    }
    
    public boolean hasSrcFileData ()
    {
        return m_hasSrcFileInfo;
    }
    
    public boolean hasLineNumberData ()
    {
        return m_hasLineNumberInfo;
    }
    
    public Iterator iterator ()
    {
        return m_classMap.values ().iterator ();
    }
    
//    public boolean hasDescriptor (final ClassDescriptor cls)
//    {
//        if ($assert.ENABLED) $assert.ASSERT (cls != null, "cls is null");
//        
//        return m_classes.contains (cls);
//    }

    public boolean hasDescriptor (final String classVMName)
    {
        if ($assert.ENABLED) $assert.ASSERT (classVMName != null, "className is null");
        
        return m_classMap.containsKey (classVMName);
    }
    
    public Object lock ()
    {
        return m_classMap;
    }
        
    public boolean add (final ClassDescriptor cls, final boolean overwrite)
    {
        if ($assert.ENABLED) $assert.ASSERT (cls != null, "cls is null");
        
        final String classVMName = cls.getClassVMName ();
        
        if (overwrite || ! m_classMap.containsKey (classVMName))
        {
            m_classMap.put (classVMName, cls);
            
            boolean incompleteDebugInfo = false;

            if (! cls.hasSrcFileInfo ())
            {
                m_hasSrcFileInfo = false;
                incompleteDebugInfo = true;
            }
            
            if (! cls.hasCompleteLineNumberInfo ())
            {
                m_hasLineNumberInfo = false;
                incompleteDebugInfo = true;
            }
            
            // SF FR 971176: provide user with sample classes that may later
            // caused warnings about line coverage not available
            
            if (incompleteDebugInfo)
            {
                final Logger log = Logger.getLogger ();
                
                if (log.atINFO ())
                {
                    final String packageVMName = cls.getPackageVMName ();
                    
                    if (m_packagesWarned.add (packageVMName))
                    {
                        log.info ("package [" + packageVMName + "] contains classes [" + cls.getName () + "] without full debug info");
                    }
                }
            }
            
            return true;
        }

        return false;
    }
    
    // IMergeable:
    
    public boolean isEmpty ()
    {
        return m_classMap.isEmpty ();
    }
    
    /*
     * note: rhs entries must override current entries
     */
    public IMergeable merge (final IMergeable rhs)
    {
        if ((rhs == null) || rhs.isEmpty () || (rhs == this))
            return this;
        else
        {
            final MetaData rhsmdata = (MetaData) rhs; // TODO: redesign to avoid this cast?
            final Map rhsclasses = rhsmdata.m_classMap;
            
            // rhs entries always override existing content:
            
            for (Iterator entries = rhsclasses.entrySet ().iterator (); entries.hasNext (); )
            {
                final Map.Entry entry = (Map.Entry) entries.next ();
                
                final String classVMName = (String) entry.getKey ();
                final Object rhsdescriptor = entry.getValue ();
                    
                m_classMap.put (classVMName, rhsdescriptor);
            }
            
            // update debug info flags if necessary:
            
            if (! rhsmdata.hasSrcFileData ()) m_hasSrcFileInfo = false;
            if (! rhsmdata.hasLineNumberData ()) m_hasLineNumberInfo = false;
                
            return this;
        }
    }
    
    // protected: .............................................................

    // package: ...............................................................
    
    
    MetaData (final CoverageOptions options)
    {
        if ($assert.ENABLED) $assert.ASSERT (options != null, "options is null");
        m_options = options;
        
        m_hasSrcFileInfo = true;
        m_hasLineNumberInfo = true;
        
        m_classMap = new HashMap ();
        m_packagesWarned = new HashSet ();
    }
    
    
    static MetaData readExternal (final DataInput in)
        throws IOException
    {
        final CoverageOptions options = CoverageOptions.readExternal (in);
        
        final boolean hasSrcFileInfo = in.readBoolean ();
        final boolean hasLineNumberInfo = in.readBoolean ();
        
        final int size = in.readInt ();
        final HashMap classMap = new HashMap (size);
        
        for (int i = 0; i < size; ++ i)
        {
            final String classVMName = in.readUTF ();
            final ClassDescriptor cls = ClassDescriptor.readExternal (in);
            
            classMap.put (classVMName, cls);
        }
        
        // [m_packagesWarned is not part of persisted state]
        
        return new MetaData (options, classMap, hasSrcFileInfo, hasLineNumberInfo);
    }
    
    static void writeExternal (final MetaData mdata, final DataOutput out)
        throws IOException
    {
        CoverageOptions.writeExternal (mdata.m_options, out);
        
        out.writeBoolean (mdata.m_hasSrcFileInfo);
        out.writeBoolean (mdata.m_hasLineNumberInfo);
        
        final Map classMap = mdata.m_classMap;
        
        final int size = classMap.size ();
        out.writeInt (size); // too bad the capacity is not visible
        
        final Iterator entries = classMap.entrySet ().iterator ();
        for (int i = 0; i < size; ++ i)
        {
            final Map.Entry entry = (Map.Entry) entries.next ();
            
            final String classVMName = (String) entry.getKey ();
            final ClassDescriptor cls = (ClassDescriptor) entry.getValue ();
            
            out.writeUTF (classVMName);
            ClassDescriptor.writeExternal (cls, out);
        }
        
        // [m_packagesWarned is not part of persisted state]
    }
    
    // private: ...............................................................
    
    
    private MetaData (final CoverageOptions options, final HashMap classMap,
                      final boolean hasSrcFileInfo, final boolean hasLineNumberInfo)
    {
        if ($assert.ENABLED) $assert.ASSERT (options != null, "options is null");
        m_options = options;
        
        m_hasSrcFileInfo = hasSrcFileInfo;
        m_hasLineNumberInfo = hasLineNumberInfo;
        
        m_classMap = classMap;
    }
    
    
    private final CoverageOptions m_options; // [never null]
    private boolean m_hasSrcFileInfo, m_hasLineNumberInfo;
    private /*final*/ HashMap /* classVMName:String->ClassDescriptor */ m_classMap; // [never null]
    
    private /*final*/ transient HashSet /*  packageVMName:String */ m_packagesWarned; // [never null]

} // end of class
// ----------------------------------------------------------------------------