Parameters must have type annotation kotlin

Kotlin parameters must have type annotation

Question: I want to fetch XML data from API and map it to Kotlin model object by using Retrofit2 + SimpleXML in Kotlin. Solution 1: This is the same problem as: kotlin data class + bean validation jsr 303 You need to use Annotation use-site targets since the default for an annotation on a property is prioritized as: parameter ( if declared in constructor ) property ( if the target site allows, but only Kotlin created annotations can do this ) field ( likely what happened here, which isn’t what you wanted ).

Retrofit2 + SimpleXML in Kotlin: MethodException: Annotation must mark a set or get method

I want to fetch XML data from API and map it to Kotlin model object by using Retrofit2 + SimpleXML in Kotlin.

However, I got such as the following error message from SimpleXML.

org.simpleframework.xml.core.MethodException: Annotation @org.simpleframework.xml.Element(data=false, name=, required=true, type=void) must mark a set or get method

  Success XXXXXXXXXXXXXXXXXXXX 4294967295  

Kotlin model object is below

@Root(name = "response") public class User()

and APIClient is as follows.

interface MyService < @GET("/testLogin.xml") fun getUser(): Call> val retrofit = Retrofit.Builder() .baseUrl(baseURL) .addConverterFactory(SimpleXmlConverterFactory.create()) .build() val call = retrofit.create(MyService::class.java).getUser() call.enqueue(object: Callback < override fun onResponse(p0: Call?, response: Response?) < val response = response?.body() >override fun onFailure(p0: Call?, t: Throwable?)

I got HTTP status code 200 and correct XML data. So I think my declaration of model object is problem.

Читайте также:  Поиск целого числа python

This is the same problem as: kotlin data class + bean validation jsr 303

You need to use Annotation use-site targets since the default for an annotation on a property is prioritized as:

  • parameter ( if declared in constructor )
  • property ( if the target site allows, but only Kotlin created annotations can do this )
  • field ( likely what happened here, which isn’t what you wanted ).

Use get or set target to place the annotation on the getter or setter. Here it is for the getter:

@Root(name = "response") public class User()

See the linked answer for the details.

To avoid the error in parse do one should place annotation tags @set e @get @Root(name = "response", strict = false) public class User()

Not familiar with the SimpleXml library but if it’s annotation processor is looking for specific get and set methods you may want to try setting up your class in the following way:

@Root(name="response") class User()

As well, if the @Element annotation supports field types you could use this approach:

@Root(name="response") class User()

Type of the parameter must be a class annotated with, Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question.Provide details and share your research! But avoid …. Asking for help, clarification, or responding to other answers.

Getting annotation from constructor value parameter using kotlinpoet-metadata

I have data class like this

@Tagme("Response") @TagResponse data class Response( val id: Int, val title: String, val label: String, val images: List, @Input(Parameter::class) val slug: Slug ) 

Using annotation processor, I was able to get Response properties using this approach:

val parentMetadata = (element as TypeElement).toImmutableKmClass() parentMetadata.constructors[0].valueParameters.map < prop ->// this will loop through class properties and return ImmutableKmValueParameter // eg: id, title, label, images, slug. // then, I need to get the annotation that property has here. // eg: @Input(Parameter::class) on slug property. // if prop is slug, this will return true. val isAnnotationAvailable = prop.hasAnnotations // Here I need to get the annotated value // val annotation = [..something..].getAnnotation(Input::class) // How to get the annotation? So I can do this: if ([prop annotation] has [@Input]) < do the something. >> 

Previously I tried to get the annotation like this:

val annotations = prop.type?.annotations 

But, I got empty list even isAnnotationAvailable value is true

Annotations are only stored in metadata if they have nowhere else they can be stored. For parameters, you must read them directly off of the Parameter (reflection) or VariableElement (elements API). This is why we have the ClassInspector API. You almost never want to try to read anything other than basic class data. Anything that’s already contained in the bytecode or elements is basically never duplicated into metadata as well. Treat metadata as added signal, not a wholesale replacement.

An annotation argument must be a compile-time, An annotation argument must be a compile-time constant (1 answer) Closed last month . I wanna send an argument from an override function on my MainActivity to use on an interface to changue an URL everytime that i made click on a drop down list.

Quarkus/Kotlin: An annotation argument must be a compile-time constant with a java class

In a quarkus/kotlin app, I have a rest client that is very basic:

@Path("/my/api/v1") @RestClient interface MyApiClient

Problem is, when a request fails, it returns a response that fails to be mapped. So I want to add an exception mapper, in order to log the real error:

class MyExceptionMapper : ResponseExceptionMapper  < override fun toThrowable(r: Response): java.lang.RuntimeException < Logger.getLogger(MyApiClient::class.java).error(r.status) return RuntimeException("failed") >> 

To do so, I should annoate my client with:

@RegisterProvider(MyExceptionMapper::class.java) 

Doing so, I have a kotlin error:

I googled but could find solutions only for strings. In this case, kotlin expects the java class to be a compile time constant. How to get it?

@RegisterProvider(MyExceptionMapper::class) 

How to Lazy Initialize with a parameter in Kotlin, 1 Answer. You can use any element within the accessible scope, that is constructor parameters, properties, and functions. You can even use other lazy properties, which can be quite useful sometimes. Here are all three variant in a single piece of code. abstract class Class (viewInterface: V) < private val …

An annotation argument must be a compile-time constant acra android

I’m trying to add acra support to my app for sending reports when app crashes. I did class:

@ReportsCrashes(formUri = "", mailTo = "****@gmail.com", mode = ReportingInteractionMode.TOAST, customReportContent = Array(5) < i: Int ->ReportField.ANDROID_VERSION >, resToastText = R.string.crash_toast_text) class AcraReports : Application() < override fun onCreate() < super.onCreate() ACRA.init(this) >> 

but I can’t add this line for defining data which I would like to send:

customReportContent = Array(5) < i: Int ->ReportField.ANDROID_VERSION >, 

At this example this field is defined in such way:

but this code doesn’t work 🙁 What I did wrong and how to add this info to my message?

I don’t know your intention behind Array(5) but this one works and you should be able to elaborate from there:

@ReportsCrashes( formUri = "", formKey = "the key", mailTo = "****@gmail.com", mode = ReportingInteractionMode.TOAST, customReportContent = [ReportField.ANDROID_VERSION], resToastText = R.string.crash_toast_text ) 

Kotlin annotations, Annotations contains the following parameters most frequently and these must be compile-time constants: primitive types (Int,Long etc) strings enumerations class other annotations arrays of the above-mentioned types Applying Annotation – We can apply annotation by putting its name prefixed …

Источник

Оцените статью