Skip to main content
summaryrefslogtreecommitdiffstats
blob: e9df14d3d82fd3046405a8a5a4299eccdea4a8f8 (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
/*******************************************************************************
 * Copyright (c) 2005, 2008 Oracle. 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:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.core.internal.utility.jdt;

import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.IVariableBinding;
import org.eclipse.jdt.core.dom.Modifier;

public class JPTTools {

	/**
	 * Return whether the specified field may be "persisted".
	 * According to the spec, "All non-transient instance variables that are not 
	 * annotated with the Transient annotation are persistent."
	 */
	public static boolean fieldIsPersistable(IVariableBinding field) {
		int modifiers = field.getModifiers();
		if (Modifier.isStatic(modifiers)) {
			return false;
		}
		if (Modifier.isTransient(modifiers)) {
			return false;
		}
		return true;
	}

	/**
	 * Return whether the specified method is a "getter" method that
	 * represents a property that may be "persisted".
	 */
	public static boolean methodIsPersistablePropertyGetter(IMethodBinding methodBinding) {
		if (methodHasBadModifiers(methodBinding)) {
			return false;
		}

		ITypeBinding returnType = methodBinding.getReturnType();
		if (returnType == null) {
			return false;
		}
		String returnTypeName = returnType.getQualifiedName();
		if (returnTypeName.equals("void")) { //$NON-NLS-1$
			return false;
		}
		if (methodBinding.getParameterTypes().length != 0) {
			return false;
		}

		String methodName = methodBinding.getName();
		int beginIndex = 0;
		boolean booleanGetter = false;
		if (methodName.startsWith("is")) { //$NON-NLS-1$
			if (returnTypeName.equals("boolean")) { //$NON-NLS-1$
				beginIndex = 2;
			} else {
				return false;
			}
		} else {
			if (methodName.startsWith("get")) { //$NON-NLS-1$
				beginIndex = 3;
				if (returnTypeName.equals("boolean")) { //$NON-NLS-1$
					booleanGetter = true;
				}
			} else {
				return false;
			}
		}

		String capitalizedAttributeName = methodName.substring(beginIndex);
		// if the type has both methods:
		//     boolean isProperty()
		//     boolean getProperty()
		// then #isProperty() takes precedence and we ignore #getProperty()
		// (see the JavaBeans spec 1.01)
		if (booleanGetter) {
			IMethodBinding isMethod = methodBindingNoParameters(methodBinding.getDeclaringClass(), "is" + capitalizedAttributeName); //$NON-NLS-1$
			if (isMethod == null) {
				return false;
			}
			if (isMethod.getReturnType().getName().equals("boolean")) { //$NON-NLS-1$
				return false;
			}
		}
		IMethodBinding setMethod = methodBindingOneParameter(methodBinding.getDeclaringClass(), "set" + capitalizedAttributeName, returnTypeName); //$NON-NLS-1$
		if (setMethod == null) {
			return false;
		}
		if (methodHasBadModifiers(setMethod)) {
			return false;
		}
		if ( ! setMethod.getReturnType().getName().equals("void")) { //$NON-NLS-1$
			return false;
		}
		return true;
	}
	
	private static IMethodBinding methodBindingNoParameters(ITypeBinding typeBinding, String methodName) {
		for (IMethodBinding method : typeBinding.getDeclaredMethods()) {
			if (method.getName().equals(methodName)) {
				if (method.getParameterTypes().length == 0) {
					return method;
				}
			}
		}
		return null;
	}
	
	private static IMethodBinding methodBindingOneParameter(ITypeBinding typeBinding, String methodName, String parameterTypeName) {
		for (IMethodBinding method : typeBinding.getDeclaredMethods()) {
			if (method.getName().equals(methodName)) {
				if (method.getParameterTypes().length == 1) {
					if (method.getParameterTypes()[0].getQualifiedName().equals(parameterTypeName)) {
						return method;
					}
				}
			}
		}
		return null;
	}

	/**
	 * Return whether the specified method's modifiers prevent it
	 * from being a getter or setter for a "persistent" property.
	 */
	private static boolean methodHasBadModifiers(IMethodBinding methodBinding) {
		if (methodBinding.isConstructor()) {
			return true;
		}
		int modifiers = methodBinding.getModifiers();
		if (Modifier.isStatic(modifiers)) {
			return true;
		}
		if (Modifier.isFinal(modifiers)) {
			return true;
		}
		if ( ! (Modifier.isPublic(modifiers) || Modifier.isProtected(modifiers))) {
			return true;
		}
		return false;
	}
	
	/**
	 * Return whether the type may be "persisted", ie marked as Entity, MappedSuperclass, Embeddable
	 */
	//TODO should persistability be dependent on having a no-arg constructor or should that just be validation?
	//seems like final, or a member type being static could be dealt with through validation instead of filtering them out.
	public static boolean typeIsPersistable(ITypeBinding typeBinding) {
		if (typeBinding.isInterface()) {
			return false;
		}
		if (typeBinding.isAnnotation()) {
			return false;
		}
		if (typeBinding.isEnum()) {
			return false;
		}
		if (typeBinding.isLocal()) {
			return false;
		}
		if (typeBinding.isAnonymous()) {
			return false;
		}
		int modifiers = typeBinding.getModifiers();
		if (Modifier.isFinal(modifiers)) {
			return false;
		}
		if (typeBinding.isMember()) {
			if (!Modifier.isStatic(modifiers)) {
				return false;
			}
		}
		return true;
	}

}

Back to the top