Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] [OT]-ish Reading Annotation Data

You need to use reflection, there is no built in "JPA Annotation
Reflection Helper" provided by eclipselink, I asked once in an
eclipselink chat and everyone thought I was nuts, but good to see I'm
not the only one looking for something like that.

Now, I have some classes I developed, but they're too integrated with
other unrelated things to share here, but here are some snippets that
will help you.

private  void readAnnotations(Class pClass, Map<Class,Map<String,
Map<Class<? extends Annotation>, Annotation>>> pPropertyAnnotationMap)
{
		if(!Object.class.equals(pClass)) {
		
			Map<String, Map<Class<? extends Annotation>, Annotation>>
myClassAnnotationMap = new HashMap<String, Map<Class<? extends
Annotation>, Annotation>>();

			Field myFields[]=pClass.getDeclaredFields();
			for(Field myField: myFields) {
				myField.setAccessible(true);
				Annotation myAnnotations[] = myField.getAnnotations();
				Map<Class<? extends Annotation>,Annotation> myAnnotationMap = new
HashMap<Class<? extends Annotation>,Annotation>();
				for(Annotation myAnnotationElement: myAnnotations) {
					myAnnotationMap.put( myAnnotationElement.annotationType(),
myAnnotationElement);

					
				}
				myClassAnnotationMap.put(myField.getName(), myAnnotationMap);
				
			}
			//Get the current map or create a new if it doesn't exist.
			Map<String, Map<Class<? extends Annotation>, Annotation>>
myCurrentClassAnnotationMap = pPropertyAnnotationMap.get(pClass);
			if(myCurrentClassAnnotationMap==null) {
				myCurrentClassAnnotationMap = myClassAnnotationMap;
			} else {
				myCurrentClassAnnotationMap.putAll(myClassAnnotationMap);
			}
			
			//Store the annotations.
			pPropertyAnnotationMap.put(pClass, myCurrentClassAnnotationMap);
			
			//Read the superclass's annotation.
			Class myClass = pClass.getSuperclass();
			if(myClass!=null) {
				readAnnotations(myClass,pPropertyAnnotationMap );
			}
		}
		
	}

@SuppressWarnings("unchecked")
	public Set<Property> findPropertiesWithAnnotation(Class<? extends
Annotation> pAnnotationClass, Class pTargetClass) {
		Set<Property> myResults = new HashSet<Property>();
		Field myFields[] = pTargetClass.getDeclaredFields();
		for(int i=0; i<myFields.length; ++i) {
			myFields[i].setAccessible(true);
			Annotation myAnnotation=myFields[i].getAnnotation(pAnnotationClass);
			if(myAnnotation!=null) {
				//log.info("Located Primary key  = " + myFields[i].getName());
				myResults.add(new Property(myFields[i].getName()));
			}
			
		}
		return myResults;
		
	}


Now per validation, if you're planning on implementing your own
validation framework at the entity level, it'd be prudent to implement
JSR 303 instead of making up your own annotations, this will save you
time in the future when more frameworks support bean validation.

http://jcp.org/en/jsr/detail?id=303



./tch



On Mon, Sep 28, 2009 at 9:16 AM, RogerV <roger.varley@xxxxxxxxxxxxxx> wrote:
>
> Is there a way of retrieving annotation data from an entity? Specifically I'm
> using the @Column(length=) annotation in a Struts 2 web app. It would be
> useful if I could read this data and use it to validate the incoming field
> length and to use it on the result to set the width of the input field.
>
> Regards
> --
> View this message in context: http://www.nabble.com/-OT--ish-Reading-Annotation-Data-tp25645176p25645176.html
> Sent from the EclipseLink - Users mailing list archive at Nabble.com.
>
> _______________________________________________
> eclipselink-users mailing list
> eclipselink-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
>


Back to the top