Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bafc168000a37965842fd708c45eabde113c0c8e (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
/*******************************************************************************
 * Copyright (c) 2011, 2012 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.jpa.core.internal.context.java;

import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jpt.common.core.resource.java.JavaResourceField;
import org.eclipse.jpt.common.core.resource.java.JavaResourceMethod;
import org.eclipse.jpt.common.utility.MethodSignature;
import org.eclipse.jpt.common.utility.internal.ArrayTools;
import org.eclipse.jpt.common.utility.internal.iterables.ListIterable;
import org.eclipse.jpt.jpa.core.context.AccessType;
import org.eclipse.jpt.jpa.core.context.PersistentAttribute;
import org.eclipse.jpt.jpa.core.context.PersistentType;
import org.eclipse.jpt.jpa.core.context.ReadOnlyPersistentAttribute;
import org.eclipse.jpt.jpa.core.context.java.JavaElementReference;
import org.eclipse.jpt.jpa.core.context.java.JavaPersistentAttribute;
import org.eclipse.jpt.jpa.core.internal.context.JptValidator;
import org.eclipse.jpt.jpa.core.internal.jpa1.context.PersistentPropertyValidator;

public class PropertyAccessor
	extends AbstractAccessor
{
	private final JavaResourceMethod resourceGetter;

	private final JavaResourceMethod resourceSetter;


	public PropertyAccessor(ReadOnlyPersistentAttribute parent, JavaResourceMethod resourceGetter, JavaResourceMethod resourceSetter) {
		super(parent);
		this.resourceGetter = resourceGetter;
		this.resourceSetter = resourceSetter;
	}

	public JavaResourceMethod getResourceAttribute() {
		return (this.resourceGetter != null) ? this.resourceGetter : this.resourceSetter;
	}

	public boolean isFor(JavaResourceField field) {
		return false;
	}

	public boolean isFor(JavaResourceMethod getterMethod, JavaResourceMethod setterMethod) {
		return (this.resourceGetter == getterMethod) && (this.resourceSetter == setterMethod);
	}

	public AccessType getDefaultAccess() {
		return AccessType.PROPERTY;
	}

	public JavaResourceMethod getResourceGetter() {
		return this.resourceGetter;
	}

	public JavaResourceMethod getResourceSetter() {
		return this.resourceSetter;
	}

	public JavaPersistentAttribute buildUnannotatedJavaAttribute(PersistentType type) {
		return this.buildJavaAttribute(type, this.buildUnannotatedJavaResourceGetter(), this.buildUnannotatedJavaResourceSetter());
	}

	/**
	 * Build a Java resource method that wraps the original Java resource
	 * getter method and behaves as though it has no annotations. This will cause
	 * all the settings in the Java <em>context</em> attribute to default.
	 */
	protected JavaResourceMethod buildUnannotatedJavaResourceGetter() {
		return new UnannotatedJavaResourceMethod(this.resourceGetter);
	}

	/**
	 * Build a Java resource method that wraps the original Java resource
	 * setter method and behaves as though it has no annotations. This will cause
	 * all the settings in the Java <em>context</em> attribute to default.
	 */
	protected JavaResourceMethod buildUnannotatedJavaResourceSetter() {
		return new UnannotatedJavaResourceMethod(this.resourceSetter);
	}

	protected JavaPersistentAttribute buildJavaAttribute(PersistentType type, JavaResourceMethod getterMethod, JavaResourceMethod setterMethod) {
		return this.getJpaFactory().buildJavaPersistentProperty(type, getterMethod, setterMethod);
	}

	public JptValidator buildAttributeValidator(PersistentAttribute persistentAttribute) {
		return new PersistentPropertyValidator(persistentAttribute, this);
	}

	public IJavaElement getJavaElement() {
		PersistentType persistentType = this.getAttribute().getOwningPersistentType();
		if (persistentType instanceof JavaElementReference) {
			IType jdtType = (IType) ((JavaElementReference) persistentType).getJavaElement();
			if (jdtType != null) {
				JavaResourceMethod method = this.getResourceAttribute();
				if (method != null) {
					String[] parmTypeNames = ArrayTools.array(method.getParameterTypeNames(), new String[method.getParametersSize()]);
					return jdtType.getMethod(method.getMethodName(), parmTypeNames);
				}
			}
		}
		return null;
	}


	// ********** unannotated Java resource method **********

	/**
	 * Wrap another Java resource method and suppress all its annotations.
	 */
	protected class UnannotatedJavaResourceMethod
		extends UnannotatedJavaResourceAttribute<JavaResourceMethod>
		implements JavaResourceMethod
	{
		protected UnannotatedJavaResourceMethod(JavaResourceMethod method){
			super(method);
		}

		public Kind getKind() {
			return Kind.METHOD;
		}

		public String getMethodName() {
			return this.member.getMethodName();
		}

		public ListIterable<String> getParameterTypeNames() {
			return this.member.getParameterTypeNames();
		}

		public String getParameterTypeName(int index) {
			return this.member.getParameterTypeName(index);
		}

		public int getParametersSize() {
			return this.member.getParametersSize();
		}

		public boolean isConstructor() {
			return this.member.isConstructor();
		}

		public boolean isFor(MethodSignature methodSignature, int occurrence) {
			return this.member.isFor(methodSignature, occurrence);
		}

		public void synchronizeWith(MethodDeclaration methodDeclaration) {
			// NOP
		}
	}
}

Back to the top