Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 37937b01854bcc048122b32e31492ae7e5a2da8e (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
/*******************************************************************************
 * Copyright (c) 2004, 2015 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.tools.internal;

import java.io.*;
import java.lang.reflect.*;
import java.util.*;

import org.eclipse.jdt.core.dom.*;

public class ReflectField extends ReflectItem implements JNIField {
	Field field;
	ReflectType type, type64;
	ReflectClass declaringClass;
	
public ReflectField(ReflectClass declaringClass, Field field, String source, CompilationUnit unit) {
	this.declaringClass = declaringClass;
	this.field = field;
	Class<?> clazz = field.getType();
	type = new ReflectType(clazz);
	type64 = type;
	boolean changes = canChange64(clazz);
	if (changes && new File(declaringClass.sourcePath).exists()) {
		TypeDeclaration type1 = (TypeDeclaration)unit.types().get(0);
		Class<?> result = null;
		FieldDeclaration[] fields = type1.getFields();
		for (int i = 0; i < fields.length && result == null; i++) {
			FieldDeclaration node = fields[i];
			for (Iterator<?> iterator = node.fragments().iterator(); iterator.hasNext();) {
				VariableDeclarationFragment decl = (VariableDeclarationFragment) iterator.next();
				if (decl.getName().getIdentifier().equals(field.getName())) {
					String s = source.substring(node.getStartPosition(), node.getStartPosition() + node.getLength());
					if (clazz == int.class && s.contains("int /*long*/")) type64 = new ReflectType(long.class);
					else if (clazz == float.class && s.contains("float /*double*/")) type64 = new ReflectType(double.class);
					else if (clazz == int[].class && (s.contains("int /*long*/") || s.contains("int[] /*long[]*/"))) type64 = new ReflectType(long[].class);
					else if (clazz == float[].class && (s.contains("float /*double*/")|| s.contains("float[] /*double[]*/"))) type = new ReflectType(double[].class);
					else if (clazz == long.class && s.contains("long /*int*/")) type = new ReflectType(int.class);
					else if (clazz == double.class && s.contains("double /*float*/")) type = new ReflectType(float.class);
					else if (clazz == long[].class && (s.contains("long /*int*/")|| s.contains("long[] /*int[]*/"))) type = new ReflectType(int[].class);
					else if (clazz == double[].class && (s.contains("double /*float*/")|| s.contains("double[] /*float[]*/"))) type = new ReflectType(float[].class);
					break;
				}
			}
		}
	}	
}

@Override
public int hashCode() {
	return field.hashCode();
}

@Override
public boolean equals(Object obj) {
	if (!(obj instanceof ReflectField)) return false;
	return ((ReflectField)obj).field.equals(field);
}

@Override
public JNIClass getDeclaringClass() {
	return declaringClass;
}

@Override
public int getModifiers() {
	return field.getModifiers();
}

@Override
public String getName() {
	return field.getName();
}

@Override
public JNIType getType() {
	return type;
}

@Override
public JNIType getType64() {
	return type64;
}

@Override
public String getAccessor() {
	return (String)getParam("accessor");
}

@Override
public String getCast() {
	String cast = ((String)getParam("cast")).trim();
	if (cast.length() > 0) {
		if (!cast.startsWith("(")) cast = "(" + cast;
		if (!cast.endsWith(")")) cast = cast + ")";
	}
	return cast;
}

@Override
public String getExclude() {
	return (String)getParam("exclude");
}

@Override
public String getMetaData() {
	String className = getDeclaringClass().getSimpleName();
	String key = className + "_" + field.getName();
	return declaringClass.metaData.getMetaData(key, "");
}

@Override
public void setAccessor(String str) { 
	setParam("accessor", str);
}

@Override
public void setCast(String str) {
	setParam("cast", str);
}

@Override
public void setExclude(String str) { 
	setParam("exclude", str);
}

@Override
public void setMetaData(String value) {
	String className = declaringClass.getSimpleName();
	String key = className + "_" + field.getName();
	declaringClass.metaData.setMetaData(key, value);
}

@Override
public String toString() {
	return field.toString();
}

}

Back to the top