Java Code Examples for spoon.reflect.declaration.CtClass#getElements()

The following examples show how to use spoon.reflect.declaration.CtClass#getElements() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LibParser.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void parseFile(File name) {
   	
   	if(!parsedfile.contains(name.getAbsolutePath())) {
   	  parsedfile.add(name.getAbsolutePath());
   	  
	  for (CtClass<?> klass : this.factory.getModel().getElements(new TypeFilter<CtClass>(CtClass.class))) {
		if(klass.getPosition().getFile().getAbsolutePath().equals(name.getAbsolutePath())) {
			@SuppressWarnings("rawtypes")
			CtClass founded=klass;
			if (!classMap.containsKey(founded.getQualifiedName())) {
				classMap.put(founded.getQualifiedName(), founded);
				List<CtEnum> enumArray = founded.getElements(new TypeFilter<>(CtEnum.class));
                   for(int index=0; index<enumArray.size(); index++) {
                   	enumMap.put(enumArray.get(index).getQualifiedName(), enumArray.get(index));
                   }
			}			
			break;
		}
	  }
      }
}
 
Example 2
Source File: TypeaccessAnalyzer.java    From coming with MIT License 5 votes vote down vote up
private void analyzeC3C4_SimilarTypeAccessActualVar(CtElement element, Cntx<Object> context, 
		List<CtTypeAccess> typeaccessaaffected, CtClass parentClass) {
	
	try {
		List<CtTypeAccess> typeaccesss = new ArrayList();
		if(parentClass!=null)
		    typeaccesss = parentClass.getElements(new TypeFilter<>(CtTypeAccess.class));

		for (CtTypeAccess virtualtypeaccess : typeaccessaaffected) {
			
			boolean c3CurrentOtherTypeAccessActualVar = false;
			boolean c4CurrentOtherSimilarTypeAccessActualVar = false;
			
			if(isTypeAccessActualVar(virtualtypeaccess)) {
				c3CurrentOtherTypeAccessActualVar=true;
				
				for(CtTypeAccess certaintypeaccess: typeaccesss) {
					if(isTypeAccessActualVar(certaintypeaccess)) {
						if(whetherSimilarTypeAccessActualVar(virtualtypeaccess, certaintypeaccess)) {
							c4CurrentOtherSimilarTypeAccessActualVar=true;
							break;
						}
					}
				}
			}
			
			writeGroupedInfo(context, adjustIdentifyInJson(virtualtypeaccess), CodeFeatures.C3_TYPEACCESS_ACTUAL_VAR,
					c3CurrentOtherTypeAccessActualVar, "FEATURES_TYPEACCESS");
			
			writeGroupedInfo(context, adjustIdentifyInJson(virtualtypeaccess), CodeFeatures.C4_SIMILAR_TYPEACCESS_ACTUAL_VAR,
					c4CurrentOtherSimilarTypeAccessActualVar, "FEATURES_TYPEACCESS");
		}

	} catch (Throwable e) {
		e.printStackTrace();
	}	
}
 
Example 3
Source File: VariableAnalyzer.java    From coming with MIT License 5 votes vote down vote up
private void analyzeV17_IsEnum (List<CtVariableAccess> varsAffected, Cntx<Object> context, CtClass parentClass) {
	try {
		
		if (parentClass == null)
			return;
		// Get all enums
		List<CtEnum> enums = parentClass.getElements(new TypeFilter<>(CtEnum.class));

		// For each var access
		for (CtVariableAccess varAccess : varsAffected) {
			
			boolean isVarAccessTypeEnum = false;
			
			if (varAccess.getVariable().getType() != null
					&& enums.contains(varAccess.getVariable().getType().getDeclaration())) {
				isVarAccessTypeEnum = true;
			}
			
			writeGroupedInfo(context, adjustIdentifyInJson(varAccess), 
					CodeFeatures.V17_VAR_IS_ENUMERATION,
					(isVarAccessTypeEnum), "FEATURES_VARS");
		}

	} catch (Throwable e) {
		e.printStackTrace();
	}
}
 
Example 4
Source File: OnTheFlyTransfoTest.java    From spoon-examples with GNU General Public License v2.0 4 votes vote down vote up
@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());
}
 
Example 5
Source File: MutationTester.java    From spoon-examples with GNU General Public License v2.0 4 votes vote down vote up
/** 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);
	}
}
 
Example 6
Source File: ConstructorAnalyzer.java    From coming with MIT License 4 votes vote down vote up
private void analyzeCon1_ConstructorOverload(CtElement element, Cntx<Object> context, CtClass parentClass,
		 List<CtConstructorCall> constructorcalls) {
	
	try {
		for (CtConstructorCall constructorcall : constructorcalls) {

			boolean con1SpecificHasSameName = false;
			
			List<CtConstructor> allconstructorsinclass = new ArrayList();
			if(parentClass!=null)
			     allconstructorsinclass = parentClass.getElements(new TypeFilter<>(CtConstructor.class));

			for (CtConstructor certainconstructorinclass : allconstructorsinclass) {

					CtConstructor anotherConstructor = (CtConstructor) certainconstructorinclass;
					// Ignoring if it's the same
					if (anotherConstructor == null || anotherConstructor.getSignature().
							equals(constructorcall.getExecutable().getSignature()))
						continue;

					if (anotherConstructor.getSimpleName().equals(getSimplenameForConstructorCall(constructorcall))) {
						// It's override
						con1SpecificHasSameName = true;
						break;
					}
			}
			
			List<CtConstructorCall> allconstructorcallsinclass = new ArrayList();

			if(parentClass!=null)
			     allconstructorcallsinclass = parentClass.getElements(new TypeFilter<>(CtConstructorCall.class));

		    if(!con1SpecificHasSameName) {
		    	
		    	for (CtConstructorCall certainconstructorcallinclass : allconstructorcallsinclass) {

		    		CtConstructorCall anotherConstructorCall = (CtConstructorCall) certainconstructorcallinclass;
					if (anotherConstructorCall == null || anotherConstructorCall.getExecutable().getSignature().
							equals(constructorcall.getExecutable().getSignature()))
						continue;

					if (getSimplenameForConstructorCall(anotherConstructorCall).equals(getSimplenameForConstructorCall(constructorcall))) {
						con1SpecificHasSameName = true;
						break;
					}
			    }
		    }
		    
		    writeGroupedInfo(context, adjustIdentifyInJson(constructorcall), CodeFeatures.CON1_OVERLOADED_CONSTRUCTOR,
		    		con1SpecificHasSameName, "FEATURES_CONSTRUCTOR");
		} 		
	} catch (Throwable e) {
		e.printStackTrace();
	}
}