View | Details | Raw Unified | Return to bug 248312 | Differences between
and this patch

Collapse All | Expand All

(-)model/org/eclipse/jdt/core/IMemberValuePair.java (-1 / +1 lines)
Lines 125-131 Link Here
125
	 *      analyzed to determine its kind. For example, in <code>@MyAnnot({3.4, 1 + 2.3})</code>,
125
	 *      analyzed to determine its kind. For example, in <code>@MyAnnot({3.4, 1 + 2.3})</code>,
126
	 *      the kind of the second element "1 + 2.3" is unknown.</li>
126
	 *      the kind of the second element "1 + 2.3" is unknown.</li>
127
	 * <li>the value is an array that contains heterogeneous values, e.g.
127
	 * <li>the value is an array that contains heterogeneous values, e.g.
128
	 *      <code>@MyAnnot(1, 2.3, "abc")</code></li>
128
	 *      <code>@MyAnnot({1, 2.3, "abc"})</code></li>
129
	 * </ul>
129
	 * </ul>
130
	 * If the value kind is unknown, the returned value is always either <code>null</code>, or an
130
	 * If the value kind is unknown, the returned value is always either <code>null</code>, or an
131
	 * array containing {@link Object}s and/or <code>null</code>s for unknown elements.
131
	 * array containing {@link Object}s and/or <code>null</code>s for unknown elements.
(-)model/org/eclipse/jdt/internal/core/LocalVariable.java (+13 lines)
Lines 17-29 Link Here
17
import org.eclipse.core.runtime.IProgressMonitor;
17
import org.eclipse.core.runtime.IProgressMonitor;
18
import org.eclipse.jdt.core.*;
18
import org.eclipse.jdt.core.*;
19
import org.eclipse.jdt.core.compiler.CharOperation;
19
import org.eclipse.jdt.core.compiler.CharOperation;
20
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
20
import org.eclipse.jdt.internal.compiler.ast.ArrayInitializer;
21
import org.eclipse.jdt.internal.compiler.ast.ArrayInitializer;
21
import org.eclipse.jdt.internal.compiler.ast.ClassLiteralAccess;
22
import org.eclipse.jdt.internal.compiler.ast.ClassLiteralAccess;
22
import org.eclipse.jdt.internal.compiler.ast.Expression;
23
import org.eclipse.jdt.internal.compiler.ast.Expression;
23
import org.eclipse.jdt.internal.compiler.ast.Literal;
24
import org.eclipse.jdt.internal.compiler.ast.Literal;
24
import org.eclipse.jdt.internal.compiler.ast.NullLiteral;
25
import org.eclipse.jdt.internal.compiler.ast.NullLiteral;
26
import org.eclipse.jdt.internal.compiler.ast.OperatorIds;
25
import org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference;
27
import org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference;
26
import org.eclipse.jdt.internal.compiler.ast.SingleNameReference;
28
import org.eclipse.jdt.internal.compiler.ast.SingleNameReference;
29
import org.eclipse.jdt.internal.compiler.ast.UnaryExpression;
27
import org.eclipse.jdt.internal.compiler.parser.RecoveryScanner;
30
import org.eclipse.jdt.internal.compiler.parser.RecoveryScanner;
28
import org.eclipse.jdt.internal.core.util.MementoTokenizer;
31
import org.eclipse.jdt.internal.core.util.MementoTokenizer;
29
import org.eclipse.jdt.internal.core.util.Util;
32
import org.eclipse.jdt.internal.core.util.Util;
Lines 198-203 Link Here
198
			if (memberValuePair.valueKind == -1)
201
			if (memberValuePair.valueKind == -1)
199
				memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN;
202
				memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN;
200
			return values;
203
			return values;
204
		} else if (expression instanceof UnaryExpression) {			//to deal with negative numerals (see bug - 248312)
205
			UnaryExpression unaryExpression = (UnaryExpression) expression;
206
			if ((unaryExpression.bits & ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT == OperatorIds.MINUS) {
207
				Literal subExpression = (Literal) unaryExpression.expression;
208
				subExpression.computeConstant();
209
				return Util.getNegativeAnnotationMemberValue(memberValuePair, subExpression.constant);
210
			} else {
211
				memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN;
212
				return null;
213
			}
201
		} else {
214
		} else {
202
			memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN;
215
			memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN;
203
			return null;
216
			return null;
(-)model/org/eclipse/jdt/internal/core/CompilationUnitStructureRequestor.java (-1 / +14 lines)
Lines 29-43 Link Here
29
import org.eclipse.jdt.internal.compiler.ast.Literal;
29
import org.eclipse.jdt.internal.compiler.ast.Literal;
30
import org.eclipse.jdt.internal.compiler.ast.MemberValuePair;
30
import org.eclipse.jdt.internal.compiler.ast.MemberValuePair;
31
import org.eclipse.jdt.internal.compiler.ast.NullLiteral;
31
import org.eclipse.jdt.internal.compiler.ast.NullLiteral;
32
import org.eclipse.jdt.internal.compiler.ast.OperatorIds;
32
import org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference;
33
import org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference;
33
import org.eclipse.jdt.internal.compiler.ast.SingleNameReference;
34
import org.eclipse.jdt.internal.compiler.ast.SingleNameReference;
35
import org.eclipse.jdt.internal.compiler.ast.UnaryExpression;
34
import org.eclipse.jdt.internal.compiler.parser.Parser;
36
import org.eclipse.jdt.internal.compiler.parser.Parser;
35
import org.eclipse.jdt.internal.compiler.parser.RecoveryScanner;
37
import org.eclipse.jdt.internal.compiler.parser.RecoveryScanner;
36
import org.eclipse.jdt.internal.compiler.util.HashtableOfObject;
38
import org.eclipse.jdt.internal.compiler.util.HashtableOfObject;
37
import org.eclipse.jdt.internal.compiler.util.HashtableOfObjectToInt;
39
import org.eclipse.jdt.internal.compiler.util.HashtableOfObjectToInt;
38
import org.eclipse.jdt.internal.core.util.ReferenceInfoAdapter;
40
import org.eclipse.jdt.internal.core.util.ReferenceInfoAdapter;
39
import org.eclipse.jdt.internal.core.util.Util;
41
import org.eclipse.jdt.internal.core.util.Util;
40
42
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
41
/**
43
/**
42
 * A requestor for the fuzzy parser, used to compute the children of an ICompilationUnit.
44
 * A requestor for the fuzzy parser, used to compute the children of an ICompilationUnit.
43
 */
45
 */
Lines 719-725 Link Here
719
		if (memberValuePair.valueKind == -1)
721
		if (memberValuePair.valueKind == -1)
720
			memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN;
722
			memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN;
721
		return values;
723
		return values;
724
	} else if (expression instanceof UnaryExpression) {			// to deal with negative numerals (see bug - 248312)
725
		UnaryExpression unaryExpression = (UnaryExpression) expression;
726
		if ((unaryExpression.bits & ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT == OperatorIds.MINUS) {
727
			Literal subExpression = (Literal) unaryExpression.expression;
728
			subExpression.computeConstant();
729
			return Util.getNegativeAnnotationMemberValue(memberValuePair, subExpression.constant);
730
		} else {
731
			memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN;
732
			return null;
733
		}
722
	} else {
734
	} else {
735
		memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN;
723
		return null;
736
		return null;
724
	}
737
	}
725
}
738
}
(-)model/org/eclipse/jdt/internal/core/util/Util.java (+28 lines)
Lines 3160-3165 Link Here
3160
				return null;
3160
				return null;
3161
		}
3161
		}
3162
	}
3162
	}
3163
	
3164
	/*
3165
	 * Creates a member value from the given constant in case of negative numerals,
3166
	 * and sets the valueKind on the given memberValuePair
3167
	 */
3168
	public static Object getNegativeAnnotationMemberValue(MemberValuePair memberValuePair, Constant constant) {
3169
		if (constant == null) {
3170
			memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN;
3171
			return null;
3172
		}
3173
		switch (constant.typeID()) {
3174
			case TypeIds.T_int :
3175
				memberValuePair.valueKind = IMemberValuePair.K_INT;
3176
				return new Integer(constant.intValue() * -1);
3177
			case TypeIds.T_float :
3178
				memberValuePair.valueKind = IMemberValuePair.K_FLOAT;
3179
				return new Float(constant.floatValue() * -1.0f);
3180
			case TypeIds.T_double :
3181
				memberValuePair.valueKind = IMemberValuePair.K_DOUBLE;
3182
				return new Double(constant.doubleValue() * -1.0);
3183
			case TypeIds.T_long :
3184
				memberValuePair.valueKind = IMemberValuePair.K_LONG;
3185
				return new Long(constant.longValue() * -1L);
3186
			default:
3187
				memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN;
3188
				return null;
3189
		}
3190
	}
3163
	/**
3191
	/**
3164
	 * Split signatures of all levels  from a type unique key.
3192
	 * Split signatures of all levels  from a type unique key.
3165
	 *
3193
	 *
(-)src/org/eclipse/jdt/core/tests/model/ClassFileTests.java (+60 lines)
Lines 124-129 Link Here
124
		"  void foo12() {}\n" +
124
		"  void foo12() {}\n" +
125
		"  @MyAnnot(_array={1, 2, 3})\n" +
125
		"  @MyAnnot(_array={1, 2, 3})\n" +
126
		"  void foo13() {}\n" +
126
		"  void foo13() {}\n" +
127
		"  @MyAnnot(_neg_int=-2)\n" +
128
		"  void foo14() {}\n" +
129
		"  @MyAnnot(_neg_float=-2.0f)\n" +
130
		"  void foo15() {}\n" +
131
		"  @MyAnnot(_neg_double=-2.0)\n" +
132
		"  void foo16() {}\n" +
133
		"  @MyAnnot(_neg_long=-2L)\n" +
134
		"  void foo17() {}\n" +
127
		"}\n" +
135
		"}\n" +
128
		"@interface MyAnnot {\n" +
136
		"@interface MyAnnot {\n" +
129
		"  int _int() default 0;\n" +
137
		"  int _int() default 0;\n" +
Lines 139-144 Link Here
139
		"  Class _class() default Object.class;\n" +
147
		"  Class _class() default Object.class;\n" +
140
		"  MyEnum _enum() default MyEnum.FIRST;\n" +
148
		"  MyEnum _enum() default MyEnum.FIRST;\n" +
141
		"  int[] _array() default {};\n" +
149
		"  int[] _array() default {};\n" +
150
		"  int _neg_int() default -1;\n" +
151
		"  float _neg_float() default -1.0f;\n" +
152
		"  double _neg_double() default -1.0;\n" +
153
		"  long _neg_long() default -1L;\n" +
142
		"}\n" +
154
		"}\n" +
143
		"@interface MyOtherAnnot {\n" +
155
		"@interface MyOtherAnnot {\n" +
144
		"}\n" +
156
		"}\n" +
Lines 455-460 Link Here
455
}
467
}
456
468
457
/*
469
/*
470
 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=248312
471
 * Ensures that an annotation with a negative int value is correct
472
 */
473
public void testAnnotations22() throws JavaModelException {
474
	IType type = this.jarRoot.getPackageFragment("annotated").getClassFile("X.class").getType();
475
	IMethod method = type.getMethod("foo14", new String[0]);
476
	assertAnnotationsEqual(
477
		"@annotated.MyAnnot(_neg_int=(int)-2)\n",
478
		method.getAnnotations());
479
}
480
481
/*
482
 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=248312
483
 * Ensures that an annotation with a negative float value is correct
484
 */
485
public void testAnnotations23() throws JavaModelException {
486
	IType type = this.jarRoot.getPackageFragment("annotated").getClassFile("X.class").getType();
487
	IMethod method = type.getMethod("foo15", new String[0]);
488
	assertAnnotationsEqual(
489
		"@annotated.MyAnnot(_neg_float=-2.0f)\n",
490
		method.getAnnotations());
491
}
492
493
/*
494
 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=248312
495
 * Ensures that an annotation with a negative double value is correct
496
 */
497
public void testAnnotations24() throws JavaModelException {
498
	IType type = this.jarRoot.getPackageFragment("annotated").getClassFile("X.class").getType();
499
	IMethod method = type.getMethod("foo16", new String[0]);
500
	assertAnnotationsEqual(
501
		"@annotated.MyAnnot(_neg_double=(double)-2.0)\n",
502
		method.getAnnotations());
503
}
504
505
/*
506
 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=248312
507
 * Ensures that an annotation with a negative long value is correct
508
 */
509
public void testAnnotations25() throws JavaModelException {
510
	IType type = this.jarRoot.getPackageFragment("annotated").getClassFile("X.class").getType();
511
	IMethod method = type.getMethod("foo17", new String[0]);
512
	assertAnnotationsEqual(
513
		"@annotated.MyAnnot(_neg_long=-2L)\n",
514
		method.getAnnotations());
515
}
516
517
/*
458
 * Ensures that no exception is thrown for a .class file name with a dot
518
 * Ensures that no exception is thrown for a .class file name with a dot
459
 * (regression test for bug 114140 assertion failed when opening a class file not not the classpath)
519
 * (regression test for bug 114140 assertion failed when opening a class file not not the classpath)
460
 */
520
 */
(-)src/org/eclipse/jdt/core/tests/model/CompilationUnitTests.java (+114 lines)
Lines 285-290 Link Here
285
}
285
}
286
286
287
/*
287
/*
288
 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=248312
289
 * Ensures that the default value (a negative int) for an annotation method is correct.
290
 */
291
public void testDefaultValue7() throws CoreException {
292
	try {
293
		String cuSource =
294
			"package p;\n" +
295
			"public @interface Y {\n" +
296
			"  public int member() default -1;\n" +
297
			"}";
298
		createFile("/P/src/p/Y.java", cuSource);
299
		IMethod method = getCompilationUnit("/P/src/p/Y.java").getType("Y").getMethod("member", new String[0]);
300
		assertMemberValuePairEquals(
301
			"member=(int)-1",
302
			method.getDefaultValue());
303
	} finally {
304
		deleteFile("/P/src/p/Y.java");
305
	}
306
}
307
308
/*
309
 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=248312
310
 * Ensures that the default value (a negative float) for an annotation method is correct.
311
 */
312
public void testDefaultValue8() throws CoreException {
313
	try {
314
		String cuSource =
315
			"package p;\n" +
316
			"public @interface Y {\n" +
317
			"  public float member() default -1.0f;\n" +
318
			"}";
319
		createFile("/P/src/p/Y.java", cuSource);
320
		IMethod method = getCompilationUnit("/P/src/p/Y.java").getType("Y").getMethod("member", new String[0]);
321
		assertMemberValuePairEquals(
322
			"member=-1.0f",
323
			method.getDefaultValue());
324
	} finally {
325
		deleteFile("/P/src/p/Y.java");
326
	}
327
}
328
329
/*
330
 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=248312
331
 * Ensures that the default value (a negative double) for an annotation method is correct.
332
 */
333
public void testDefaultValue9() throws CoreException {
334
	try {
335
		String cuSource =
336
			"package p;\n" +
337
			"public @interface Y {\n" +
338
			"  public double member() default -1.0;\n" +
339
			"}";
340
		createFile("/P/src/p/Y.java", cuSource);
341
		IMethod method = getCompilationUnit("/P/src/p/Y.java").getType("Y").getMethod("member", new String[0]);
342
		assertMemberValuePairEquals(
343
			"member=(double)-1.0",
344
			method.getDefaultValue());
345
	} finally {
346
		deleteFile("/P/src/p/Y.java");
347
	}
348
}
349
350
/*
351
 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=248312
352
 * Ensures that the default value (a negative long) for an annotation method is correct.
353
 */
354
public void testDefaultValue10() throws CoreException {
355
	try {
356
		String cuSource =
357
			"package p;\n" +
358
			"public @interface Y {\n" +
359
			"  public long member() default -1L;\n" +
360
			"}";
361
		createFile("/P/src/p/Y.java", cuSource);
362
		IMethod method = getCompilationUnit("/P/src/p/Y.java").getType("Y").getMethod("member", new String[0]);
363
		assertMemberValuePairEquals(
364
			"member=-1L",
365
			method.getDefaultValue());
366
	} finally {
367
		deleteFile("/P/src/p/Y.java");
368
	}
369
}
370
371
/*
288
 * Ensure that the deprecated flag is correctly reported
372
 * Ensure that the deprecated flag is correctly reported
289
 * (regression test fo bug 23207 Flags.isDeprecated(IMethod.getFlags()) doesn't work)
373
 * (regression test fo bug 23207 Flags.isDeprecated(IMethod.getFlags()) doesn't work)
290
 */
374
 */
Lines 2290-2294 Link Here
2290
		this.workingCopy.getSource());
2374
		this.workingCopy.getSource());
2291
}
2375
}
2292
2376
2377
/*
2378
 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=248312
2379
 * Ensures that negative values work while annotating local variables.
2380
 */
2381
public void testBug248312() throws CoreException{
2382
	createWorkingCopy(
2383
			"package p;\n" +
2384
			"public @interface Y {\n" +
2385
			"  public int member_int() default -1;\n" +
2386
			"  public float member_float() default -1.0f\n" +
2387
			"  public double member_double=-1.0\n" +
2388
			"  public long member_long=-1L\n" +
2389
			"}\n" +
2390
			"public class Test{\n" +
2391
			"	void testMethod(){\n" +
2392
			"		@Y(member_int=-2) @Y(member_float=-2.0f)\n" +
2393
			"		@Y(member_double=-2.0) @Y(member_long=-2L)\n" +
2394
			"		Object testField1\n" +
2395
			"	}\n" +
2396
			"}"
2397
			);
2398
	ILocalVariable variable1 = selectLocalVariable(this.workingCopy, "testField1");
2399
	IAnnotation[] annotations = variable1.getAnnotations();
2400
	assertAnnotationsEqual(
2401
	"@Y(member_int=(int)-2)\n" +
2402
	"@Y(member_float=-2.0f)\n" +
2403
	"@Y(member_double=(double)-2.0)\n" +
2404
	"@Y(member_long=-2L)\n",
2405
	annotations);
2406
}
2293
2407
2294
}
2408
}

Return to bug 248312