Error Annotation Value Must Be A Class Literal
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about the value for annotation attribute must be a constant expression Stack Overflow the company Business Learn more about hiring developers or posting ads with
Attribute Value Must Be Constant Butterknife
us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is attribute value must be constant enum a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Annotation attribute must be a class literal? Why? Constants should be fine too attribute value must be constant android up vote 6 down vote favorite 2 Can someone explain why String and Class annotation parameters are expected differently? Why does the compiler require literals for Classes, wherby accepting constants for Strings as well? Working example with Spring's @RequestMapping: public class MyController { public static final String REQUEST_MAPPING = "/index.html"; @RequestMapping(MyController.REQUEST_MAPPING) // ALL OK! ... } WTF example with TestNG's @Test: public class MyControllerTest { public static final Class TEST_EXCEPTION
The Value For Annotation Attribute Requestmapping.value Must Be A Constant Expression
= RuntimeException.class; @Test(expectedExceptions = MyControllerTest.TEST_EXCEPTION) // compilation error, WTF: // The value for annotation attribute Test.expectedExceptions must be a class literal ... } What does work of course is @Test(expectedExceptions = RuntimeException.class). But why? The only difference in the annotation parameter I see is its type: String vs Class. Why on earth would the Java compiler accept String constant as well but accept only class literals? java testng jls share|improve this question edited Oct 21 '14 at 21:09 JasonMArcher 6,22183744 asked Dec 22 '11 at 13:09 sibidiba 3,57942441 add a comment| 3 Answers 3 active oldest votes up vote 4 down vote accepted The Java Language Specification doesn't permit you to use compile-time constants with parameters of type Class. You can only use class literals. The JLS has the following to say about suitable parameter values for annotations: An element type T is commensurate with an element value V if and only if one of the following conditions is true: T is an array type E[] and either: V is an ElementValueArrayInitializer and each ElementValueInitializer (analogous to a variable initializer in an array initializer) in V is commensurate with E. Or V is an ElementValue that is commensurate with T. The type of V is assignment compatible
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the android error element value must be a constant expression company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions java annotation value from properties file Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million
Java Element Value Must Be A Constant Expression
programmers, just like you, helping each other. Join them; it only takes a minute: Sign up In Java why this error: 'attribute value must be constant'? up vote 6 down vote favorite I have some TestNG http://stackoverflow.com/questions/8604316/annotation-attribute-must-be-a-class-literal-why-constants-should-be-fine-too code, where I am passing a Test annotation parameter called timeOut = TESTNG_TEST_TIMEOUT . @Test(description = "Tests something.", groups = { "regression" }, timeOut = TESTNG_TEST_TIMEOUT, enabled = true) And in my TestBase class I have this member: public final static long TESTNG_TEST_TIMEOUT = TimeUnit.MINUTES.toMillis(5); When I use the above line of code, I get a 'attribute value must be constant' error in Eclipse. But, if I simply define the member like so, http://stackoverflow.com/questions/28031784/in-java-why-this-error-attribute-value-must-be-constant it works: public final static long TESTNG_TEST_TIMEOUT = 300000; Is the use of TimeUnit not a constant? java annotations testng constants share|improve this question edited Jan 19 '15 at 19:10 Sotirios Delimanolis 154k25246361 asked Jan 19 '15 at 19:04 djangofan 11.6k32109186 1 But note that you can probably use a static init block to circumvent this problem. –Hot Licks Jan 19 '15 at 19:10 Ahh. Interesting thought. Thanks. –djangofan Jan 19 '15 at 19:38 add a comment| 1 Answer 1 active oldest votes up vote 9 down vote accepted This public final static long TESTNG_TEST_TIMEOUT = 300000; is a constant variable, a type of constant expression. This public final static long TESTNG_TEST_TIMEOUT = TimeUnit.MINUTES.toMillis(5); is not. Annotation members expect constant expressions (and a few other things like enums and Class literals). share|improve this answer answered Jan 19 '15 at 19:05 Sotirios Delimanolis 154k25246361 2 Thanks for the hyperlinks. I can see that the doc says a constant must be a primitive or a predeclared String. Thanks. –djangofan Jan 19 '15 at 19:08 add a comment| Your Answer draft saved draft discarded Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email and Password Post as a guest Name Email Post as a guest Name Email d
page Other source code files at this http://alvinalexander.com/java/jwarehouse/eclipse/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/AnnotationTest.java.shtml package level Click here to learn more about this project The source code /******************************************************************************* * Copyright (c) 2000, 2007 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms must be of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ package org.eclipse.jdt.core.tests.compiler.regression; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Hashtable; import java.util.Map; value must be import junit.framework.Test; import org.eclipse.jdt.core.ToolFactory; import org.eclipse.jdt.core.util.ClassFileBytesDisassembler; import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration; import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader; import org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException; import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; import org.eclipse.jdt.core.tests.util.Util; public class AnnotationTest extends AbstractComparableTest { String reportMissingJavadocComments = null; public AnnotationTest(String name) { super(name); } // Static initializer to specify tests subset using TESTS_* static variables // All specified tests which do not belong to the class are skipped... static { // TESTS_NAMES = new String[] { "test127" }; // TESTS_NUMBERS = new int[] { 249 }; // TESTS_RANGE = new int[] { 249, -1 }; } public static Test suite() { return buildComparableTestSuite(testClass()); } public static Class testClass() { return AnnotationTest.class; } protected Map getCompilerOptions() { Map options = super.getCompilerOptions(); options.put(CompilerOptions.OPTION_DocCommentSupport, CompilerOptions.ENABLED); options.put(CompilerOptions.OPTION_ReportInvalidJavadoc, CompilerOptions.ERROR); options.put(CompilerOptions.OPTION_ReportInvalidJavadocTagsVisibility, CompilerOptions.PRIVATE); options.put(CompilerOptions.OPTION_ReportMissingJavadocTags, CompilerOptions.ERROR); options.put(CompilerOptions.OPTION_ReportMissingJavadocTagsVisibility, CompilerOptions.PRIVATE)