JavaPropertiesHelper { @CheckForNull public static ExpressionTree retrievedPropertyDefaultValue(ExpressionTree expression) { if (expression.is(Tree.Kind.IDENTIFIER)) { return retrievedPropertyDefaultValue((IdentifierTree) expression); } else if (expression.is(Tree.Kind.METHOD_INVOCATION)) { return retrievedPropertyDefaultValue((MethodInvocationTree) expression); } return null; } private  JavaPropertiesHelper(); @CheckForNull static ExpressionTree retrievedPropertyDefaultValue(ExpressionTree expression);  }
@Test void null_if_not_get_property() throws Exception { ExpressionTree tree = firstExpression("void foo(java.util.Properties props){ props.setProperty(\"myKey\", \"myValue\"); }"); ExpressionTree defaultValue = JavaPropertiesHelper.retrievedPropertyDefaultValue(tree); assertThat(defaultValue).isNull(); tree = firstExpression("void foo(){ System.out.println(); }"); defaultValue = JavaPropertiesHelper.retrievedPropertyDefaultValue(tree); assertThat(defaultValue).isNull(); }
@Test void null_if_get_property_without_default_value() throws Exception { ExpressionTree tree = firstExpression("void foo(java.util.Properties props){ props.getProperty(\"myKey\"); }"); ExpressionTree defaultValue = JavaPropertiesHelper.retrievedPropertyDefaultValue(tree); assertThat(defaultValue).isNull(); }
@Test void null_if_not_identifier_or_method_invocation() throws Exception { ExpressionTree tree = firstExpression("void foo(int a){ a++; }"); ExpressionTree defaultValue = JavaPropertiesHelper.retrievedPropertyDefaultValue(tree); assertThat(defaultValue).isNull(); }
@Test void retrieve_default_value_on_method_invocation() throws Exception { ExpressionTree tree = firstExpression("void foo(java.util.Properties props){ props.getProperty(\"myKey\", \"defaultValue\"); }"); ExpressionTree defaultValue = JavaPropertiesHelper.retrievedPropertyDefaultValue(tree); assertThat(defaultValue).isNotNull(); assertThat(defaultValue.is(Tree.Kind.STRING_LITERAL)).isTrue(); }
@Test void retrieve_default_value_on_identifier() throws Exception { ExpressionTree tree = firstExpression( "void foo(String prop){ foo(myValue); } " + "java.util.Properties props = new java.util.Properties();" + "String myValue = props.getProperty(\"myKey\", \"defaultValue\");"); ExpressionTree defaultValue = JavaPropertiesHelper.retrievedPropertyDefaultValue(((MethodInvocationTree) tree).arguments().get(0)); assertThat(defaultValue).isNotNull(); assertThat(defaultValue.is(Tree.Kind.STRING_LITERAL)).isTrue(); }
@Test void null_if_variable_used_more_than_once() throws Exception { ExpressionTree tree = firstExpression( "void foo(String prop){ foo(myValue); myValue = 0; } " + "java.util.Properties props = new java.util.Properties();" + "String myValue = props.getProperty(\"myKey\", \"defaultValue\");"); ExpressionTree defaultValue = JavaPropertiesHelper.retrievedPropertyDefaultValue(((MethodInvocationTree) tree).arguments().get(0)); assertThat(defaultValue).isNull(); }
@Test void null_if_variable_not_initialized() throws Exception { ExpressionTree tree = firstExpression( "void foo(String prop){ foo(myValue);} " + "String myValue;"); ExpressionTree defaultValue = JavaPropertiesHelper.retrievedPropertyDefaultValue(((MethodInvocationTree) tree).arguments().get(0)); assertThat(defaultValue).isNull(); }
@Test void null_if_variable_not_initilialized_method_invocation() throws Exception { ExpressionTree tree = firstExpression( "void foo(String prop){ foo(myValue);} " + "java.util.Properties props = new java.util.Properties();" + "String myValue = \"hello\";"); ExpressionTree defaultValue = JavaPropertiesHelper.retrievedPropertyDefaultValue(((MethodInvocationTree) tree).arguments().get(0)); assertThat(defaultValue).isNull(); }
@Test void null_if_unknown_symbol() throws Exception { ExpressionTree tree = firstExpression("void foo() { unknown(e -> e.method()); }"); ExpressionTree functionCallArgument = ((MethodInvocationTree) tree).arguments().get(0); MethodInvocationTree methodCallInsideLambda = (MethodInvocationTree)((LambdaExpressionTree)functionCallArgument).body(); ExpressionTree defaultValue = JavaPropertiesHelper.retrievedPropertyDefaultValue(methodCallInsideLambda); assertThat(defaultValue).isNull(); }