Skip to main content
summaryrefslogtreecommitdiffstats
blob: e85f805ac0535e6e3a7340c712ebb4aad2a8302a (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
/*******************************************************************************
 * Copyright (c) 2015, 2016 Google, Inc and others.
 * 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:
 *   Stefan Xenos (Google) - Initial implementation
 *******************************************************************************/
package org.eclipse.jdt.internal.core.nd.java;

import java.util.List;

import org.eclipse.jdt.internal.core.nd.Nd;
import org.eclipse.jdt.internal.core.nd.db.IString;
import org.eclipse.jdt.internal.core.nd.field.FieldByte;
import org.eclipse.jdt.internal.core.nd.field.FieldInt;
import org.eclipse.jdt.internal.core.nd.field.FieldLong;
import org.eclipse.jdt.internal.core.nd.field.FieldManyToOne;
import org.eclipse.jdt.internal.core.nd.field.FieldOneToMany;
import org.eclipse.jdt.internal.core.nd.field.FieldOneToOne;
import org.eclipse.jdt.internal.core.nd.field.FieldString;
import org.eclipse.jdt.internal.core.nd.field.StructDef;

public class NdVariable extends NdBinding {
	public static final FieldManyToOne<NdTypeSignature> TYPE;
	public static final FieldInt VARIABLE_ID;
	public static final FieldManyToOne<NdMethod> DECLARING_METHOD;
	public static final FieldManyToOne<NdBinding> PARENT;
	public static final FieldString NAME;
	public static final FieldOneToOne<NdConstant> CONSTANT;
	public static final FieldLong TAG_BITS;
	public static final FieldByte VARIABLE_FLAGS;
	public static final FieldOneToMany<NdAnnotationInVariable> ANNOTATIONS;
	public static final FieldOneToMany<NdTypeAnnotationInVariable> TYPE_ANNOTATIONS;

	@SuppressWarnings("hiding")
	public static StructDef<NdVariable> type;

	public static final byte FLG_GENERIC_SIGNATURE_PRESENT 	= 0x01;

	static {
		type = StructDef.create(NdVariable.class, NdBinding.type);
		TYPE = FieldManyToOne.create(type, NdTypeSignature.VARIABLES_OF_TYPE);
		VARIABLE_ID = type.addInt();
		DECLARING_METHOD = FieldManyToOne.create(type, NdMethod.DECLARED_VARIABLES);
		PARENT = FieldManyToOne.create(type, NdBinding.VARIABLES);
		NAME = type.addString();
		CONSTANT = FieldOneToOne.create(type, NdConstant.class, NdConstant.PARENT_VARIABLE);
		TAG_BITS = type.addLong();
		VARIABLE_FLAGS = type.addByte();
		ANNOTATIONS = FieldOneToMany.create(type, NdAnnotationInVariable.OWNER);
		TYPE_ANNOTATIONS = FieldOneToMany.create(type, NdTypeAnnotationInVariable.OWNER);
		type.done();
	}

	public NdVariable(Nd nd, long bindingRecord) {
		super(nd, bindingRecord);
	}

	public NdVariable(NdBinding parent) {
		super(parent.getNd(), parent.getFile());

		PARENT.put(getNd(), this.address, parent);
	}

	public boolean hasVariableFlag(int toTest) {
		return (VARIABLE_FLAGS.get(getNd(), this.address) & toTest) != 0;
	}

	public void setVariableFlag(byte toSet) {
		int newFlags = VARIABLE_FLAGS.get(getNd(), this.address) | toSet;
		VARIABLE_FLAGS.put(getNd(), this.address, (byte)newFlags);
	}

	public void setName(char[] name) {
		NAME.put(getNd(), this.address, name);
	}

	public IString getName() {
		return NAME.get(getNd(), this.address);
	}

	public void setType(NdTypeSignature typeId) {
		TYPE.put(getNd(), this.address, typeId);
	}

	public void setConstant(NdConstant constant) {
		CONSTANT.put(getNd(), this.address, constant);
	}

	public NdConstant getConstant() {
		return CONSTANT.get(getNd(), this.address);
	}

	public NdTypeSignature getType() {
		return TYPE.get(getNd(), this.address);
	}

	public long getTagBits() {
		return TAG_BITS.get(getNd(), this.address);
	}

	public void setTagBits(long tagBits) {
		TAG_BITS.put(getNd(), this.address, tagBits);
	}

	public List<NdTypeAnnotationInVariable> getTypeAnnotations() {
		return TYPE_ANNOTATIONS.asList(getNd(), this.address);
	}

	public List<NdAnnotationInVariable> getAnnotations() {
		return ANNOTATIONS.asList(getNd(), this.address);
	}

	public String toString() {
		try {
			StringBuilder result = new StringBuilder();
			NdTypeSignature localType = getType();
			if (localType != null) {
				result.append(localType.toString());
				result.append(" "); //$NON-NLS-1$
			}
			IString name = getName();
			if (name != null) {
				result.append(name.toString());
			}
			NdConstant constant = getConstant();
			if (constant != null) {
				result.append(" = "); //$NON-NLS-1$
				result.append(constant.toString());
			}
			return result.toString();
		} catch (RuntimeException e) {
			// This is called most often from the debugger, so we want to return something meaningful even
			// if the code is buggy, the database is corrupt, or we don't have a read lock.
			return super.toString();
		}
	}
}

Back to the top