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


Java Filter类代码示例

本文整理汇总了Java中spoon.reflect.visitor.Filter的典型用法代码示例。如果您正苦于以下问题:Java Filter类的具体用法?Java Filter怎么用?Java Filter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


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

示例1: processOverridden

import spoon.reflect.visitor.Filter; //导入依赖的package包/类
private static <T> void processOverridden(CtClass<?> mergeInto, CtClass<?> toMerge,
                                          final CtMethod<T> methodToMerge) {
    List<CtInvocation<T>> superInvocations = mergeInto.getElements(
            new Filter<CtInvocation<T>>() {
                @Override
                public boolean matches(CtInvocation<T> invocation) {
                    if (!(invocation.getTarget() instanceof CtSuperAccess))
                        return false;
                    CtExecutable<?> m = invocation.getExecutable().getDeclaration();
                    return m != null && MethodNode.overrides((CtMethod<?>) m, methodToMerge);
                }
            });
    
    methodToMerge.setSimpleName(classPrefixedName(toMerge, methodToMerge));
    methodToMerge.setVisibility(PRIVATE);
    removeAnnotation(methodToMerge, Override.class);
    
    for (CtInvocation<T> superInvocation : superInvocations) {
        superInvocation.setTarget(null);
        superInvocation.setExecutable(methodToMerge.getReference());
    }
    add(mergeInto, methodToMerge, mergeInto::addMethod);
}
 
开发者ID:OpenHFT,项目名称:Stage-Compiler,代码行数:24,代码来源:ExtensionChains.java

示例2: findWidgetRegistrations

import spoon.reflect.visitor.Filter; //导入依赖的package包/类
private @NotNull Set<CtAbstractInvocation<?>> findWidgetRegistrations(final @NotNull WidgetProcessor.WidgetUsage usage) {
	final CtExecutable<?> cmdExec = cmd.getExecutable();
	final Filter<CtAbstractInvocation<?>> filter = new BasicFilter<CtAbstractInvocation<?>>(CtAbstractInvocation.class) {
		@Override
		public boolean matches(final CtAbstractInvocation<?> exec) {
			final OptionalInt pos = getListenerRegPositionInInvok(exec);
			return pos.isPresent() && WidgetHelper.INSTANCE.isListenerClass(exec.getExecutable().getParameters().get(pos.getAsInt()),
				exec.getFactory(), WidgetHelper.INSTANCE.getListenerInterface(cmdExec).orElse(null));
		}
	};

	return usage.getUsagesWithCons().stream().
		// gathering their parent statement.
			map(acc -> SpoonHelper.INSTANCE.getStatement(acc)).filter(stat -> stat.isPresent()).map(stat -> stat.get()).
		// Gathering the method call that matches listener registration: single parameter that is a listener type.
			map(stat -> stat.getElements(filter)).flatMap(s -> s.stream()).collect(Collectors.toSet());
}
 
开发者ID:diverse-project,项目名称:InspectorGuidget,代码行数:18,代码来源:ListenerCommandRefactor.java

示例3: changeThisAccesses

import spoon.reflect.visitor.Filter; //导入依赖的package包/类
/**
 * The statements of the command may refer to the external listener through 'this'. In such a case, the 'this' has to be changed
 * in a variable access.
 */
private void changeThisAccesses(final @NotNull List<CtElement> stats, final @NotNull CtAbstractInvocation<?> regInvok) {
	final Filter<CtThisAccess<?>> filter = new ThisAccessFilter(false);
	final CtExecutable<?> regMethod = regInvok.getParent(CtExecutable.class);

	stats.stream().map(stat -> stat.getElements(filter)).flatMap(s -> s.stream()).forEach(th -> {
		List<CtLocalVariable<?>> thisVar = regMethod.getElements(new BasicFilter<CtLocalVariable<?>>(CtLocalVariable.class) {
			@Override
			public boolean matches(final CtLocalVariable<?> element) {
				final CtType<?> decl = element.getType().getDeclaration();
				return decl!=null && decl.equals(th.getType().getDeclaration());
			}
		});
		if(thisVar.isEmpty()) {
			LOG.log(Level.SEVERE, "Cannot find a local variable for a 'this' access: " + thisVar);
		}else {
			th.replace(regInvok.getFactory().Code().createVariableRead(thisVar.get(0).getReference(), false));
		}
	});
}
 
开发者ID:diverse-project,项目名称:InspectorGuidget,代码行数:24,代码来源:ListenerCommandRefactor.java

示例4: getFieldInitializer

import spoon.reflect.visitor.Filter; //导入依赖的package包/类
public List<CtLiteral> getFieldInitializer(CtField ctField) {
    List<CtLiteral> ctLiterals = new ArrayList<CtLiteral>();
    for (CtElement ctElement : ctField.getElements(new Filter<CtLiteral>() {
        @Override
        public boolean matches(CtLiteral ctLiteral) {
            return true;
        }
    })) {
        ctLiterals.add((CtLiteral)ctElement);
    }
    return ctLiterals;
}
 
开发者ID:slipperyseal,项目名称:trebuchet,代码行数:13,代码来源:FieldGroups.java

示例5: wasInitializedBefore

import spoon.reflect.visitor.Filter; //导入依赖的package包/类
private boolean wasInitializedBefore(CtStatement statement, CtVariable<?> variable) {
    if (variable.getDefaultExpression() == null) {
        CtBlock<?> block = variable.getParent(CtBlock.class);
        Filter<CtAssignment<?, ?>> filter = initializationAssignmentsFilterFor(variable, statement);
        return !block.getElements(filter).isEmpty();
    }
    return true;
}
 
开发者ID:SpoonLabs,项目名称:nopol,代码行数:9,代码来源:CollectableValueFinder.java

示例6: initializationAssignmentsFilterFor

import spoon.reflect.visitor.Filter; //导入依赖的package包/类
private Filter<CtAssignment<?, ?>> initializationAssignmentsFilterFor(CtVariable<?> variable, CtStatement statement) {
    VariableAssignmentFilter variableAssignment = new VariableAssignmentFilter(variable);
    BeforeLocationFilter<CtAssignment<?, ?>> beforeLocation = new BeforeLocationFilter(CtAssignment.class, statement.getPosition());
    InBlockFilter<CtAssignment<?, ?>> inVariableDeclarationBlock = new InBlockFilter(CtAssignment.class, asList(variable.getParent(CtBlock.class)));
    InBlockFilter<CtAssignment<?, ?>> inStatementBlock = new InBlockFilter(CtAssignment.class, asList(statement.getParent(CtBlock.class)));
    Filter<CtAssignment<?, ?>> inBlockFilter = new CompositeFilter(FilteringOperator.UNION, inStatementBlock, inVariableDeclarationBlock);
    return new CompositeFilter(FilteringOperator.INTERSECTION, variableAssignment, beforeLocation, inBlockFilter);
}
 
开发者ID:SpoonLabs,项目名称:nopol,代码行数:9,代码来源:CollectableValueFinder.java

示例7: process

import spoon.reflect.visitor.Filter; //导入依赖的package包/类
@Override
public void process(final CtMethod<?> method) {
    if (method != null) {
        if (isTestCase(method)) {
            instrumentMethod(method);
        } else {
            if (method.getBody() != null) {
                List<CtIf> ifList = method.getBody().getElements(
                        new Filter<CtIf>() {

                            @Override
                            public boolean matches(CtIf arg0) {
                                if (!(arg0 instanceof CtIf)) {
                                    return false;
                                }

                                return true;
                            }
                        });
                for (CtIf tmp : ifList) {
                    instrumentIfInsideMethod(tmp);
                }
            }
        }

        String s = method.getDeclaringType().getQualifiedName();
        if (this.ifMetric != null && !this.ifMetric.modifyClass.contains(s)) {
            this.ifMetric.modifyClass.add(s);
        }


    }

}
 
开发者ID:SpoonLabs,项目名称:nopol,代码行数:35,代码来源:IfCountingInstrumentingProcessor.java

示例8: elementFromSnippet

import spoon.reflect.visitor.Filter; //导入依赖的package包/类
@SuppressWarnings({"unchecked", "rawtypes"})
private CtElement elementFromSnippet(File sourceFile, String codeSnippet) {
	Factory model = SpoonModelLibrary.modelFor(new File[]{sourceFile});
	Filter filter = new CodeSnippetFilter(sourceFile, codeSnippet);
	List<CtElement> elements = SpoonElementLibrary.filteredElements(model, filter);
	assertEquals(1, elements.size());
	return elements.get(0);
}
 
开发者ID:SpoonLabs,项目名称:nopol,代码行数:9,代码来源:ValuesCollectorTest.java

示例9: filterBlocksForBuildingDeps

import spoon.reflect.visitor.Filter; //导入依赖的package包/类
@Override
public <E extends CtElement> List<E> filterBlocksForBuildingDeps(Filter<E> filter) {
    Stream<CtMethod<?>> depsBuildingMethods =
            concat(initStageMethods.stream(), stageMethods.keySet().stream());
    Stream<CtMethod<Void>> closeMethods =
            Stream.of(getCloseMethod().get(), getDoCloseMethod());
    depsBuildingMethods = concat(depsBuildingMethods, closeMethods);
    return depsBuildingMethods.flatMap(initMethod -> initMethod.getElements(filter).stream())
            .collect(toList());
}
 
开发者ID:OpenHFT,项目名称:Stage-Compiler,代码行数:11,代码来源:StageModel.java

示例10: checkListenerMatching

import spoon.reflect.visitor.Filter; //导入依赖的package包/类
/**
 * Checks that the matchings correspond to the listener that contains the command:
 * a reference of the listener is searched in the usages of the identified widgets and is compared
 * to the listener of the command to determine whether this widget really matches the command.
 * This analysis does not work with toolkit that does not require listener registration such as
 * with JavaFX where a listener method can be associated with the widget directly in the FXML.
 * @param listenerClass The listener class of the command.
 * @param cmdWidgetMatches The list of the widgets that may correspond to the command.
 * @param <T> The type of the matching.
 * @return The filtered list of widgets.
 */
private @NotNull <T extends CmdWidgetMatch> List<T> checkListenerMatching(final @Nullable CtClass<?> listenerClass, final @NotNull List<T> cmdWidgetMatches) {
	if(listenerClass == null) return cmdWidgetMatches;

	final CtTypeReference<?> listRef = listenerClass.getReference();
	final Filter<CtTypedElement<?>> filt = new BasicFilter<>(CtTypedElement.class);

	cmdWidgetMatches.removeIf(m ->
		// Removing if in the statement of the access there is a reference to the current listener class.
		m.usage.getUsagesWithCons().stream().noneMatch(a -> a.getParent(CtStatement.class).getElements(filt).stream().
			map(var -> var.getType()).anyMatch(ty -> ty != null && ty.equals(listRef))));
	return cmdWidgetMatches;
}
 
开发者ID:diverse-project,项目名称:InspectorGuidget,代码行数:24,代码来源:CommandWidgetFinder.java

示例11: removeActionCommandStatements

import spoon.reflect.visitor.Filter; //导入依赖的package包/类
private void removeActionCommandStatements() {
	usages.forEach(usage -> {
		Filter<CtInvocation<?>> filter = new BasicFilter<CtInvocation<?>>(CtInvocation.class) {
			@Override
			public boolean matches(final CtInvocation<?> element) {
				return WidgetHelper.INSTANCE.ACTION_CMD_METHOD_NAMES.stream().anyMatch(elt -> element.getExecutable().getSimpleName().equals(elt));
			}
		};

		// Getting all the set action command and co statements.
		List<CtStatement> actionCmds = usage.accesses.stream().map(access -> access.getParent(CtStatement.class)).
			filter(stat -> stat != null && !stat.getElements(filter).isEmpty()).collect(Collectors.toList());

		// Deleting each set action command and co statement.
		actionCmds.forEach(stat -> {
			LOG.log(Level.INFO, () -> cmd + ": removing setActionCmd: " + stat);
			stat.delete();
			collectRefactoredType(stat);
		});

		// Deleting the unused private/protected/package action command names defined as constants or variables (analysing the
		// usage of public variables is time-consuming).
		actionCmds.stream().filter(cmds -> cmds instanceof CtInvocation<?>).
			// Considering the arguments of the invocation only.
			map(invok -> ((CtInvocation<?>)invok).getArguments()).flatMap(s -> s.stream()).
			map(arg -> arg.getElements(new VariableAccessFilter())).flatMap(s -> s.stream()).
			map(access -> access.getVariable().getDeclaration()).distinct().
			filter(var -> var!=null && (var.getVisibility()==ModifierKind.PRIVATE || var.getVisibility()==ModifierKind.PROTECTED ||
				var.getVisibility()==null) && SpoonHelper.INSTANCE.extractUsagesOfVar(var).size()<2).
			forEach(var -> {
				LOG.log(Level.INFO, () -> cmd + ": removing action cmd names: " + var);
				var.delete();
				collectRefactoredType(var);
			});
	});
}
 
开发者ID:diverse-project,项目名称:InspectorGuidget,代码行数:37,代码来源:ListenerCommandRefactor.java

示例12: changeNonLocalFieldAccesses

import spoon.reflect.visitor.Filter; //导入依赖的package包/类
private void changeNonLocalFieldAccesses(final @NotNull List<CtElement> stats, final @NotNull CtAbstractInvocation<?> regInvok) {
	final Filter<CtFieldRead<?>> filter = new BasicFilter<>(CtFieldRead.class);
	// Getting the class where the listener is registered.
	final CtType<?> listenerRegClass = regInvok.getParent(CtType.class);
	final List<CtFieldRead<?>> nonLocalFieldAccesses =
		// Getting all the invocations used in the statements.
		stats.stream().map(stat -> stat.getElements(filter)).flatMap(s -> s.stream()).
			// Keeping the invocations that are on fields
				filter(f -> f.getVariable().getDeclaration()!=null &&
				// Keeping the invocations that calling fields are not part of the class that registers the listener.
				f.getVariable().getDeclaration().getParent(CtType.class) != listenerRegClass).
			collect(Collectors.toList());

	// The invocation may refer to a method that is defined in the class where the registration occurs.
	nonLocalFieldAccesses.stream().filter(field -> {
		try {
			return field.getType().getDeclaration() == listenerRegClass;
		}catch(final ParentNotInitializedException ex) {
			return false;
		}
	}).
		// In this case, the target of the invocation (the field read) is removed since the invocation will be moved to
		// the registration class.
		forEach(f -> {
			LOG.log(Level.INFO, () -> cmd + ": removing a field access by its target: " + f);
			f.replace(f.getTarget());
		});
}
 
开发者ID:diverse-project,项目名称:InspectorGuidget,代码行数:29,代码来源:ListenerCommandRefactor.java

示例13: filteredElements

import spoon.reflect.visitor.Filter; //导入依赖的package包/类
public static <T extends CtElement> List<T> filteredElements(Factory factory, Filter<T> filter) {
    return Query.getElements(factory, filter);
}
 
开发者ID:SpoonLabs,项目名称:nopol,代码行数:4,代码来源:SpoonElementLibrary.java

示例14: generateMutants

import spoon.reflect.visitor.Filter; //导入依赖的package包/类
/** returns a list of mutant classes */
public void generateMutants() {
	Launcher l = new Launcher();
	l.addInputResource(sourceCodeToBeMutated);
	l.buildModel();

	CtClass origClass = (CtClass) l.getFactory().Package().getRootPackage()
			.getElements(new TypeFilter(CtClass.class)).get(0);

	// now we apply a transformation
	// we replace "+" and "*" by "-"
	List<CtElement> elementsToBeMutated = origClass.getElements(new Filter<CtElement>() {

		@Override
		public boolean matches(CtElement arg0) {
			return mutator.isToBeProcessed(arg0);
		}
	});
	
	for (CtElement e : elementsToBeMutated) {
		// this loop is the trickiest part
		// because we want one mutation after the other
		
		// cloning the AST element
		CtElement op = l.getFactory().Core().clone(e);
		
		// mutate the element
		mutator.process(op);
		
		// temporarily replacing the original AST node with the mutated element 
		replace(e,op);

		// creating a new class containing the mutating code
		CtClass klass = l.getFactory().Core()
				.clone(op.getParent(CtClass.class));
		// setting the package
		klass.setParent(origClass.getParent());

		// adding the new mutant to the list
		mutants.add(klass);

		// restoring the original code
		replace(op, e);
	}
}
 
开发者ID:SpoonLabs,项目名称:spoon-examples,代码行数:46,代码来源:MutationTester.java

示例15: example

import spoon.reflect.visitor.Filter; //导入依赖的package包/类
@Test
public void example() throws Exception {
 Launcher l = new Launcher();
 
 // required for having IFoo.class in the classpath in Maven
 l.setArgs(new String[] {"--source-classpath","target/test-classes"});
 
 l.addInputResource("src/test/resources/transformation/");
 l.buildModel();
 
 CtClass foo = l.getFactory().Package().getRootPackage().getElements(new NamedElementFilter<>(CtClass.class, "Foo1")).get(0);

 // compiling and testing the initial class
 Class<?> fooClass = InMemoryJavaCompiler.newInstance().compile(foo.getQualifiedName(), "package "+foo.getPackage().getQualifiedName()+";"+foo.toString());
 IFoo x = (IFoo) fooClass.newInstance();
 // testing its behavior
 assertEquals(5, x.m());

 // now we apply a transformation
 // we replace "+" by "-"
 for(Object e : foo.getElements(new TypeFilter(CtBinaryOperator.class))) {
  CtBinaryOperator op = (CtBinaryOperator)e;
  if (op.getKind()==BinaryOperatorKind.PLUS) {
	  op.setKind(BinaryOperatorKind.MINUS);
  }
 }
 
 // first assertion on the results of the transfo
 
 // there are no more additions in the code
 assertEquals(0, foo.getElements(new Filter<CtBinaryOperator<?>>() {
@Override
public boolean matches(CtBinaryOperator<?> arg0) {
	return arg0.getKind()==BinaryOperatorKind.PLUS;
}		  
 }).size());

 // second assertions on the behavior of the transformed code
 
 // compiling and testing the transformed class
 fooClass = InMemoryJavaCompiler.newInstance().compile(foo.getQualifiedName(), "package "+foo.getPackage().getQualifiedName()+";"+foo.toString());
 IFoo y = (IFoo) fooClass.newInstance();
 // testing its behavior with subtraction
 assertEquals(1, y.m());
 
 System.out.println("yes y.m()="+y.m());
}
 
开发者ID:SpoonLabs,项目名称:spoon-examples,代码行数:48,代码来源:OnTheFlyTransfoTest.java


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