Пример #1
0
  public void testPartialCU1() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.util.Vector;\n");
    buf.append("public class E {\n");
    buf.append("    private int fField1;\n");
    buf.append("    private int fField2;\n");
    buf.append("    public void foo1() {\n");
    buf.append("        fField1 = fField2;\n");
    buf.append("    }\n");
    buf.append("    public int foo1(int i) {\n");
    buf.append("        return i;\n");
    buf.append("    }\n");
    buf.append("}");
    String existing = buf.toString();
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", existing, false, null);

    String statement = "fField1 = fField2;";
    int offset = existing.indexOf(statement);

    CompilationUnit astRoot = getPartialCompilationUnit(cu, offset);
    String string = ASTNodes.asFormattedString(astRoot, 0, String.valueOf('\n'), null);

    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.util.Vector;\n");
    buf.append("public class E {\n");
    buf.append("    private int fField1;\n");
    buf.append("    private int fField2;\n");
    buf.append("    public void foo1() {\n");
    buf.append("        fField1 = fField2;\n");
    buf.append("    }\n");
    buf.append("    public int foo1(int i) {\n");
    buf.append("    }\n");
    buf.append("}");
    String expected = buf.toString();

    assertEqualString(string, expected);

    offset = expected.indexOf(statement);

    ASTNode node = NodeFinder.perform(astRoot, offset, statement.length());
    Assignment assignment = (Assignment) ((ExpressionStatement) node).getExpression();
    Expression e1 = assignment.getLeftHandSide();
    Expression e2 = assignment.getRightHandSide();
    assertNotNull(e1.resolveTypeBinding());
    assertNotNull(e2.resolveTypeBinding());

    assertTrue(((SimpleName) e1).resolveBinding() instanceof IVariableBinding);
    assertTrue(((SimpleName) e2).resolveBinding() instanceof IVariableBinding);

    assertAllBindings(astRoot);
  }
Пример #2
0
  public void testPartialCUPositionNotInMethod2() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.util.Vector;\n");
    buf.append("import java.io.IOException;\n");
    buf.append("import java.text.ParseException;\n");
    buf.append("public class E {\n");
    buf.append("    private class EInner {\n");
    buf.append("        {\n");
    buf.append("            System.out.println();\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("    private int fField1;\n");
    buf.append("    private int fField2;\n");
    buf.append("    public void foo1() throws IOException, ParseException {\n");
    buf.append("        fField1 = fField2;\n");
    buf.append("        if (fField1 == 0) {\n");
    buf.append("            fField2++;\n");
    buf.append("        }\n");
    buf.append("        EInner inner = new EInner();\n");
    buf.append("    }\n");
    buf.append("    public int foo2(int i) {\n");
    buf.append("        private class Local {\n");
    buf.append("            private int fField3;\n");
    buf.append("            public int local(int i) {\n");
    buf.append("                return 1;\n");
    buf.append("            }\n");
    buf.append("        }\n");
    buf.append("        Local local = new Local();\n");
    buf.append("        return i;\n");
    buf.append("    }\n");
    buf.append("}");
    String existing = buf.toString();
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", existing, false, null);

    int offset = existing.indexOf("private int fField3;");

    CompilationUnit astRoot = getPartialCompilationUnit(cu, offset);
    String string = ASTNodes.asFormattedString(astRoot, 0, String.valueOf('\n'), null);

    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.util.Vector;\n");
    buf.append("import java.io.IOException;\n");
    buf.append("import java.text.ParseException;\n");
    buf.append("public class E {\n");
    buf.append("    private class EInner {\n");
    buf.append("        {\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("    private int fField1;\n");
    buf.append("    private int fField2;\n");
    buf.append("    public void foo1() throws IOException, ParseException {\n");
    buf.append("    }\n");
    buf.append("    public int foo2(int i) {\n");
    buf.append("        private class Local {\n");
    buf.append("            private int fField3;\n");
    buf.append("            public int local(int i) {\n");
    buf.append("                return 1;\n");
    buf.append("            }\n");
    buf.append("        }\n");
    buf.append("        Local local = new Local();\n");
    buf.append("        return i;\n");
    buf.append("    }\n");
    buf.append("}");
    String expected = buf.toString();

    assertEqualString(string, expected);
    assertAllBindings(astRoot);
  }