View Javadoc
1   package de.fhdw.wtf.walker.tasks;
2   
3   import java.util.HashSet;
4   import java.util.Set;
5   
6   import de.fhdw.wtf.common.ast.Attribute;
7   import de.fhdw.wtf.common.ast.ConstructorOrOperation;
8   import de.fhdw.wtf.common.ast.Group;
9   import de.fhdw.wtf.common.ast.Model;
10  import de.fhdw.wtf.common.ast.type.AtomicType;
11  import de.fhdw.wtf.common.ast.type.BaseType;
12  import de.fhdw.wtf.common.ast.type.ClassType;
13  import de.fhdw.wtf.common.ast.type.CompositeType;
14  import de.fhdw.wtf.common.ast.type.Type;
15  import de.fhdw.wtf.common.ast.type.TypeProxy;
16  import de.fhdw.wtf.common.ast.visitor.AtomicTypeVisitorException;
17  import de.fhdw.wtf.common.ast.visitor.TypeVisitorException;
18  import de.fhdw.wtf.common.exception.ast.ASTException;
19  import de.fhdw.wtf.common.exception.walker.DoubleAttributenameException;
20  import de.fhdw.wtf.common.exception.walker.TaskException;
21  import de.fhdw.wtf.common.task.TaskExecutor;
22  import de.fhdw.wtf.walker.walker.HelperUtils;
23  import de.fhdw.wtf.walker.walker.SimpleWalkerTask;
24  
25  /**
26   * This Task check the Attributenames in every Class and the superclasses. If there are duplicate Name of Attributes,
27   * this Checker will thrown an {@link DoubleAttributenameException}.
28   * 
29   * This Checker must after the {@link CyclicInheritanceCheck} and the {@link BaseTypeInheritanceCheck}.
30   */
31  public final class DoubleAttributenameCheck extends SimpleWalkerTask {
32  	
33  	/**
34  	 * Constructor of {@link DoubleAttributenameCheck}.
35  	 * 
36  	 * @param m
37  	 *            model
38  	 * @param taskmanager
39  	 *            taskmanager
40  	 */
41  	private DoubleAttributenameCheck(final Model m, final TaskExecutor taskmanager) {
42  		super(m, taskmanager);
43  	}
44  	
45  	/**
46  	 * Creates a {@link DoubleAttributenameCheck}-Object.
47  	 * 
48  	 * @param model
49  	 *            model
50  	 * @param taskmanager
51  	 *            taskmanager
52  	 * @return The {@link DoubleAttributenameCheck}-Object.
53  	 */
54  	public static DoubleAttributenameCheck create(final Model model, final TaskExecutor taskmanager) {
55  		return new DoubleAttributenameCheck(model, taskmanager);
56  	}
57  	
58  	@Override
59  	public void handleClass(final ClassType c) throws TaskException {
60  		this.checkAttributeNames(c);
61  	}
62  	
63  	@Override
64  	public void handleGroup(final Group g) throws TaskException {
65  		// Nothing
66  	}
67  	
68  	@Override
69  	public void handleAttribute(final Attribute a, final ClassType owner) throws TaskException {
70  		// Nothing
71  	}
72  	
73  	/**
74  	 * Check the attribute-Names of the class and the superclasses for double attributenames.
75  	 * 
76  	 * @param clss
77  	 *            clss
78  	 * @throws DoubleAttributenameException
79  	 *             DoubleAttributenameException
80  	 */
81  	private void checkAttributeNames(final ClassType clss) throws DoubleAttributenameException {
82  		this.checkAttributeNames(clss, new HashSet<String>());
83  	}
84  	
85  	/**
86  	 * Check the attribute-Names of the class and the superclasses for double attributenames.
87  	 * 
88  	 * @param clss
89  	 *            clss
90  	 * @param attrNamen
91  	 *            already found attributenames
92  	 * @throws DoubleAttributenameException
93  	 *             DoubleAttributenameException
94  	 */
95  	void checkAttributeNames(final ClassType clss, final Set<String> attrNamen) throws DoubleAttributenameException {
96  		for (final Attribute currentAttr : clss.getAttributes()) {
97  			if (!attrNamen.add(currentAttr.getName())) {
98  				throw DoubleAttributenameException.create(currentAttr);
99  			}
100 		}
101 		
102 		/*for (final Type currentSupertype : clss.getSuperTypes()) {
103 			currentSupertype.accept(new TypeVisitorException<DoubleAttributenameException>() {
104 				
105 				@Override
106 				public void handle(final TypeProxy typeProxy) throws DoubleAttributenameException {
107 					final Type targetType = HelperUtils.getTargetType(typeProxy);
108 					targetType.accept(new TypeVisitorException<DoubleAttributenameException>() {
109 						
110 						@Override
111 						public void handle(final TypeProxy tP) {
112 							throw new ASTException("It could not be a typeProxy in a typeProxy");
113 						}
114 						
115 						@Override
116 						public void handle(final CompositeType compositeType) {
117 							// Nothing
118 						}
119 						
120 						@Override
121 						public void handle(final AtomicType atomicType) throws DoubleAttributenameException {
122 							atomicType.accept(new AtomicTypeVisitorException<DoubleAttributenameException>() {
123 								
124 								@Override
125 								public void handle(final ClassType clazz) throws DoubleAttributenameException {
126 									DoubleAttributenameCheck.this.checkAttributeNames(clazz, attrNamen);
127 								}
128 								
129 								@Override
130 								public void handle(final BaseType baseType) {
131 									// Nothing
132 								}
133 							});
134 						}
135 					});
136 				}
137 				
138 				@Override
139 				public void handle(final CompositeType compositeType) {
140 					// Nothing
141 				}
142 				
143 				@Override
144 				public void handle(final AtomicType atomicType) {
145 					throw new ASTException("It´s could not be an atomic type direct");
146 				}
147 			});
148 		}*/
149 	}
150 	
151 	@Override
152 	public void finalizeTask() throws TaskException {
153 		// Nothing
154 	}
155 	
156 	@Override
157 	public void beginTask() throws TaskException {
158 		// Nothing to do here
159 	}
160 	
161 	@Override
162 	public void handleConstructorOrOperation(final ConstructorOrOperation coo, final ClassType owner)
163 			throws TaskException {
164 		// Nothing to do here
165 		
166 	}
167 }