Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-7443

instantiating a class withTraits does not use the classloader of the trait

    XMLWordPrintableJSON

Details

    Description

      this fails:
      def aWithB = new ClassA().withTraits(traitB)
      when traitB is not from the classloader of class A.

      full example code:

      package groovy.lang.traits
      
      import org.codehaus.groovy.control.CompilerConfiguration;
      
      class GroovyTraitsClassloaderTest {
      
      	static class ClassA {
      		
      	}
      	
      	static trait TraitA {
      		
      		def aMethod() {
      			println "traitA method"
      		}
      		
      	}
      	
      	static main(args) {
      	
      		
      		def aWithA = new ClassA().withTraits(TraitA)
      		
      		aWithA.aMethod()
      		
      		
      		GroovyClassLoader gcl = new GroovyClassLoader(Thread.currentThread().getContextClassLoader());
      		
      		Class classB = gcl.parseClass("""\
      class ClassB {}
      """)
      		
      		Class traitB = gcl.parseClass("""\
      trait TraitB {
      	
      	def bMethod() {
      		println "traitB method"
      	}
      	
      }
      """)
      		
      		//ok
      		def bWithA = classB.newInstance().withTraits(TraitA)
      
      		//ok
      		def bWithB = classB.newInstance().withTraits(traitB)
      		bWithB.bMethod()
      		
      		//fails
      		def aWithB = new ClassA().withTraits(traitB)
      		
      		aWithB.bMethod()
      		
      		
      	}
      
      }
      

      Attachments

        1. GroovyTraitsClassloaderTest.groovy
          0.9 kB
          Marc Hadfield

        Issue Links

          Activity

            People

              paulk Paul King
              mchadfield Marc Hadfield
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: