Code view:

A painting is a person's account of this edit as text, recorded independently of the node mapping - a rendering often has several equally defensible answers where the mapping has one. Deletions and insertions carry no position on the opposite side in a painting, so that panel draws no caret for them.

Before

…lines 1–10 unchanged (10 lines)
11 }
12
13 public <T> T newInstance(Class<T> cls) {
14 if (outerClassInstance == null) {
15 return noArgConstructor(cls);
16 }
17 return withOuterClass(cls);
18 }
19
20 private <T> T withOuterClass(Class<T> cls) {
21 try {
22 //this is kind of overengineered because we don't need to support more params
23 //however, I know we will be needing it :)
24 Constructor<T> c = cls.getDeclaredConstructor(outerClassInstance.getClass());
25 return c.newInstance(outerClassInstance);
26 } catch (Exception e) {
27 throw paramsException(cls, e);
28 }
29 }
30
31 private static <T> InstantationException paramsException(Class<T> cls, Exception e) {
32 return new InstantationException("Unable to create mock instance of '"
33 + cls.getSimpleName() + "'.\nPlease ensure that the outer instance has correct type and that the target class has parameter-less constructor.", e);
34 }
35
36
37 private static <T> T noArgConstructor(Class<T> cls) {
38 try {
39 return cls.newInstance();
40 } catch (Exception e) {
41 throw new InstantationException("Unable to create mock instance of '"
…lines 42–46 unchanged (5 lines)

After

…lines 1–10 unchanged (10 lines)
11 }
12
13 public <T> T newInstance(Class<T> cls) {
14 if (outerClassInstance == null) {
15 return noArgConstructor(cls);
16 }
17 return withParams(cls, outerClassInstance);
18 }
19
20 private static <T> T withParams(Class<T> cls, Object... params) {
21 try {
22 //this is kind of overengineered because we don't need to support more params
23 //however, I know we will be needing it :)
24 for (Constructor<?> constructor : cls.getDeclaredConstructors()) {
25 Class<?>[] types = constructor.getParameterTypes();
26 if (paramsMatch(types, params)) {
27 return (T) constructor.newInstance(params);
28 }
29 }
30 } catch (Exception e) {
31 throw paramsException(cls, e);
32 }
33 throw paramsException(cls, null);
34 }
35
36 private static <T> InstantationException paramsException(Class<T> cls, Exception e) {
37 return new InstantationException("Unable to create mock instance of '"
38 + cls.getSimpleName() + "'.\nPlease ensure that the outer instance has correct type and that the target class has parameter-less constructor.", e);
39 }
40
41 private static boolean paramsMatch(Class<?>[] types, Object[] params) {
42 if (params.length != types.length) {
43 return false;
44 }
45 for (int i = 0; i < params.length; i++) {
46 if (!types[i].isInstance(params[i])) {
47 return false;
48 }
49 }
50 return true;
51 }
52
53 private static <T> T noArgConstructor(Class<T> cls) {
54 try {
55 return cls.newInstance();
56 } catch (Exception e) {
57 throw new InstantationException("Unable to create mock instance of '"
…lines 58–62 unchanged (5 lines)

Before

program
package_declaration
package "package"
scoped_identifier
scoped_identifier
scoped_identifier
scoped_identifier
identifier "org"
. "."
identifier "mockito"
. "."
identifier "internal"
. "."
identifier "creation"
. "."
identifier "instance"
; ";"
import_declaration
import "import"
scoped_identifier
scoped_identifier
scoped_identifier
identifier "java"
. "."
identifier "lang"
. "."
identifier "reflect"
. "."
identifier "Constructor"
; ";"
class_declaration
modifiers
public "public"
class "class"
identifier "ConstructorInstantiator"
super_interfaces
implements "implements"
type_list
type_identifier "Instantiator"
class_body
{ "{"
field_declaration
modifiers
private "private"
final "final"
type_identifier "Object"
variable_declarator
identifier "outerClassInstance"
; ";"
constructor_declaration (+22 nodes collapsed)
method_declaration
modifiers
public "public"
type_parameters
< "<"
type_parameter
type_identifier "T"
> ">"
type_identifier "T"
identifier "newInstance"
formal_parameters
( "("
formal_parameter
generic_type
type_identifier "Class"
type_arguments
< "<"
type_identifier "T"
> ">"
identifier "cls"
) ")"
block
{ "{"
if_statement (+21 nodes collapsed)
return_statement
return "return"
method_invocation
identifier "withOuterClass"
argument_list
( "("
identifier "cls"
) ")"
; ";"
} "}"
method_declaration
modifiers
private "private"
type_parameters
< "<"
type_parameter
type_identifier "T"
> ">"
type_identifier "T"
identifier "withOuterClass"
formal_parameters
( "("
formal_parameter
generic_type
type_identifier "Class"
type_arguments
< "<"
type_identifier "T"
> ">"
identifier "cls"
) ")"
block
{ "{"
try_statement
try "try"
block
{ "{"
line_comment "//this is kind of overengineered because we don't need to su"...
line_comment "//however, I know we will be needing it :)"
local_variable_declaration
generic_type
type_identifier "Constructor"
type_arguments
< "<"
type_identifier "T"
> ">"
variable_declarator
identifier "c"
= "="
method_invocation
identifier "cls"
. "."
identifier "getDeclaredConstructor"
argument_list
( "("
method_invocation
identifier "outerClassInstance"
. "."
identifier "getClass"
argument_list
( "("
) ")"
) ")"
; ";"
return_statement
return "return"
method_invocation
identifier "c"
. "."
identifier "newInstance"
argument_list
( "("
identifier "outerClassInstance"
) ")"
; ";"
} "}"
catch_clause (+22 nodes collapsed)
} "}"
method_declaration (+61 nodes collapsed)
method_declaration (+83 nodes collapsed)
} "}"

After

program
package_declaration
package "package"
scoped_identifier
scoped_identifier
scoped_identifier
scoped_identifier
identifier "org"
. "."
identifier "mockito"
. "."
identifier "internal"
. "."
identifier "creation"
. "."
identifier "instance"
; ";"
import_declaration
import "import"
scoped_identifier
scoped_identifier
scoped_identifier
identifier "java"
. "."
identifier "lang"
. "."
identifier "reflect"
. "."
identifier "Constructor"
; ";"
class_declaration
modifiers
public "public"
class "class"
identifier "ConstructorInstantiator"
super_interfaces
implements "implements"
type_list
type_identifier "Instantiator"
class_body
{ "{"
field_declaration
modifiers
private "private"
final "final"
type_identifier "Object"
variable_declarator
identifier "outerClassInstance"
; ";"
constructor_declaration (+22 nodes collapsed)
method_declaration
modifiers
public "public"
type_parameters
< "<"
type_parameter
type_identifier "T"
> ">"
type_identifier "T"
identifier "newInstance"
formal_parameters
( "("
formal_parameter
generic_type
type_identifier "Class"
type_arguments
< "<"
type_identifier "T"
> ">"
identifier "cls"
) ")"
block
{ "{"
if_statement (+21 nodes collapsed)
return_statement
return "return"
method_invocation
identifier "withParams"
argument_list
( "("
identifier "cls"
, ","
identifier "outerClassInstance"
) ")"
; ";"
} "}"
method_declaration
modifiers
private "private"
static "static"
type_parameters
< "<"
type_parameter
type_identifier "T"
> ">"
type_identifier "T"
identifier "withParams"
formal_parameters
( "("
formal_parameter
generic_type
type_identifier "Class"
type_arguments
< "<"
type_identifier "T"
> ">"
identifier "cls"
, ","
spread_parameter
type_identifier "Object"
... "..."
variable_declarator
identifier "params"
) ")"
block
{ "{"
try_statement
try "try"
block
{ "{"
line_comment "//this is kind of overengineered because we don't need to su"...
line_comment "//however, I know we will be needing it :)"
enhanced_for_statement
for "for"
( "("
generic_type
type_identifier "Constructor"
type_arguments
< "<"
wildcard
? "?"
> ">"
identifier "constructor"
: ":"
method_invocation
identifier "cls"
. "."
identifier "getDeclaredConstructors"
argument_list
( "("
) ")"
) ")"
block
{ "{"
local_variable_declaration
array_type
generic_type
type_identifier "Class"
type_arguments
< "<"
wildcard
? "?"
> ">"
dimensions
[ "["
] "]"
variable_declarator
identifier "types"
= "="
method_invocation
identifier "constructor"
. "."
identifier "getParameterTypes"
argument_list
( "("
) ")"
; ";"
if_statement
if "if"
parenthesized_expression
( "("
method_invocation
identifier "paramsMatch"
argument_list
( "("
identifier "types"
, ","
identifier "params"
) ")"
) ")"
block
{ "{"
return_statement
return "return"
cast_expression
( "("
type_identifier "T"
) ")"
method_invocation
identifier "constructor"
. "."
identifier "newInstance"
argument_list
( "("
identifier "params"
) ")"
; ";"
} "}"
} "}"
} "}"
catch_clause (+22 nodes collapsed)
throw_statement
throw "throw"
method_invocation
identifier "paramsException"
argument_list
( "("
identifier "cls"
, ","
null_literal "null"
) ")"
; ";"
} "}"
method_declaration (+61 nodes collapsed)
method_declaration
modifiers
private "private"
static "static"
boolean_type "boolean"
identifier "paramsMatch"
formal_parameters
( "("
formal_parameter
array_type
generic_type
type_identifier "Class"
type_arguments
< "<"
wildcard
? "?"
> ">"
dimensions
[ "["
] "]"
identifier "types"
, ","
formal_parameter
array_type
type_identifier "Object"
dimensions
[ "["
] "]"
identifier "params"
) ")"
block
{ "{"
if_statement
if "if"
parenthesized_expression
( "("
binary_expression
field_access
identifier "params"
. "."
identifier "length"
!= "!="
field_access
identifier "types"
. "."
identifier "length"
) ")"
block
{ "{"
return_statement
return "return"
false "false"
; ";"
} "}"
for_statement
for "for"
( "("
local_variable_declaration
integral_type
int "int"
variable_declarator
identifier "i"
= "="
decimal_integer_literal "0"
; ";"
binary_expression
identifier "i"
< "<"
field_access
identifier "params"
. "."
identifier "length"
; ";"
update_expression
identifier "i"
++ "++"
) ")"
block
{ "{"
if_statement
if "if"
parenthesized_expression
( "("
unary_expression
! "!"
method_invocation
array_access
identifier "types"
[ "["
identifier "i"
] "]"
. "."
identifier "isInstance"
argument_list
( "("
array_access
identifier "params"
[ "["
identifier "i"
] "]"
) ")"
) ")"
block
{ "{"
return_statement
return "return"
false "false"
; ";"
} "}"
} "}"
return_statement
return "return"
true "true"
; ";"
} "}"
method_declaration (+83 nodes collapsed)
} "}"