Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a079156aed4d9dd731aac6d0ad831cd1f9796404 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*****************************************************************************
 * Copyright (c) 2011 Nicolas Deblock & Manuel Giles.
 *
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * 	Nicolas Deblock  nico.deblock@gmail.com  - Initial API and implementation
 * 	Manuel Giles	 giles.manu@live.fr		 - Initial API and implementation
 * 	Cedric Dumoulin  Cedric.dumoulin@lifl.fr - Idea of the java generator project & help for the conception 
 *
 *****************************************************************************/

package org.eclipse.papyrus.java.generator.jdtsynchronizer.impl;



import org.eclipse.jdt.core.Flags;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.papyrus.java.generator.jdtsynchronizer.GeneratorPreference;
import org.eclipse.papyrus.java.generator.metamodel.jdt.jdtmm.JDTField;
import org.eclipse.papyrus.java.generator.metamodel.jdt.jdtmm.JDTMember;
import org.eclipse.papyrus.java.generator.metamodel.jdt.jdtmm.JDTType;

/**
 * gathers all the util function to synchronize with JDT
 * 
 * @author Nicolas Deblock and Manuel Giles
 * 
 */
public class SynchTools {


	/**
	 * Find a IJavaElement in a list of IJavaElement
	 * It's util to search a children of a IJavaElement
	 * 
	 * @param <U>
	 *        a IJavaElement
	 * @param lstElement
	 *        list of IJavaElement
	 * @param elementName
	 *        name of IJavaElement which is search
	 * @return the element who is search, null if there is no element find
	 */
	public static <U extends IJavaElement> U searchIJavaElement(U[] lstElement, String elementName) {
		for(U e : lstElement) {
			if(e.getElementName().equals(elementName))
				return e;
		}
		return null;
	}

	/**
	 * return the visibility of jdtMember :
	 * public or private or protected, abstract, static, final, synchronized
	 * 
	 * @param jdtMember
	 * @return visibility of jdtMember
	 */
	public static String getVisibility(JDTMember jdtMember) {
		StringBuffer buffer = new StringBuffer();

		if(Flags.isPublic(jdtMember.getFlags()))
			buffer.append("public ");
		if(Flags.isPrivate(jdtMember.getFlags()))
			buffer.append("private ");
		if(Flags.isProtected(jdtMember.getFlags()))
			buffer.append("protected ");

		if(Flags.isAbstract(jdtMember.getFlags()))
			buffer.append("abstract ");

		if(Flags.isStatic(jdtMember.getFlags()))
			buffer.append("static ");
		if(Flags.isFinal(jdtMember.getFlags()))
			buffer.append("final ");
		if(Flags.isSynchronized(jdtMember.getFlags()))
			buffer.append("synchronized ");
		return buffer.toString();
	}

	/**
	 * allow to know if a type is primitive
	 * 
	 * @param typename
	 * @return true if typename is a primive type
	 */
	public static boolean isPrimiveType(String typename) {
		if(typename.equals("byte") )
			return true;
		if(typename.equals("char"))
			return true;
		if(typename.equals("double"))
			return true;
		if(typename.equals("float"))
			return true;
		if(typename.equals("int") )
			return true;
		if(typename.equals("long"))
			return true;
		if(typename.equals("short"))
			return true;
		if(typename.equals("boolean"))
			return true;
		if(typename.equals("String"))
			return true;
		return false;
	}

	/**
	 * return the default return of a type (ex: 0 for a int, false for a boolean, null for a class,...)
	 * 
	 * @param typename
	 * @return true if typename is a primive type
	 */
	public static String defaultReturn(String typename) {
		if(typename.equals("byte"))
			return "0";
		if(typename.equals("char"))
			return "0";
		if(typename.equals("double"))
			return "0";
		if(typename.equals("float"))
			return "0";
		if(typename.equals("int"))
			return "0";
		if(typename.equals("long"))
			return "0";
		if(typename.equals("short"))
			return "0";
		if(typename.equals("boolean"))
			return "false";

		return "null";
	}

	/**
	 * return the Class of a primitive type (ex : int => Integer)
	 * 
	 * @param typename
	 * @return true if typename is a primive type
	 */
	private static String getPrimiveTypeClass(String typename) {
		if(typename.equals("byte"))
			return "Byte";
		if(typename.equals("char"))
			return "Character";
		if(typename.equals("double"))
			return "Double";
		if(typename.equals("float"))
			return "Float";
		if(typename.equals("int"))
			return "Integer";
		if(typename.equals("long"))
			return "Long";
		if(typename.equals("short"))
			return "Short";
		if(typename.equals("boolean"))
			return "Boolean";
		return typename;
	}


	public static String getPackageFragment(JDTType type) {
		try {
			if(type.getCompilationUnit() != null) {
				return type.getCompilationUnit().getPackageFragment().getQualifiedName();
			}
			else
				return getPackageFragment(type.getOwner());
		} catch (NullPointerException e) {
			// id null pointer exception, we return null
			return null;
		}
	}

	public static void createImport(IType itype, JDTType currentType, JDTType importType) throws JavaModelException {
		String packageCurrentType = getPackageFragment(currentType);
		String packageImportType = getPackageFragment(importType);

		// if no package to import, stop
		if(packageImportType == null)
			return;


		// if they have the same package, and if it is not a nested class, we stop
		if(packageCurrentType != null && importType.getCompilationUnit() != null && packageCurrentType.equals(packageImportType))
			return;

		String _import = importType.getQualifiedName();
		if(!SynchTools.isPrimiveType(_import) && _import.contains("."))
			itype.getCompilationUnit().createImport(_import, null, null);
	}

	/**
	 * Get the multivalued declaration for the specified typeName.
	 * 
	 * @param type The enclosing type that will contains the declaration. Requested to ajust import.
	 * @param typeName The type for wich a multivalued statement is requested
	 * @param preference The preferences
	 * @return The multivalued statement for the specified typeName.
	 * 
	 * @throws JavaModelException
	 */
	public static String getMultiValued(IType type, String typeName, GeneratorPreference preference) throws JavaModelException {
		// create the import
		String _classQualifiedName = preference.genericClassForMultiValue();
		String _class = _classQualifiedName.substring(_classQualifiedName.lastIndexOf(".") + 1);

		type.getCompilationUnit().createImport(_classQualifiedName, null, null);
		return _class + "<" + getPrimiveTypeClass(typeName) + ">";
	}

	/**
	 * Get the multivalued declaration for the specified typeName.
	 * 
	 * @param type The enclosing type that will contains the declaration. Requested to ajust import.
	 * @param typeName The type for wich a multivalued statement is requested
	 * @param preference The preferences
	 * @return The multivalued statement for the specified typeName.
	 * 
	 * @throws JavaModelException
	 */
	public static String getMultiValued(IType type, JDTType valueType, GeneratorPreference preference) throws JavaModelException {
		// create the import
		String _classQualifiedName = preference.genericClassForMultiValue();
		String _class = _classQualifiedName.substring(_classQualifiedName.lastIndexOf(".") + 1);

		type.getCompilationUnit().createImport(_classQualifiedName, null, null);
		return _class + "<" + getPrimiveTypeClass(valueType.getElementName()) + ">";
	}

}

Back to the top