Skip to main content
summaryrefslogtreecommitdiffstats
blob: e343550c8f75b362664524da9233dae10b87eed8 (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
/*******************************************************************************
 * Copyright (c) 2005, 2007 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.jdtutility;

import java.beans.Introspector;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.dom.BodyDeclaration;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.MethodDeclaration;

/**
 * Adapt and extend a jdt method.
 * Attribute based on a Java property, e.g.
 *     private int getFoo() {
 *         return foo;
 *     }
 *     private void setFoo(int foo) {
 *         this.foo = foo;
 *     }
 * 
 * For now we only hold the getter method, since that's where the
 * annotations are put.
 */
public class MethodAttribute extends Attribute {

	public MethodAttribute(IMethod getMethod) {
		super(getMethod);
	}

	public IMethod jdtMethod() {
		return (IMethod) this.getJdtMember();
	}

	MethodDeclaration methodDeclaration() {
		return (MethodDeclaration) this.bodyDeclaration();
	}

	MethodDeclaration methodDeclaration(CompilationUnit astRoot) {
		return (MethodDeclaration) this.bodyDeclaration(astRoot);
	}


	// ********** Member implementation **********

	@Override
	public BodyDeclaration bodyDeclaration(CompilationUnit astRoot) {
		String methodName = this.getName();
		for (MethodDeclaration methodDeclaration : this.declaringTypeDeclaration(astRoot).getMethods()) {
			if (methodDeclaration.getName().getFullyQualifiedName().equals(methodName)
					&& (methodDeclaration.parameters().size() == 0)) {
				return methodDeclaration;
			}
		}
		return null;
	}


	// ********** Attribute implementation **********

	@Override
	public boolean isMethod() {
		return true;
	}

	/**
	 * "foo" returned for a method named "getFoo" or "isFoo"
	 */
	@Override
	public String attributeName() {
		String methodName = super.getName();
		int beginIndex = 0;
		if (methodName.startsWith("get")) {
			beginIndex = 3;
		} else if (methodName.startsWith("is")) {
			beginIndex = 2;
		}
		return Introspector.decapitalize(methodName.substring(beginIndex));
	}

	@Override
	public String typeSignature() {
		try {
			return this.jdtMethod().getReturnType();
		} catch (JavaModelException ex) {
			throw new RuntimeException(ex);
		}
	}

}

Back to the top