That is a bad thing to be trying to do. If you don't know what the generic type is going to be, how do you know if passing a String is type-safe?
If you really want to do this, you have to add the appropriate annotation to the exec method to tell it to suppress the unsafe/unchecked type conversion error. But that is just turning the compilation into a potential ClassCastException at runtime that might be thrown unexpectedly by some subtype of ExecContext that does something different in the doSomething method.
EDIT I don't think that the following is correct either:
public static <T> void exec(ExecContext<T> ctx) {
String s = new String("saoj");
ctx.doSomething(s);
}
because the compiler cannot tell that the actual type used for T will be String or a (hypothetical) subtype of String. Suppose for instance that exec was called as follows:
ExecContext<Integer> ctx = ...;
exec(ctx);
Now when exec calls the doSomething method, it will pass a String instance as an argument, where the generic signature of the doSomething method says it should be passing an instance of E; i.e. an Integer.