Skip to content

Commit

Permalink
Resolving issue w/ enumerations not having metainformation, propogati…
Browse files Browse the repository at this point in the history
…ng all of the metainformatin fixes, making sure that everything has the metainfo it should. This introduces a super super super subtle regression with certain error invariants, I'll fix those with another commit once some of the core descr infrastructure and understanding infra is all resolved.
  • Loading branch information
Tom White committed Mar 12, 2013
1 parent 0b550e9 commit 0014034
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 10 deletions.
75 changes: 66 additions & 9 deletions simplCore/src/simpl/descriptions/EnumerationDescriptor.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package simpl.descriptions;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import ecologylab.generic.ReflectionTools;


import simpl.annotations.dbal.simpl_collection;
import simpl.annotations.dbal.simpl_scalar;
Expand All @@ -17,7 +21,7 @@
import simpl.tools.XMLTools;


public class EnumerationDescriptor implements ISimplStringMarshaller {
public class EnumerationDescriptor implements ISimplStringMarshaller, IMetaInformationProvider {

/**
* Gets an enumeration description for a given class.
Expand All @@ -33,12 +37,13 @@ public static EnumerationDescriptor get(Class<?> enumerationClass) throws SIMPLD

if(isCustomValuedEnum(enumerationClass))
{

// Remember: An enum is a collection of static constants of a type...
Object[] enumerationConstants = enumerationClass.getEnumConstants();

for(Object o : enumerationConstants)
{
String enumEntryName = ((Enum<?>)o).toString();
String enumEntryName = ((Enum<?>)o).name();
Integer enumEntryValue;
try
{
Expand All @@ -63,24 +68,47 @@ public static EnumerationDescriptor get(Class<?> enumerationClass) throws SIMPLD
}
}

ed.getEnumerationEntries().add(new EnumerationEntry(enumEntryName, enumEntryValue));
EnumerationEntry entry = new EnumerationEntry(enumEntryName, enumEntryValue);

addAllMetaInformation(entry, enumerationClass);

ed.getEnumerationEntries().add(entry);
}
}
else
{




// Remember: An enum is a collection of static constants of a type...
Object[] enumerationConstants = enumerationClass.getEnumConstants();

for(Object o : enumerationConstants)
{
String enumEntryName = ((Enum<?>)o).toString();
String enumEntryName = ((Enum<?>)o).name();

EnumerationEntry entry = new EnumerationEntry(enumEntryName);

ed.getEnumerationEntries().add(new EnumerationEntry(enumEntryName));
addAllMetaInformation(entry, enumerationClass);

ed.getEnumerationEntries().add(entry);
}
}

return ed;
}

private static void addAllMetaInformation(EnumerationEntry entry, Class<?> enumerationClass)
{
Field entryField = ReflectionTools.getField(enumerationClass, entry.getName());
AnnotationParser ap = new AnnotationParser();
Collection<MetaInformation> metaInfo = ap.getAllMetaInformation(entryField);
for(MetaInformation mi : metaInfo)
{
entry.addMetaInformation(mi);
}
}

private String tagName;

Expand All @@ -94,8 +122,9 @@ public void setTagName(String tag)
this.tagName = tag;
}

private LinkedList<MetaInformation> metaInfo;

private IMetaInformationProvider metainfo;


private ArrayList<String> otherTags;

/**
Expand All @@ -104,7 +133,7 @@ public void setTagName(String tag)
private void basicInitialization()
{
this.enumerationEntries = new LinkedList<>();
this.metaInfo = new LinkedList<MetaInformation>();
this.metainfo = new MetaInformationCollection();
this.otherTags = new ArrayList<String>();
}

Expand All @@ -131,6 +160,15 @@ private EnumerationDescriptor(Class<?> describedEnum) throws SIMPLDescriptionExc
this.enumerationClass = describedEnum;
this.enumerationName = describedEnum.getName();
this.packageName = describedEnum.getPackage().getName();

// Add all meta information at the enumeration level
AnnotationParser ap = new AnnotationParser();
Collection<MetaInformation> metaInfo = ap.getAllMetaInformation(enumerationClass);

for(MetaInformation imo : metaInfo)
{
this.addMetaInformation(imo);
}

this.tagName = XMLTools.getXmlTagName(describedEnum, "");
if(isCustomValuedEnum(describedEnum))
Expand Down Expand Up @@ -469,11 +507,30 @@ public Object unmarshal(String string) throws SIMPLTranslationException{
throw new SIMPLTranslationException(new SimplIssue("Could not find the string value in the enumeration!", string, null));
}
}

}

public Object getSimpleName() {
// TODO Auto-generated method stub
return this.getEnumerationClass().getSimpleName();
}

@Override
public void addMetaInformation(MetaInformation imo) {
this.metainfo.addMetaInformation(imo);
}

@Override
public Collection<MetaInformation> getMetaInformation() {
return this.metainfo.getMetaInformation();
}

@Override
public boolean containsMetaInformation(String name) {
return this.metainfo.containsMetaInformation(name);
}

@Override
public MetaInformation getMetaInformation(String name) {
return this.metainfo.getMetaInformation(name);
}
}
27 changes: 26 additions & 1 deletion simplCore/src/simpl/descriptions/EnumerationEntry.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package simpl.descriptions;

import java.util.Collection;

import simpl.annotations.dbal.simpl_scalar;

/**
* A class to represent entries in an enumeration
* @author tom
*
*/
public class EnumerationEntry {
public class EnumerationEntry implements IMetaInformationProvider{

/**
* The name of the entry.
Expand Down Expand Up @@ -36,17 +38,40 @@ public void setValue(Integer value) {
*/
@simpl_scalar
private Integer value;
private MetaInformationCollection metainfo;

public EnumerationEntry(){}

public EnumerationEntry(String name)
{
this.name = name;
this.metainfo = new MetaInformationCollection();
}

public EnumerationEntry(String name, Integer value)
{
this.name = name;
this.value = value;
this.metainfo = new MetaInformationCollection();
}

@Override
public void addMetaInformation(MetaInformation imo) {
this.metainfo.addMetaInformation(imo);
}

@Override
public Collection<MetaInformation> getMetaInformation() {
return this.metainfo.getMetaInformation();
}

@Override
public boolean containsMetaInformation(String name) {
return this.metainfo.containsMetaInformation(name);
}

@Override
public MetaInformation getMetaInformation(String name) {
return this.metainfo.getMetaInformation(name);
}
}
35 changes: 35 additions & 0 deletions simplCore/src/simpl/descriptions/MetaInformation.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,45 @@

import java.util.Collection;

/**
* MetaInformation refers to all of the annotations that may exist on a given object;
* for languages that do not have annotations, metainformation simply contains the relevant metainformation correlated to a description.
* Format specific serialization information, etc, is all conveyed as metainformation, retrieved via a MetaInformationProvider. (A fancy dictionary!)
* @author tom
*
*/
public interface MetaInformation {
/**
* Returns the name of the annotation
* @return
*/
String getAnnotationName();
/**
* Gets a collection of parameter descriptiors for this annotation
* @return
*/
Collection<ParameterDescriptor> getParameters();
/**
* Returns true if the parameter exists in the metainformation
* @param parameterName
* @return
*/
Boolean hasParameter(String parameterName);
/**
* Gets the value of the parameter with name given
* @param parameterName
* @return
*/
Object getValueFor(String parameterName);
/**
* Gets the value of the "Value()" for this metainformation.
* @return
*/
Object getValue();

/**
* Adds a parameter descriptor to the metainformation.
* Automaticaly indexes by the name
*/
void addParameter(ParameterDescriptor param);
}
11 changes: 11 additions & 0 deletions simplCore/src/simpl/descriptions/MetaInformationImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,15 @@ public void addParameter(ParameterDescriptor param) {
}
}

@Override
/**
* Gest the value of the Value() for a given annotation;
* this is fairly java specific; If there is a single value for an annotation, it'll be value();
* Accross platforms, this should just return the single value in an annotation, or an error if there are multiple values.
*/
public Object getValue() {
// TODO Auto-generated method stub
return this.getValueFor("value");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package simpl.descriptions.beiber;

import static org.junit.Assert.*;

import org.junit.Test;

import ecologylab.serialization.primaryScenarioEnum;

import simpl.annotations.dbal.simpl_filter;
import simpl.annotations.dbal.simpl_inherit;
import simpl.annotations.dbal.simpl_scalar;
import simpl.annotations.dbal.simpl_tag;
import simpl.descriptions.ClassDescriptor;
import simpl.descriptions.ClassDescriptors;
import simpl.descriptions.EnumerationDescriptor;
import simpl.descriptions.FieldDescriptor;
import simpl.descriptions.IMetaInformationProvider;
import simpl.descriptions.MetaInformation;
import simpl.exceptions.SIMPLDescriptionException;

/**
* The annotation parser is tested in the annotationParserTests...
* this makes sure that the field, class, and enum descriptions all correctly use the annotaiton parser
* and correclty capture relevant annotation information. :D
* @author tom
*
*/
public class FieldsAndClassesObtainAllMetaInformation {


@simpl_inherit
@myRandomAnnotation
class myAnnotatedClass
{
@simpl_scalar
@simpl_tag("another_tag_name")
@myRandomAnnotation
public Integer someField;
}


@Test
public void testClassObtainsAllMetaInformation() {
ClassDescriptor cd = ClassDescriptors.getClassDescriptor(myAnnotatedClass.class);

assertFalse("Should not contain this metadata~", cd.containsMetaInformation("something_that_does_not_exist"));

assertTrue("Should simpl_inherit!", cd.containsMetaInformation("simpl_inherit"));
assertTrue("Should have myRandomAnnotation~",cd.containsMetaInformation("myRandomAnnotation"));

MetaInformation simplInherit = cd.getMetaInformation("simpl_inherit");
assertEquals("simpl_inherit", simplInherit.getAnnotationName());
assertTrue("Should have no parameters!", simplInherit.getParameters().isEmpty());

MetaInformation myRandomAnnotation = cd.getMetaInformation("myRandomAnnotation");
assertEquals("myRandomAnnotation", myRandomAnnotation.getAnnotationName());
assertTrue("Should have no parameters!", myRandomAnnotation.getParameters().isEmpty());


FieldDescriptor fd = cd.allFieldDescriptors().get(0);
assertTrue(fd.containsMetaInformation("simpl_scalar"));
assertTrue(fd.containsMetaInformation("myRandomAnnotation"));

assertTrue(fd.containsMetaInformation("simpl_tag"));

MetaInformation simpl_tag = fd.getMetaInformation("simpl_tag");
assertTrue(simpl_tag.hasParameter("value"));
assertEquals("another_tag_name", simpl_tag.getValueFor("value"));
assertEquals("another_tag_name", simpl_tag.getValue());
}


@Test
public void testEnumerationDescriptionObtainsAllMetaInformation() throws SIMPLDescriptionException
{
EnumerationDescriptor ed = EnumerationDescriptor.get(annotatedEnumeration.class);
assertTrue(ed.containsMetaInformation("myRandomAnnotation"));
assertTrue(ed.containsMetaInformation("simpl_tag"));
assertEquals("my_spiffy_tag",ed.getMetaInformation("simpl_tag").getValue());

assertTrue(ed.getEnumerationEntries().get(0).containsMetaInformation("myRandomAnnotation"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package simpl.descriptions.beiber;

import simpl.annotations.dbal.simpl_scalar;
import simpl.annotations.dbal.simpl_tag;

@myRandomAnnotation
@simpl_tag("my_spiffy_tag")
public enum annotatedEnumeration {

@myRandomAnnotation
firstValue(3),
secondValue(5),
thirdValue(7);

@simpl_scalar
@myRandomAnnotation
private Integer myValue;

private annotatedEnumeration(Integer aValue)
{
this.myValue = aValue;
}
}
Loading

0 comments on commit 0014034

Please sign in to comment.