Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6351851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:05:18+00:00 2026-05-24T22:05:18+00:00

I’d like write a custom type conversion Category in Groovy. The goal is to

  • 0

I’d like write a custom type conversion Category in Groovy. The goal is to assign the values of a Map to the fields of a Groovy bean. In the future there will be different response types. The values of of the Map are always of type String but will have to be converted into a different data type. To make this work I created a Category class that implements a method named asType. This is a simplified example of my code:

class MapCategory {
    static Object asType(Map self, Class clazz) {
        if(clazz == Response) {
            Response response = new Response()

            self.each { key, value ->
                response.setProperty(key, value)
            }

            return response
        }

        DefaultGroovyMethods.asType(self, clazz)
    }
}

class Response {
    String result
    String message
}

This works just fine when when I apply the category using the use keyword.

use(MapCategory) {
    println [result: 'OK', message: 'Success'] as Response
}

However, when I try to use the @Mixin annotation instead it doesn’t seem to work correctly. I get the correct response type but all fields are null.

@Mixin(MapCategory)
class MyClass {
    def printResponse() {
        println [result: 'OK', message: 'Success'] as Response    
    }
}

Does anybody know why it doesn’t work correctly using the annotation?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T22:05:19+00:00Added an answer on May 24, 2026 at 10:05 pm

    Mixins don’t work that way. You are trying to mix in the method for Map into your MyClass object. The mixin would only work if MyClass extended Map.

    Instead, you want to use the use keyword like normal, and just use your category as a category.

    Alternatively, you might not need it at all. Did you know that you can, by default, convert any map into any GroovyBean without extra code? Just use the map-based constructor, like so:

    @groovy.transform.Canonical // Groovy 1.8, just added for automatic toString method
    class Response {
        String result
        String message
    }
    
    println new Response([result: 'OK', message: 'Success'])
    println([result: 'bad', message: 'blah'] as Response)
    

    Notice, automatic Map conversion works both ways. It’s a built-in feature of Groovy.

    Of course, if you need something more complex than just assigning bean properties, this won’t help.

    Note: I’d give you a link, but the Groovy website appears to be broken, and I can’t find code examples. 🙁

    EDIT: Another Suggestion

    Instead of using a Category at all, why don’t you let the bean itself handle it:

    @groovy.transform.Canonical
    class Response {
        String result
        String message
        int num
    
        public void setNum(String num) {
           this.num = Integer.parseInt(num) 
        }
    }
    
    def map = [result: 'OK', message: 'Success', num: '35' ]
    
    println map as Response
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have text I am displaying in SIlverlight that is coming from a CMS
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I have a JSP page retrieving data and when single or double quotes are
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.