当前位置: 首页>>代码示例>>Java>>正文


Java CtMethod.clone方法代码示例

本文整理汇总了Java中spoon.reflect.declaration.CtMethod.clone方法的典型用法代码示例。如果您正苦于以下问题:Java CtMethod.clone方法的具体用法?Java CtMethod.clone怎么用?Java CtMethod.clone使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在spoon.reflect.declaration.CtMethod的用法示例。


在下文中一共展示了CtMethod.clone方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: cloneMethod

import spoon.reflect.declaration.CtMethod; //导入方法依赖的package包/类
private static CtMethod cloneMethod(CtMethod method, String suffix) {
    CtMethod cloned_method = method.clone();
    //rename the clone
    cloned_method.setSimpleName(method.getSimpleName() + (suffix.isEmpty() ? "" : suffix + cloneNumber));
    cloneNumber++;

    CtAnnotation toRemove = cloned_method.getAnnotations().stream()
            .filter(annotation -> annotation.toString().contains("Override"))
            .findFirst().orElse(null);

    if (toRemove != null) {
        cloned_method.removeAnnotation(toRemove);
    }
    return cloned_method;
}
 
开发者ID:STAMP-project,项目名称:dspot,代码行数:16,代码来源:AmplificationHelper.java

示例2: apply

import spoon.reflect.declaration.CtMethod; //导入方法依赖的package包/类
@Override
public List<CtMethod> apply(CtMethod testMethod) {
    final CtCodeSnippetStatement snippet = testMethod.getFactory().Code().
            createCodeSnippetStatement("UncompilableClass class = new UncompilableClass()");
    final CtMethod method = testMethod.clone();
    method.getBody().insertEnd(snippet);
    method.setSimpleName("uncompilableTest");

    final CtCodeSnippetStatement snippet1 = testMethod.getFactory().Code().createCodeSnippetStatement("String clazz = new String()");
    final CtMethod method1 = testMethod.clone();
    method1.getBody().insertEnd(snippet1);
    method1.setSimpleName("compilableTest");

    return Arrays.asList(method, method1);
}
 
开发者ID:STAMP-project,项目名称:dspot,代码行数:16,代码来源:DSpotCompilerTest.java

示例3: performExtraction

import spoon.reflect.declaration.CtMethod; //导入方法依赖的package包/类
public static CtMethod<?> performExtraction(CtMethod<?> ctMethod) {

        // remove assertion

        final CtMethod<?> ctMethodWithoutAssertion =
                new AssertionRemover().removeAssertion(ctMethod);

        final Factory factory = ctMethodWithoutAssertion.getFactory();
        final CtMethod<?> extractedMethod = ctMethodWithoutAssertion.clone();

        extractedMethod.setSimpleName("extract_" + ctMethodWithoutAssertion.getSimpleName());
        new ArrayList<>(extractedMethod.getThrownTypes()).forEach(extractedMethod::removeThrownType);
        extractedMethod.setParent(ctMethod.getParent());
        ctMethodWithoutAssertion.getAnnotations().forEach(extractedMethod::removeAnnotation);
        final int[] count = new int[1];
        final Map<CtAbstractInvocation<?>, List<CtVariableAccess>> parametersPerInvocation =
                new HashMap<>();
        extractedMethod.getElements(new TypeFilter<>(CtLiteral.class))
                .stream()
                .filter(ctLiteral -> !(ctLiteral.getValue() instanceof String)) // TODO
                .forEach(ctLiteral -> {
                    final CtParameter<?> parameter = factory.createParameter();
                    parameter.setType(ctLiteral.getType());
                    parameter.setSimpleName("lit" + count[0]++);
                    extractedMethod.addParameter(parameter);
                    final CtVariableAccess<?> variableRead = factory.createVariableRead(factory.createParameterReference(parameter), false);
                    final CtAbstractInvocation invocation = ctLiteral.getParent(CtAbstractInvocation.class);
                    if (invocation != null) {
                        if (!parametersPerInvocation.containsKey(invocation)) {
                            parametersPerInvocation.put(invocation, new ArrayList<>());
                        }
                        if (ctLiteral.getParent() instanceof CtUnaryOperator) {
                            parametersPerInvocation.get(invocation)
                                    .add(invocation.getArguments().indexOf(ctLiteral.getParent()), variableRead);
                        } else {
                            parametersPerInvocation.get(invocation)
                                    .add(invocation.getArguments().indexOf(ctLiteral), variableRead);
                        }
                    } else {
                        ctLiteral.replace(variableRead);
                    }
                });

        extractedMethod.getElements(new TypeFilter<>(CtAbstractInvocation.class))
                .stream()
                .filter(parametersPerInvocation::containsKey)
                .forEach(ctAbstractInvocation ->
                    ctAbstractInvocation.setArguments(parametersPerInvocation.get(ctAbstractInvocation))
                );

        return extractedMethod;
    }
 
开发者ID:STAMP-project,项目名称:Ex2Amplifier,代码行数:53,代码来源:ArgumentsExtractor.java

示例4: testMethodCallAddRandom

import spoon.reflect.declaration.CtMethod; //导入方法依赖的package包/类
@Test
public void testMethodCallAddRandom() throws Exception {

      /*
        Test that we duplicate method call in a test for each used method in the test.
            Here, we test the applyRandom feature that will build one method randomly by reusing an existing call.
      */

    CtClass<Object> testClass = Utils.getFactory().Class().get("fr.inria.mutation.ClassUnderTestTest");
    AmplificationHelper.setSeedRandom(23L);

    TestMethodCallAdder methodCallAdder = new TestMethodCallAdder();
    methodCallAdder.reset(null);

    final CtMethod<?> originalMethod = testClass.getMethods().stream().filter(m -> "testAddCall".equals(m.getSimpleName())).findFirst().get();
    CtMethod amplifiedMethod = originalMethod.clone();
    amplifiedMethod = methodCallAdder.applyRandom(originalMethod);

    assertEquals(originalMethod.getBody().getStatements().size() + 1, amplifiedMethod.getBody().getStatements().size());
    CtStatement expectedStatement = originalMethod.getBody().getStatements().get(2);
    assertEquals("// MethodCallAdder" + nl + expectedStatement.toString(), amplifiedMethod.getBody().getStatements().get(2).toString());
    assertEquals(expectedStatement.toString(), amplifiedMethod.getBody().getStatements().get(3).toString());


    amplifiedMethod = methodCallAdder.applyRandom(originalMethod);
    assertEquals(originalMethod.getBody().getStatements().size() + 1, amplifiedMethod.getBody().getStatements().size());
    expectedStatement = originalMethod.getBody().getStatements().get(1);
    assertEquals("// MethodCallAdder" + nl + expectedStatement.toString(), amplifiedMethod.getBody().getStatements().get(1).toString());
    assertEquals(expectedStatement.toString(), amplifiedMethod.getBody().getStatements().get(2).toString());

    amplifiedMethod = methodCallAdder.applyRandom(originalMethod);
    assertEquals(originalMethod.getBody().getStatements().size() + 1, amplifiedMethod.getBody().getStatements().size());
    expectedStatement = originalMethod.getBody().getStatements().get(1);
    assertEquals("// MethodCallAdder" + nl + expectedStatement.toString(), amplifiedMethod.getBody().getStatements().get(1).toString());
    assertEquals(expectedStatement.toString(), amplifiedMethod.getBody().getStatements().get(2).toString());

    // stack random amplification
    amplifiedMethod.setParent(originalMethod.getParent());
    CtMethod stackedAmplifiedMethod = methodCallAdder.applyRandom(amplifiedMethod);
    assertEquals(amplifiedMethod.getBody().getStatements().size() + 1, stackedAmplifiedMethod.getBody().getStatements().size());
    assertEquals(originalMethod.getBody().getStatements().size() + 2, stackedAmplifiedMethod.getBody().getStatements().size());
}
 
开发者ID:STAMP-project,项目名称:dspot,代码行数:43,代码来源:TestMethodCallAdderTest.java


注:本文中的spoon.reflect.declaration.CtMethod.clone方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。