com.github.javaparser.ast.expr.SingleMemberAnnotationExpr Java Examples

The following examples show how to use com.github.javaparser.ast.expr.SingleMemberAnnotationExpr. 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: RegistryMethodVisitor.java    From gauge-java with Apache License 2.0 6 votes vote down vote up
private void addStepToRegistry(Expression expression, MethodDeclaration methodDeclaration, SingleMemberAnnotationExpr annotation) {
    String parameterizedStep = getParameterizedStep(expression);
    String stepText = new StepsUtil().getStepText(parameterizedStep);
    stepValue = new StepValue(stepText, parameterizedStep);

    entry = new StepRegistryEntry();
    entry.setName(methodDeclaration.getDeclarationAsString());
    String className = getClassName(methodDeclaration);
    String fullyQualifiedName = className == null
            ? methodDeclaration.getNameAsString() : className + "." + methodDeclaration.getNameAsString();
    entry.setFullyQualifiedName(fullyQualifiedName);
    entry.setStepText(parameterizedStep);
    entry.setStepValue(stepValue);
    entry.setParameters(methodDeclaration.getParameters());
    entry.setSpan(methodDeclaration.getRange().get());
    entry.setHasAlias(hasAlias(annotation));
    entry.setAliases(getAliases(annotation));
    entry.setFileName(file);

    stepRegistry.addStep(stepValue, entry);
}
 
Example #2
Source File: RegistryMethodVisitor.java    From gauge-java with Apache License 2.0 6 votes vote down vote up
@Override
public void visit(MethodDeclaration methodDeclaration, Object arg) {
    List<AnnotationExpr> annotations = methodDeclaration.getAnnotations();
    if (annotations.isEmpty()) {
        return;
    }

    for (AnnotationExpr annotationExpr : annotations) {
        if (!(annotationExpr instanceof SingleMemberAnnotationExpr)) {
            continue;
        }
        SingleMemberAnnotationExpr annotation = (SingleMemberAnnotationExpr) annotationExpr;
        if (annotation.getMemberValue() instanceof ArrayInitializerExpr) {
            ArrayInitializerExpr memberValue = (ArrayInitializerExpr) annotation.getMemberValue();
            for (Expression expression : memberValue.getValues()) {
                addStepToRegistry(expression, methodDeclaration, annotation);
            }
        } else {
            addStepToRegistry(annotation.getMemberValue(), methodDeclaration, annotation);
        }
    }
}
 
Example #3
Source File: RegistryMethodVisitor.java    From gauge-java with Apache License 2.0 5 votes vote down vote up
private List<String> getAliases(SingleMemberAnnotationExpr annotation) {
    List<String> aliases = new ArrayList<>();
    if (annotation.getMemberValue() instanceof ArrayInitializerExpr) {
        ArrayInitializerExpr memberValue = (ArrayInitializerExpr) annotation.getMemberValue();
        for (Expression expression : memberValue.getValues()) {
            aliases.add(getParameterizedStep(expression));
        }
    }
    return aliases;
}
 
Example #4
Source File: DependencyInjectionAnnotator.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
/**
 * Annotates given node with set of roles to enforce security
 *
 * @param node  node to be annotated
 * @param roles roles that are allowed
 */
default <T extends NodeWithAnnotations<?>> T withSecurityRoles(T node, String[] roles) {
    if (roles != null && roles.length > 0) {
        List<Expression> rolesExpr = new ArrayList<>();

        for (String role : roles) {
            rolesExpr.add(new StringLiteralExpr(role.trim()));
        }

        node.addAnnotation(new SingleMemberAnnotationExpr(new Name("javax.annotation.security.RolesAllowed"), new ArrayInitializerExpr(NodeList.nodeList(rolesExpr))));
    }
    return node;
}
 
Example #5
Source File: OpenApiObjectGenerator.java    From flow with Apache License 2.0 5 votes vote down vote up
private String getEndpointName(ClassOrInterfaceDeclaration classDeclaration,
        AnnotationExpr endpointAnnotation) {
    String endpointName = Optional.ofNullable(endpointAnnotation)
            .filter(Expression::isSingleMemberAnnotationExpr)
            .map(Expression::asSingleMemberAnnotationExpr)
            .map(SingleMemberAnnotationExpr::getMemberValue)
            .map(Expression::asStringLiteralExpr)
            .map(LiteralStringValueExpr::getValue)
            .filter(GeneratorUtils::isNotBlank)
            .orElse(classDeclaration.getNameAsString());

    // detect the endpoint value name
    if (endpointName.equals(classDeclaration.getNameAsString()) && endpointAnnotation != null) {
        String endpointValueName = getParameterValueFromAnnotation(endpointAnnotation, "value");
        if (endpointValueName != null) {
            endpointName = endpointValueName.substring(1, endpointValueName.length() - 1);
        }
    }

    String validationError = endpointNameChecker.check(endpointName);
    if (validationError != null) {
        throw new IllegalStateException(
                String.format("Endpoint name '%s' is invalid, reason: '%s'",
                        endpointName, validationError));
    }
    return endpointName;
}
 
Example #6
Source File: PrettyPrintVisitor.java    From stategen with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void visit(final SingleMemberAnnotationExpr n, final Void arg) {
    printJavaComment(n.getComment(), arg);
    printer.print("@");
    n.getName().accept(this, arg);
    printer.print("(");
    n.getMemberValue().accept(this, arg);
    printer.print(")");
}
 
Example #7
Source File: JavaEnumBuilderImpl.java    From chrome-devtools-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * Adds new enum constant.
 *
 * @param name Constant name.
 * @param value Real constant value.
 */
public void addEnumConstant(String name, String value) {
  EnumConstantDeclaration enumConstantDeclaration = new EnumConstantDeclaration(name);

  SingleMemberAnnotationExpr jsonPropertyAnnotation = new SingleMemberAnnotationExpr();
  jsonPropertyAnnotation.setName(JSON_PROPERTY);
  jsonPropertyAnnotation.setMemberValue(new StringLiteralExpr(value));
  enumConstantDeclaration.addAnnotation(jsonPropertyAnnotation);

  declaration.addEntry(enumConstantDeclaration);
}
 
Example #8
Source File: JFinalControllerParser.java    From JApiDocs with Apache License 2.0 5 votes vote down vote up
@Override
protected void afterHandleMethod(RequestNode requestNode, MethodDeclaration md) {
    String methodName = md.getNameAsString();
    requestNode.setUrl(getUrl(methodName));
    md.getAnnotationByName("ActionKey").ifPresent(an -> {
        if(an instanceof SingleMemberAnnotationExpr){
            String url = ((SingleMemberAnnotationExpr)an).getMemberValue().toString();
            requestNode.setMethod(Arrays.asList(RequestMethod.GET.name(), RequestMethod.POST.name()));
            requestNode.setUrl(Utils.removeQuotations(url));
            return;
        }
    });
}
 
Example #9
Source File: SingleMemberAnnotationExprMerger.java    From dolphin with Apache License 2.0 5 votes vote down vote up
@Override
public SingleMemberAnnotationExpr doMerge(SingleMemberAnnotationExpr first, SingleMemberAnnotationExpr second) {
  SingleMemberAnnotationExpr smae = new SingleMemberAnnotationExpr();
  smae.setName(mergeSingle(first.getName(),second.getName()));
  smae.setMemberValue(mergeSingle(first.getMemberValue(),second.getMemberValue()));
  return smae;
}
 
Example #10
Source File: TraceVisitor.java    From JCTools with Apache License 2.0 4 votes vote down vote up
@Override
public void visit(SingleMemberAnnotationExpr n, Void arg) {
    out.println("SingleMemberAnnotationExpr: " + (extended ? n : ""));
    super.visit(n, arg);
}
 
Example #11
Source File: CDIDependencyInjectionAnnotator.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Override
public <T extends NodeWithAnnotations<?>> T withNamed(T node, String name) {
    node.addAnnotation(new SingleMemberAnnotationExpr(new Name("javax.inject.Named"), new StringLiteralExpr(name)));
    return node;
}
 
Example #12
Source File: RegistryMethodVisitor.java    From gauge-java with Apache License 2.0 4 votes vote down vote up
private Boolean hasAlias(SingleMemberAnnotationExpr annotation) {
    return annotation.getMemberValue() instanceof ArrayInitializerExpr;
}
 
Example #13
Source File: SingleMemberAnnotationExprMerger.java    From dolphin with Apache License 2.0 4 votes vote down vote up
@Override
public boolean doIsEquals(SingleMemberAnnotationExpr first, SingleMemberAnnotationExpr second) {

  return first.getName().equals(second.getName()) && first.getMemberValue().equals(second.getMemberValue());
}
 
Example #14
Source File: DMNRestResourceGenerator.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public String generate() {
    CompilationUnit clazz = parse(this.getClass().getResourceAsStream("/class-templates/DMNRestResourceTemplate.java"));
    clazz.setPackageDeclaration(this.packageName);

    ClassOrInterfaceDeclaration template = clazz
            .findFirst(ClassOrInterfaceDeclaration.class)
            .orElseThrow(() -> new NoSuchElementException("Compilation unit doesn't contain a class or interface declaration!"));

    template.setName(resourceClazzName);

    template.findAll(StringLiteralExpr.class).forEach(this::interpolateStrings);
    template.findAll(MethodDeclaration.class).forEach(this::interpolateMethods);

    interpolateInputType(template);

    if (useInjection()) {
        template.findAll(FieldDeclaration.class,
                         CodegenUtils::isApplicationField).forEach(fd -> annotator.withInjection(fd));
    } else {
        template.findAll(FieldDeclaration.class,
                         CodegenUtils::isApplicationField).forEach(this::initializeApplicationField);
    }

    MethodDeclaration dmnMethod = template.findAll(MethodDeclaration.class, x -> x.getName().toString().equals("dmn")).get(0);
    for (DecisionService ds : dmnModel.getDefinitions().getDecisionService()) {
        if (ds.getAdditionalAttributes().keySet().stream().anyMatch(qn -> qn.getLocalPart().equals("dynamicDecisionService"))) {
            continue;
        }

        MethodDeclaration clonedMethod = dmnMethod.clone();
        String name = CodegenStringUtil.escapeIdentifier("decisionService_" + ds.getName());
        clonedMethod.setName(name);
        MethodCallExpr evaluateCall = clonedMethod.findFirst(MethodCallExpr.class, x -> x.getNameAsString().equals("evaluateAll")).orElseThrow(() -> new RuntimeException("Template was modified!"));
        evaluateCall.setName(new SimpleName("evaluateDecisionService"));
        evaluateCall.addArgument(new StringLiteralExpr(ds.getName()));
        clonedMethod.addAnnotation(new SingleMemberAnnotationExpr(new Name("javax.ws.rs.Path"), new StringLiteralExpr("/" + ds.getName())));
        ReturnStmt returnStmt = clonedMethod.findFirst(ReturnStmt.class).orElseThrow(() -> new RuntimeException("Template was modified!"));
        if (ds.getOutputDecision().size() == 1) {
            MethodCallExpr rewrittenReturnExpr = returnStmt.findFirst(MethodCallExpr.class,
                                                                      mce -> mce.getNameAsString().equals("extractContextIfSucceded"))
                                                           .orElseThrow(() -> new RuntimeException("Template was modified!"));
            rewrittenReturnExpr.setName("extractSingletonDSIfSucceded");
        }

        if (useMonitoring) {
            addMonitoringToMethod(clonedMethod, ds.getName());
        }

        template.addMember(clonedMethod);
    }

    if (useMonitoring) {
        addMonitoringImports(clazz);
        ClassOrInterfaceDeclaration exceptionClazz = clazz.findFirst(ClassOrInterfaceDeclaration.class, x -> "DMNEvaluationErrorExceptionMapper".equals(x.getNameAsString()))
                .orElseThrow(() -> new NoSuchElementException("Could not find DMNEvaluationErrorExceptionMapper, template has changed."));
        addExceptionMetricsLogging(exceptionClazz, nameURL);
        addMonitoringToMethod(dmnMethod, nameURL);
    }

    template.getMembers().sort(new BodyDeclarationComparator());
    return clazz.toString();
}
 
Example #15
Source File: SpringDependencyInjectionAnnotator.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Override
public <T extends NodeWithAnnotations<?>> T withConfigInjection(T node, String configKey, String defaultValue) {
    node.addAnnotation(new SingleMemberAnnotationExpr(new Name("org.springframework.beans.factory.annotation.Value"), new StringLiteralExpr("${" + configKey + ":" + defaultValue + "}")));
    return node;
}
 
Example #16
Source File: SpringDependencyInjectionAnnotator.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Override
public <T extends NodeWithAnnotations<?>> T withConfigInjection(T node, String configKey) {
    node.addAnnotation(new SingleMemberAnnotationExpr(new Name("org.springframework.beans.factory.annotation.Value"), new StringLiteralExpr("${" + configKey + ":#{null}}")));
    return node;
}
 
Example #17
Source File: SpringDependencyInjectionAnnotator.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Override
public <T extends NodeWithAnnotations<?>> T withNamedApplicationComponent(T node, String name) {
    node.addAnnotation(new SingleMemberAnnotationExpr(new Name("org.springframework.stereotype.Component"), new StringLiteralExpr(name)));
    return node;
}
 
Example #18
Source File: SpringDependencyInjectionAnnotator.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Override
public <T extends NodeWithAnnotations<?>> T withNamed(T node, String name) {
    node.addAnnotation(new SingleMemberAnnotationExpr(new Name("org.springframework.beans.factory.annotation.Qualifier"), new StringLiteralExpr(name)));
    return node;
}
 
Example #19
Source File: CDIDependencyInjectionAnnotator.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Override
public <T extends NodeWithAnnotations<?>> T withOutgoingMessage(T node, String channel) {
    node.addAnnotation(new SingleMemberAnnotationExpr(new Name("io.smallrye.reactive.messaging.annotations.Channel"), new StringLiteralExpr(channel)));
    return node;
}
 
Example #20
Source File: CDIDependencyInjectionAnnotator.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Override
public <T extends NodeWithAnnotations<?>> T withIncomingMessage(T node, String channel) {
    node.addAnnotation(new SingleMemberAnnotationExpr(new Name("org.eclipse.microprofile.reactive.messaging.Incoming"), new StringLiteralExpr(channel)));
    return node;
}