Changeset 18:102cc7e44ba3 in rrlib_serialization-java


Ignore:
Timestamp:
11.08.2014 19:05:01 (5 years ago)
Author:
Max Reichardt <mreichardt@…>
Branch:
default
Phase:
public
Message:

Removed setName() from DataTypeBase to remove concurrency issues. Instead, name needs to be specified in constructor. Added Immutable type trait.

Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • Serialization.java

    r14 r18  
    3737import org.rrlib.serialization.rtti.Factory; 
    3838import org.rrlib.serialization.rtti.GenericObject; 
     39import org.rrlib.serialization.rtti.Immutable; 
    3940import org.rrlib.xml.XMLNode; 
    4041 
     
    344345        } 
    345346        Class<?> type = src.getClass(); 
    346         boolean immutable = type.isPrimitive() || Number.class.isAssignableFrom(type) || Boolean.class.equals(type) || String.class.equals(type); 
     347        boolean immutable = type.isPrimitive() || Number.class.isAssignableFrom(type) || Boolean.class.equals(type) || String.class.equals(type) || Immutable.class.isAssignableFrom(type); 
    347348        immutable &= (!Copyable.class.isAssignableFrom(type)); 
    348349        if (immutable) { 
  • rtti/DataType.java

    r17 r18  
    4747    @SuppressWarnings("rawtypes") 
    4848    public DataType(Class<?> javaClass, String name, boolean createListTypes) { 
    49         setName(name != null ? name : javaClass.getSimpleName()); 
     49        super(name != null ? name : javaClass.getSimpleName()); 
    5050        type = Classification.PLAIN; 
    5151        this.javaClass = javaClass; 
     
    7878     */ 
    7979    private DataType(DataTypeBase e, Classification type) { 
     80        super(type == Classification.LIST ? ("List<" + e.getName() + ">") : ("List<" + e.getName() + "*>")); 
    8081        this.type = type; 
    8182        this.elementType = e; 
    8283        this.typeTraits = (byte)(e.typeTraits & (IS_BINARY_SERIALIZABLE | IS_STRING_SERIALIZABLE | IS_XML_SERIALIZABLE)); 
    83         if (type == Classification.LIST) { 
    84             setName("List<" + e.getName() + ">"); 
    85         } else if (type == Classification.PTR_LIST) { 
    86             setName("List<" + e.getName() + "*>"); 
    87         } else { 
    88             throw new RuntimeException("Unsupported"); 
    89         } 
    9084    } 
    9185 
  • rtti/DataTypeBase.java

    r17 r18  
    3636 * 
    3737 * Assigns unique type-id to each data type. 
    38  * Can be used as factory for data types (necessary for deserializing) 
     38 * Can be used as factory for data types (necessary for deserializing 
    3939 * vectors containing pointers). 
    4040 * 
     
    4242 * this mechanism should work with. 
    4343 * 
    44  * This class is passed by value 
     44 * To avoid that the list of types is modified while accessing it, 
     45 * synchronize on 'DataTypeBase.class'. 
    4546 */ 
    4647public class DataTypeBase { 
     
    7576 
    7677    /** Name of data type */ 
    77     private String name; 
    78  
    79     /** Is this the default name? - then it may be changed */ 
    80     protected boolean defaultName = true; 
     78    private final String name; 
    8179 
    8280    /** Data type uid */ 
     
    116114    //protected boolean remoteType = false; 
    117115 
    118     public DataTypeBase() { 
    119         synchronized (types) { 
     116    public DataTypeBase(String name) { 
     117        synchronized (DataTypeBase.class) { 
    120118            uid = (short)types.size(); 
     119            this.name = name; 
     120            for (DataTypeBase type : types) { 
     121                if (type.getName().equals(name)) { 
     122                    Log.log(LogLevel.WARNING, "Two types with the same name were registered: " + name); 
     123                } 
     124            } 
    121125            if (types.size() >= MAX_TYPES) { 
    122126                Log.log(LogLevel.ERROR, this, "Maximum number of data types exceeded. Increase cMAX_TYPES."); 
    123127                throw new RuntimeException("Maximum number of data types exceeded. Increase MAX_TYPES."); 
    124128            } 
     129 
    125130            types.add(this); 
    126131            Log.log(LogLevel.DEBUG_VERBOSE_1, this, "Adding data type " + getName()); 
     
    338343        ann.annotatedType = this; 
    339344        int annIndex = -1; 
    340         synchronized (types) { 
     345        synchronized (DataTypeBase.class) { 
    341346            Integer i = annotationIndexLookup.get(ann.getClass()); 
    342347            if (i == null) { 
     
    374379        return enumConstants; 
    375380    } 
    376  
    377     /** 
    378      * Set name of data type 
    379      * (only valid if still default == not set before) 
    380      * 
    381      * @param newName New name of type 
    382      */ 
    383     protected void setName(String newName) { 
    384         if (!defaultName) { 
    385             assert(name.equals(newName)) : "Name already set"; 
    386             return; 
    387         } 
    388         defaultName = false; 
    389         name = newName; 
    390  
    391         for (int i = 0; i < types.size(); i++) { 
    392             if (i != uid && types.get(i).getName().equals(newName)) { 
    393                 Log.log(LogLevel.WARNING, "Two types with the same name were registered: " + newName); 
    394             } 
    395         } 
    396     } 
    397381} 
Note: See TracChangeset for help on using the changeset viewer.