Java Code Examples for spoon.reflect.declaration.CtAnnotation

The following examples show how to use spoon.reflect.declaration.CtAnnotation. These examples are extracted from open source projects. 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 Project: camunda-bpm-swagger   Source File: RestResourceProcessor.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void process(final CtInterface<?> resourceInterface) {
  final CtAnnotation<Annotation> annotation = getFactory().Code().createAnnotation(getFactory().Code().createCtTypeReference(Api.class));
  final RestService restService = context.getServiceDocumentation().get(resourceInterface.getQualifiedName());
  if (restService != null) {
    String[] tags;
    if (restService.getTags().contains(",")) {
      tags = restService.getTags().split(",");
    } else {
      tags = new String[]{ restService.getTags() };
    }

  annotation
    // .addValue("tags", tags)
    .addValue("hidden", true);
  resourceInterface.addAnnotation(annotation);
} else {
    log.error("No documentation for resource {} found.", resourceInterface.getQualifiedName());
  }
}
 
Example 2
@Override
public void process(final CtMethod<?> method) {
  final CtAnnotation<Annotation> annotation = getFactory().Code().createAnnotation(getFactory().Code().createCtTypeReference(ApiModelProperty.class));

  final String fieldName = uncapitalize(removeStart(method.getSimpleName(), "get"));
  final String classFqn = TypeHelper.getClassname(method);

  final Map<String, ParameterDescription> docs = context.getDtoDocumentation().get(classFqn);
  if (docs != null) {
    final ParameterDescription parameterDescription = docs.get(fieldName);
    if (parameterDescription != null) {
      log.debug("Found parameter description for {} {}", classFqn, fieldName);
      annotation.addValue("value", parameterDescription.getDescription());
      if (parameterDescription.getRequired() != null) {
        annotation.addValue("required", parameterDescription.getRequired().booleanValue());
      }
    }
  }

  annotation.addValue("name", fieldName);

  method.addAnnotation(annotation);
}
 
Example 3
/**
 * get all dependencies added by a CtTypedElement
 *
 * @param element
 * @return all dependencies added by element
 */
private List<CtTypeReference<?>> getDependencies(CtTypedElement<?> element) {
    List<CtTypeReference<?>> listDependencies = new ArrayList<>();
    // Literal
    if (element instanceof CtAnnotation) {
        return listDependencies;
    }
    if (element instanceof CtLiteral<?>) {
        CtLiteral<?> literal = (CtLiteral<?>) element;
        literal.getValue();
        if (literal.getValue() instanceof CtTypeReference<?>) {
            listDependencies.add((CtTypeReference<?>) literal.getValue());
        } else if (literal.getValue() instanceof CtTypedElement<?>) {
            listDependencies.add(((CtTypedElement<?>) literal.getValue())
                    .getType());
        }
    }
    // method invocation
    if (element instanceof CtInvocation<?>) {
        CtInvocation<?> invocation = (CtInvocation<?>) element;
        // the class of the method
        listDependencies.add(invocation.getExecutable().getDeclaringType());
    }
    listDependencies.add(element.getType());
    return listDependencies;
}
 
Example 4
Source Project: nopol   Source File: ClassCollector.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * get all dependencies added by a CtTypedElement
 *
 * @param element
 * @return all dependencies added by element
 */
private List<CtTypeReference<?>> getDependencies(CtTypedElement<?> element) {
    List<CtTypeReference<?>> listDependencies = new ArrayList<>();
    // Literal
    if (element instanceof CtAnnotation) {
        return listDependencies;
    }
    if (element instanceof CtLiteral<?>) {
        CtLiteral<?> literal = (CtLiteral<?>) element;
        literal.getValue();
        if (literal.getValue() instanceof CtTypeReference<?>) {
            listDependencies.add((CtTypeReference<?>) literal.getValue());
        } else if (literal.getValue() instanceof CtTypedElement<?>) {
            listDependencies.add(((CtTypedElement<?>) literal.getValue())
                    .getType());
        }
    }
    // method invocation
    if (element instanceof CtInvocation<?>) {
        CtInvocation<?> invocation = (CtInvocation<?>) element;
        // the class of the method
        listDependencies.add(invocation.getExecutable().getDeclaringType());
    }
    listDependencies.add(element.getType());
    return listDependencies;
}
 
Example 5
Source Project: nopol   Source File: TestSelectionProcessor.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean isToBeProcessed(CtMethod element){

    boolean isTest = false;
    for(CtAnnotation<? extends Annotation> annotation : element.getAnnotations()){
        if (annotation.getType().equals(annotation.getFactory().Annotation().createReference(org.junit.Test.class))) {
            isTest = true;
        }
    }

    if(!isTest){
        return false; //keep this one
    }

    for(CtMethod<?> method : keptMethods){
        if(element.getSignature().equals(method.getSignature())){
            return false; //keep this one
        }
    }

    return super.isToBeProcessed(element);
}
 
Example 6
Source Project: camunda-bpm-swagger   Source File: ApiModelProcessor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void process(final CtClass<?> element) {

  final CtAnnotation<Annotation> annotation = getFactory().Code().createAnnotation(getFactory().Code().createCtTypeReference(ApiModel.class));
  element.addAnnotation(annotation);
  log.debug("Add ApiModel to {}", element.getQualifiedName());
}
 
Example 7
private boolean isTestCase(CtMethod<?> method) {
    boolean isNamedTest = method.getSimpleName().toLowerCase().endsWith("test"); // to detect TestCase under JUnit 3.x
    boolean hasTestAnnotation = false; // to detect TestCase under JUnit 4.x
    List<CtAnnotation<?>> listAnnotation = method.getAnnotations();
    for (CtAnnotation<?> tmp : listAnnotation) {
        if (tmp.getType().equals(method.getFactory().Annotation().createReference(org.junit.Test.class))) {
            hasTestAnnotation = true;
        }
    }

    return isNamedTest || hasTestAnnotation;
}
 
Example 8
Source Project: nopol   Source File: Main.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * this method analyze all java class in generatedTestDir and return a list of all Junit method
 * @param generatedTestFolder
 * @param classPath
 * @return List<CtMethod> list of methods
 */
public static List<CtMethod<?>> getNewTestsMethods(String generatedTestFolder, String classPath){
    List<CtMethod<?>>  testsMethods = new ArrayList<CtMethod<?>>();

    logger.debug("--------------------------------------------------");
    logger.debug(" ##### Search tests methods ##### ");
    Launcher spoon = new Launcher();
    spoon.getEnvironment().setAutoImports(true);
    spoon.addInputResource(generatedTestFolder);
    spoon.getEnvironment().setSourceClasspath(classPath.split(File.pathSeparator));
    spoon.buildModel();
    //getannotatedMethod.. could be better
    for(CtType<?> clazz : spoon.getFactory().Class().getAll()){
        methodLoop:
            for(CtMethod<?> method : clazz.getAllMethods()){
                for(CtAnnotation<? extends Annotation> annotation : method.getAnnotations()){
                    if (annotation.getType().equals(method.getFactory().Annotation().createReference(org.junit.Test.class))) {
                        logger.debug("[FOUND] "+method.getSignature());
                        testsMethods.add(method);
                        continue methodLoop;
                    }
                }
            }
    }

    return testsMethods;
}