Search in sources :

Example 91 with ClassOrInterfaceDeclaration

use of com.github.javaparser.ast.body.ClassOrInterfaceDeclaration in project meghanada-server by mopemope.

the class ParameterNameVisitor method visit.

@Override
public void visit(ClassOrInterfaceDeclaration n, Object arg) {
    super.visit(n, arg);
    final EnumSet<Modifier> modifiers = n.getModifiers();
    if (!modifiers.contains(Modifier.PRIVATE)) {
        final List<BodyDeclaration<?>> members = n.getMembers();
        final SimpleName simpleName = n.getName();
        final String clazz = simpleName.getId();
        // String clazz = n.getName();
        this.className = this.pkg + '.' + clazz;
        log.debug("class {}", this.className);
        int i = 0;
        for (final BodyDeclaration<?> body : members) {
            if (body instanceof MethodDeclaration) {
                MethodDeclaration methodDeclaration = (MethodDeclaration) body;
                this.getParameterNames(methodDeclaration, n.isInterface());
                i++;
            } else if (body instanceof ConstructorDeclaration) {
            // Constructor
            } else if (body instanceof ClassOrInterfaceDeclaration) {
                final ClassOrInterfaceDeclaration classOrInterfaceDeclaration = (ClassOrInterfaceDeclaration) body;
                String name = classOrInterfaceDeclaration.getName().getIdentifier();
                String key = this.pkg + '.' + name;
                name = this.originClassName + '.' + name;
                for (MethodParameterNames mpn : this.parameterNamesList) {
                    if (mpn != null && mpn.className != null && mpn.className.equals(key)) {
                        mpn.className = name;
                    }
                }
            }
        }
        if (i > 0 && this.names.className != null) {
            this.parameterNamesList.add(this.names);
            this.names = new MethodParameterNames();
        }
    }
}
Also used : ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) SimpleName(com.github.javaparser.ast.expr.SimpleName) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) Modifier(com.github.javaparser.ast.Modifier)

Example 92 with ClassOrInterfaceDeclaration

use of com.github.javaparser.ast.body.ClassOrInterfaceDeclaration in project actframework by actframework.

the class ApiManager method exploreDeclaration.

private void exploreDeclaration(ClassOrInterfaceDeclaration classDeclaration, Map<String, Javadoc> methodJavaDocs, String prefix) {
    String className = classDeclaration.getName();
    String newPrefix = S.blank(prefix) ? className : S.concat(prefix, ".", className);
    for (Node node : classDeclaration.getChildrenNodes()) {
        if (node instanceof ClassOrInterfaceDeclaration) {
            exploreDeclaration((ClassOrInterfaceDeclaration) node, methodJavaDocs, newPrefix);
        } else if (node instanceof MethodDeclaration) {
            MethodDeclaration methodDeclaration = (MethodDeclaration) node;
            List<AnnotationExpr> annoList = methodDeclaration.getAnnotations();
            boolean needJavadoc = false;
            if (null != annoList && !annoList.isEmpty()) {
                for (AnnotationExpr anno : annoList) {
                    String annoName = anno.getName().getName();
                    if (actionAnnotations.contains(annoName)) {
                        needJavadoc = true;
                        break;
                    }
                }
            }
            if (!needJavadoc) {
                continue;
            }
            Comment comment = methodDeclaration.getComment();
            if (!(comment instanceof JavadocComment)) {
                continue;
            }
            JavadocComment javadocComment = (JavadocComment) comment;
            Javadoc javadoc = JavadocParser.parse(javadocComment);
            methodJavaDocs.put(S.concat(newPrefix, ".", methodDeclaration.getName()), javadoc);
        }
    }
}
Also used : JavadocComment(com.github.javaparser.ast.comments.JavadocComment) Comment(com.github.javaparser.ast.comments.Comment) JavadocComment(com.github.javaparser.ast.comments.JavadocComment) AnnotationExpr(com.github.javaparser.ast.expr.AnnotationExpr) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Node(com.github.javaparser.ast.Node) Javadoc(act.apidoc.javadoc.Javadoc)

Example 93 with ClassOrInterfaceDeclaration

use of com.github.javaparser.ast.body.ClassOrInterfaceDeclaration in project drools by kiegroup.

the class AccumulateInline method initInlineAccumulateTemplate.

private void initInlineAccumulateTemplate() {
    accumulateInlineClassName = StringUtil.toId(context.getRuleDescr().getName()) + "Accumulate" + accumulateDescr.getLine();
    CompilationUnit templateCU;
    try {
        templateCU = StaticJavaParser.parseResource("AccumulateInlineTemplate.java");
    } catch (IOException e) {
        throw new InvalidInlineTemplateException(e);
    }
    ClassOrInterfaceDeclaration parsedClass = templateCU.getClassByName("AccumulateInlineFunction").orElseThrow(InvalidInlineTemplateException::new);
    parsedClass.setName(accumulateInlineClassName);
    parsedClass.findAll(ClassOrInterfaceType.class, c -> "CONTEXT_DATA_GENERIC".equals(c.asString())).forEach(c -> c.setName(accumulateInlineClassName + ".ContextData"));
    this.accumulateInlineClass = parsedClass;
    contextData = this.accumulateInlineClass.findFirst(ClassOrInterfaceDeclaration.class, c -> "ContextData".equals(c.getNameAsString())).orElseThrow(InvalidInlineTemplateException::new);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassExpr(com.github.javaparser.ast.expr.ClassExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) DrlxParseUtil.createMvelCompiler(org.drools.modelcompiler.builder.generator.DrlxParseUtil.createMvelCompiler) PatternSourceDescr(org.drools.drl.ast.descr.PatternSourceDescr) DeclarationSpec(org.drools.modelcompiler.builder.generator.DeclarationSpec) DrlxParseUtil(org.drools.modelcompiler.builder.generator.DrlxParseUtil) CompiledBlockResult(org.drools.mvelcompiler.CompiledBlockResult) PatternDescr(org.drools.drl.ast.descr.PatternDescr) DeclarativeInvokerDescr(org.drools.drl.ast.descr.DeclarativeInvokerDescr) DrlxParseUtil.forceCastForName(org.drools.modelcompiler.builder.generator.DrlxParseUtil.forceCastForName) Type(com.github.javaparser.ast.type.Type) DrlxParseUtil.addSemicolon(org.drools.modelcompiler.builder.generator.DrlxParseUtil.addSemicolon) Expression(com.github.javaparser.ast.expr.Expression) CompilationUnit(com.github.javaparser.ast.CompilationUnit) Node(com.github.javaparser.ast.Node) PackageModel(org.drools.modelcompiler.builder.PackageModel) NodeList(com.github.javaparser.ast.NodeList) StaticJavaParser.parseStatement(com.github.javaparser.StaticJavaParser.parseStatement) Collection(java.util.Collection) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Set(java.util.Set) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) Modifier(com.github.javaparser.ast.Modifier) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) List(java.util.List) StaticJavaParser(com.github.javaparser.StaticJavaParser) Optional(java.util.Optional) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) AccumulateVisitor.collectNamesInBlock(org.drools.modelcompiler.builder.generator.visitor.accumulate.AccumulateVisitor.collectNamesInBlock) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) DrlxParseUtil.addCurlyBracesToBlock(org.drools.modelcompiler.builder.generator.DrlxParseUtil.addCurlyBracesToBlock) DrlxParseUtil.rescopeNamesToNewScope(org.drools.modelcompiler.builder.generator.DrlxParseUtil.rescopeNamesToNewScope) ArrayList(java.util.ArrayList) ACC_FUNCTION_CALL(org.drools.modelcompiler.builder.generator.DslMethodNames.ACC_FUNCTION_CALL) HashSet(java.util.HashSet) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) StringUtil(org.drools.modelcompiler.util.StringUtil) FromDescr(org.drools.drl.ast.descr.FromDescr) AccumulateDescr(org.drools.drl.ast.descr.AccumulateDescr) DslMethodNames.createDslTopLevelMethod(org.drools.modelcompiler.builder.generator.DslMethodNames.createDslTopLevelMethod) MvelCompiler(org.drools.mvelcompiler.MvelCompiler) BIND_AS_CALL(org.drools.modelcompiler.builder.generator.DslMethodNames.BIND_AS_CALL) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) IOException(java.io.IOException) NameExpr(com.github.javaparser.ast.expr.NameExpr) Statement(com.github.javaparser.ast.stmt.Statement) MethodReferenceExpr(com.github.javaparser.ast.expr.MethodReferenceExpr) RuleContext(org.drools.modelcompiler.builder.generator.RuleContext) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) IOException(java.io.IOException) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType)

Example 94 with ClassOrInterfaceDeclaration

use of com.github.javaparser.ast.body.ClassOrInterfaceDeclaration in project drools by kiegroup.

the class QueryDefImplGenerator method generate.

public CompilationUnit generate() {
    CompilationUnit cu = new CompilationUnit("org.drools.model.impl");
    cu.setImports(nodeList(parseImport("import org.drools.model.Argument;"), parseImport(String.format("import org.drools.model.Query%dDef;", arity)), parseImport("import org.drools.model.Variable;"), parseImport("import org.drools.model.view.QueryCallViewItem;"), parseImport("import org.drools.model.view.QueryCallViewItemImpl;"), parseImport("import static org.drools.model.FlowDSL.declarationOf;"), parseImport("import static org.drools.model.impl.RuleBuilder.DEFAULT_PACKAGE;")));
    ClassOrInterfaceDeclaration clazz = classDeclaration(cu);
    nameClassConstructor(clazz);
    packageNameClassConstructor(clazz);
    nameClassArgConstructor(clazz);
    pkgNameClassArgConstructor(clazz);
    callMethod(clazz);
    getArgumentsMethod(clazz);
    getters(clazz);
    generateEquals(clazz);
    return cu;
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)

Example 95 with ClassOrInterfaceDeclaration

use of com.github.javaparser.ast.body.ClassOrInterfaceDeclaration in project drools by kiegroup.

the class QueryDefImplGenerator method generateEquals.

private void generateEquals(ClassOrInterfaceDeclaration clazz) {
    String template = "   @Override\n" + "    public boolean isEqualTo( ModelComponent other ) {\n" + "        if ( this == other ) return true;\n" + "        if ( !(other instanceof DEF_IMPL_TYPE) ) return false;\n" + "\n" + "        DEF_IMPL_TYPE that = (DEF_IMPL_TYPE) other;\n" + "\n" + "        return EQUALS_CALL;\n" + "    }";
    BodyDeclaration<?> parse = parseBodyDeclaration(template);
    Expression andExpr = new BooleanLiteralExpr(true);
    for (int i : rangeArity().toArray()) {
        String argWithIndex = argIndex(i);
        Expression e = parseExpression(String.format("ModelComponent.areEqualInModel( %s, that.%s )", argWithIndex, argWithIndex));
        andExpr = new BinaryExpr(andExpr, e, BinaryExpr.Operator.AND);
    }
    final Expression finalAndExpr = andExpr;
    parse.findAll(ClassOrInterfaceType.class, n -> n.toString().equals("DEF_IMPL_TYPE")).forEach(s -> s.replace(parseType("Query" + arity + "DefImpl")));
    parse.findAll(NameExpr.class, n -> n.toString().equals("EQUALS_CALL")).forEach(s -> s.replace(finalAndExpr));
    clazz.addMember(parse);
}
Also used : ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) NodeList.nodeList(com.github.javaparser.ast.NodeList.nodeList) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) StaticJavaParser.parseBodyDeclaration(com.github.javaparser.StaticJavaParser.parseBodyDeclaration) Type(com.github.javaparser.ast.type.Type) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) StaticJavaParser.parseType(com.github.javaparser.StaticJavaParser.parseType) Expression(com.github.javaparser.ast.expr.Expression) CompilationUnit(com.github.javaparser.ast.CompilationUnit) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) QueryCallViewItem(org.drools.model.view.QueryCallViewItem) NodeList(com.github.javaparser.ast.NodeList) ArrayCreationExpr(com.github.javaparser.ast.expr.ArrayCreationExpr) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) TypeParameter(com.github.javaparser.ast.type.TypeParameter) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) StaticJavaParser.parseExpression(com.github.javaparser.StaticJavaParser.parseExpression) NameExpr(com.github.javaparser.ast.expr.NameExpr) Modifier(com.github.javaparser.ast.Modifier) QueryCallViewItemImpl(org.drools.model.view.QueryCallViewItemImpl) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ArrayInitializerExpr(com.github.javaparser.ast.expr.ArrayInitializerExpr) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) StaticJavaParser.parseImport(com.github.javaparser.StaticJavaParser.parseImport) Expression(com.github.javaparser.ast.expr.Expression) StaticJavaParser.parseExpression(com.github.javaparser.StaticJavaParser.parseExpression) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType)

Aggregations

ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)261 Test (org.junit.Test)174 CompilationUnit (com.github.javaparser.ast.CompilationUnit)164 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)84 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)79 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)71 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)52 Context (com.github.javaparser.symbolsolver.core.resolution.Context)39 ClassOrInterfaceDeclarationContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.ClassOrInterfaceDeclarationContext)38 CompilationUnitContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext)37 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)36 Expression (com.github.javaparser.ast.expr.Expression)33 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)28 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)27 MemoryTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver)24 HashMap (java.util.HashMap)21 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)20 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)20 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)20 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)19