Groovy MissingPropertyException on HashSet? - groovy

My app has the following directory structure:
myapp/
src/
com.me.myapp/
Driver.groovy
Fizz.groovy
stages (package)
AbstractStage.groovy
impl (package)
DefaultStage.groovy
Where:
class Driver {
static void main(String[] args) {
AbstractStage stage1 = new DefaultStage()
stage1.derp
}
}
abstract class AbstractStage {
Set<Fizz> fizzes = new HashSet<Fizz>()
void derp() {
println "I have " + fizzes.size + " attached fizzes!"
}
abstract void doSomething()
}
class DefaultStage extends AbstractStage {
Set<Fizz> executedFizzes = new HashSet<Fizz>()
#Override
void doSomething() {
// TODO: Implement
}
}
When I run this I get:
Exception in thread "main" groovy.lang.MissingPropertyException: No such property: derp for class:com.me.myapp.stages.impl.DefaultStage
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:50)
at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:49)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:231)
at com.me.myapp.Driver.main(Driver.groovy:6)
...where Driver.groovy:6 corresponds to the line that calls derp.
What's going on here?

derp is a method, not a property, so you need to invoke it:
static void main(String[] args) {
AbstractStage stage1 = new DefaultStage()
stage1.derp() // You need the '()' here.
}

Related

How to use the mock object of the test class and call main method from the test class

In the test class trying to test the main method and not sure how to use the mock object here.
How to use the mock object orders to call the main method and it's methods inside the main method
Any suggestions on how this can be done?
public class Orders {
public static void main(String[] s) {
Orders jp = new Orders();
jp.method1();
List<OrderItems> lstOrdItms = jp.getListOrderItems();
jp.processOrderItems( lstOrdItms );
}
public void method1(List<OrderItem> lstOrderItem) {
.........
}
public List<OrderItem> getListOrderItems() {
............
}
public void processOrderItems() {
............
}
}
public class OrdersTest {
#Mocks
Orders orders;
.....
#Before
public void setUp() {
........
}
#Test
public void testMain() throws SQLException {
// Not sure how to test here
// This will actually execute the main method instead of mock.
orders.main(new String[]{});
}
}

Generic list and static main

using System;
using System.Collections.Generic;
namespace ConsoleApplication74
{
class Program<T>
{
public void Add(T X)
{
Console.WriteLine("{0}", X);
}
static void Main(string[] args)
{
Program<string> MyGeneric = new Program<string>();
MyGeneric.Add("ABC");
Console.Read();
}
}
I have erroe Program does not contain a static 'Main' method suitable for an entry point.
Program.cs properties has Build Action as Compile.
I have no idea what is wrong.
The Main method, or entry point in your program, cannot be in a class that has generic arguments. Your Program class has a T type argument. The C# specification calls this out in section 3.1 under Application Startup:
The application entry point method may not be in a generic class declaration.
You should make a new class instead of trying to use Program:
class Program
{
static void Main(string[] args)
{
MyClass<string> MyGeneric = new MyClass<string>();
MyGeneric.Add("ABC");
Console.Read();
}
}
class MyClass<T>
{
public void Add(T X)
{
Console.WriteLine("{0}", X);
}
}

Is threre any way to ues override method in anonymous class on Groovy #CompileStatic annotation

Is threre any way to ues override method in anonymous class on Groovy #CompileStatic annotation?
groogy source
import groovy.transform.CompileStatic;
interface HelloWorld {
public void greet();
}
class HelloWorldAnonymousClassesParents {
public void hi() {
println "hi"
}
}
#CompileStatic
public class HelloWorldAnonymousClasses extends HelloWorldAnonymousClassesParents {
public void hi() {
System.out.println("hihi ");
}
public void sayHello() {
HelloWorld spanishGreeting = new HelloWorld() {
public void greet() {
hi() //<- here [Static type checking] - Reference to method is ambiguous error
System.out.println("spanishGreeting");
}
};
spanishGreeting.greet();
hi()
}
}
def myApp = new HelloWorldAnonymousClasses();
myApp.sayHello();
Same source in java run well
java source
package org.octopus;
class HelloWorldAnonymousClassesParents {
public void hi() {
System.out.println("hi ");
}
}
interface HelloWorld {
public void greet();
}
public class Test extends HelloWorldAnonymousClassesParents{
public void hi() {
System.out.println("hihi ");
}
public void sayHello() {
HelloWorld spanishGreeting = new HelloWorld() {
public void greet() {
hi();
System.out.println("spanishGreeting");
}
};
spanishGreeting.greet();
hi();
}
public static void main(String... args) {
Test myApp = new Test();
myApp.sayHello();
}
}
How can I avoid that error with #CompileStatic annotation?
You can write it as a closure. This implies an as HelloWorld and as the interface only has one method groovy can deduct this.
HelloWorld spanishGreeting = {
hi()
System.out.println("spanishGreeting");
}
above code is groovy 2.3; with earlier groovy 2 versions it needs an explicit cast like
def spanishGreeting = {/*...*/} as HelloWorld

Groovy Inner Classes wont work with Apache Wicket

Im trying to write simple things with Apache Wicket (6.15.0) and Groovy (2.2.2 or 2.3.1). And Im having trouble with inner classes.
class CreatePaymentPanel extends Panel {
public CreatePaymentPanel(String id) {
super(id)
add(new PaymentSelectFragment('currentPanel').setOutputMarkupId(true))
}
public class PaymentSelectFragment extends Fragment {
public PaymentSelectFragment(String id) {
super(id, 'selectFragment', CreatePaymentPanel.this) // problem here
add(new AjaxLink('cardButton') {
#Override
void onClick(AjaxRequestTarget target) {
... CreatePaymentPanel.this // not accessible here
}
})
add(new AjaxLink('terminalButton') {
#Override
void onClick(AjaxRequestTarget target) {
... CreatePaymentPanel.this // not accessible here
}
});
}
} // end of PaymentSelectFragment class
} // end of CreatePaymentPanel class
Groovy tries to find a property "this" in CreatePaymentPanel class.. How to workaround this? It is a valid java code, but not groovy.
However,
Test.groovy:
class Test {
static void main(String[] args) {
def a = new A()
}
static class A {
A() {
def c = new C()
}
public void sayA() { println 'saying A' }
class B {
public B(A instance) {
A.this.sayA()
instance.sayA()
}
}
/**
* The problem occurs here
*/
class C extends B {
public C() {
super(A.this) // groovy tries to find property "this" in A class
sayA()
}
}
}
}
Above code wont work, the same error occurs, like in Wicket's case.
And TestJava.java, the same and working:
public class TestJava {
public static void main(String[] args) {
A a = new A();
}
static class A {
A() {
C c = new C();
}
public void sayA() {
System.out.println("saying A");
}
class B {
public B(A instance) {
instance.sayA();
}
}
/**
* This works fine
*/
class C extends B {
public C() {
super(A.this);
sayA();
}
}
}
}
What I am missing?
You can't refer to a CreatePaymentPanel.this inside of PaymentSelectFragment because there is no instance of CreatePamentPanel that would be accessible there. What would you expect that to evaluate to if it were allowed?

Is it possible to write a custom redirect in java

I am testing an application.
My test is complex, and I spawn 2 thread that start 2 process builders which spawn 2 java processes.
Is it possible to write a custom redirect that will be similar to inherit but prepend something to every out and err message, so that I would know its origin.
Example code below:
public class test {
public static void main(String... args){
Thread t = new Thread(new testHelper());
t.start();
t = new Thread(new testHelper());
t.start();
}
}
import java.io.IOException;
public class testHelper implements Runnable {
#Override
public void run() {
Class klass = testWorker.class;
System.out.println(klass.getCanonicalName());
String separator = System.getProperty("file.separator");
String classpath = System.getProperty("java.class.path");
String path = System.getProperty("java.home")
+ separator + "bin" + separator + "java";
ProcessBuilder processBuilder =
new ProcessBuilder(path, "-cp",
classpath,
klass.getCanonicalName());
processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
Process process = null;
try {
process = processBuilder.start();
} catch (IOException e) {
e.printStackTrace();
}
try {
process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Child Process is done");
}
}
public class testWorker {
public static void main(String ... args) throws InterruptedException {
System.out.println("Doing some stuff");
Thread.sleep(10000);
System.out.println("Finished doing some stuff");
}
}
No, its not possible. In the source code for java.lang.ProcessBuilder.Redirect the constructor is private and has this to say
/**
* No public constructors. Clients must use predefined
* static {#code Redirect} instances or factory methods.
*/
private Redirect() {}

Resources