]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/canvas/impl/DependencyReflection.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / canvas / impl / DependencyReflection.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.g2d.canvas.impl;
13
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;
22
23 import org.simantics.g2d.canvas.ICanvasParticipant;
24 import org.simantics.g2d.diagram.handler.DiagramHandler;
25
26 /**
27  * @author Toni Kalajainen
28  */
29 public class DependencyReflection {
30
31         private final static ReferenceDefinition[] EMPTY = new ReferenceDefinition[0]; 
32         
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 {}
39         
40         /**
41          * Scans an object with reflection for all dependencies 
42          * <p>
43          * Example:
44          * 
45          * @Dependency
46          * ViewportInteractor vi;
47          * 
48          * @param obj object to scan
49          * @return an array of painters and their priorities
50          */
51         public static ReferenceDefinition[] getDependencies(final Object obj, ReferenceType type)
52         {
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);
58         }
59
60     private static void _addDependencies(Class<?> clazz, Collection<ReferenceDefinition> result, ReferenceType type)
61         {
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;
72                         else assert(false);
73                         if (referenceType!=type) continue;
74                         
75                         assert(!(dep!=null && ref!=null));
76                         ReferenceDefinition rd = new ReferenceDefinition();
77                         rd.dependency = dep!=null;
78                         rd.field = f;
79                         rd.requirement = c;
80                         rd.referenceType = referenceType;
81                         
82                         f.setAccessible(true);
83                         result.add(rd);
84                 }
85                 Class<?> superClass = clazz.getSuperclass();
86                 if (superClass!=null)
87                         _addDependencies(superClass, result, type);
88         }
89         
90         public final static class ReferenceDefinition {
91                 public Class<?> requirement;
92                 public Field field;
93                 public boolean dependency;
94                 public ReferenceType referenceType; 
95         }       
96         
97         public static enum ReferenceType {
98                 CanvasParticipant, DiagramHandler
99         }
100         
101 }