1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g2d.canvas.impl;
14 import java.lang.annotation.ElementType;
15 import java.lang.annotation.Retention;
16 import java.lang.annotation.RetentionPolicy;
17 import java.lang.annotation.Target;
18 import java.lang.reflect.Field;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.List;
23 import org.simantics.g2d.canvas.ICanvasParticipant;
24 import org.simantics.g2d.diagram.handler.DiagramHandler;
27 * @author Toni Kalajainen
29 public class DependencyReflection {
31 private final static ReferenceDefinition[] EMPTY = new ReferenceDefinition[0];
33 @Retention(RetentionPolicy.RUNTIME)
34 @Target(ElementType.FIELD)
35 public static @interface Dependency {}
36 @Retention(RetentionPolicy.RUNTIME)
37 @Target(ElementType.FIELD)
38 public static @interface Reference {}
41 * Scans an object with reflection for all dependencies
46 * ViewportInteractor vi;
48 * @param obj object to scan
49 * @return an array of painters and their priorities
51 public static ReferenceDefinition[] getDependencies(final Object obj, ReferenceType type)
53 List<ReferenceDefinition> result = new ArrayList<ReferenceDefinition>();
54 Class<?> clazz = obj.getClass();
55 _addDependencies(clazz, result, type);
56 //if (result==null) return EMPTY;
57 return result.toArray(EMPTY);
60 private static void _addDependencies(Class<?> clazz, Collection<ReferenceDefinition> result, ReferenceType type)
62 for (final Field f : clazz.getDeclaredFields()) {
63 Class<?> c = f.getType();
64 Dependency dep = (Dependency) f.getAnnotation(Dependency.class);
65 Reference ref = (Reference) f.getAnnotation(Reference.class);
66 if (dep==null && ref==null) continue;
67 ReferenceType referenceType = null;
68 if ( ICanvasParticipant.class.isAssignableFrom( c ) )
69 referenceType = ReferenceType.CanvasParticipant;
70 else if ( DiagramHandler.class.isAssignableFrom( c ) )
71 referenceType = ReferenceType.DiagramHandler;
73 if (referenceType!=type) continue;
75 assert(!(dep!=null && ref!=null));
76 ReferenceDefinition rd = new ReferenceDefinition();
77 rd.dependency = dep!=null;
80 rd.referenceType = referenceType;
82 f.setAccessible(true);
85 Class<?> superClass = clazz.getSuperclass();
87 _addDependencies(superClass, result, type);
90 public final static class ReferenceDefinition {
91 public Class<?> requirement;
93 public boolean dependency;
94 public ReferenceType referenceType;
97 public static enum ReferenceType {
98 CanvasParticipant, DiagramHandler