Search in sources :

Example 6 with BodyDeclaration

use of com.github.javaparser.ast.body.BodyDeclaration in project structr by structr.

the class JavaParserModule method toJson.

private JsonResult toJson(final CompilationUnit cu) {
    final JsonResult jsonResult = new JsonResult();
    final NodeList<TypeDeclaration<?>> types = cu.getTypes();
    if (types.isEmpty()) {
        return jsonResult;
    }
    final TypeDeclaration<?> type = types.get(0);
    jsonResult.addName(type);
    jsonResult.addModifiers(type);
    final Optional<PackageDeclaration> pkg = cu.getPackageDeclaration();
    if (pkg.isPresent()) {
        jsonResult.addPackage(pkg.get());
    }
    final List<BodyDeclaration<?>> members = type.getMembers();
    final List<JsonResult> membersList = new ArrayList<>();
    members.forEach((t) -> {
        final JsonResult member = new JsonResult();
        if (t instanceof FieldDeclaration) {
            final FieldDeclaration fd = t.asFieldDeclaration();
            member.addName(fd.getVariable(0));
            member.addType(fd.getVariable(0).getType());
            member.addModifiers(fd);
        } else if (t instanceof CallableDeclaration) {
            final CallableDeclaration callable = t.asCallableDeclaration();
            if (t instanceof ConstructorDeclaration) {
                final ConstructorDeclaration cd = t.asConstructorDeclaration();
                member.addName(cd);
                member.isConstructor();
                member.addModifiers(cd);
            } else if (t instanceof MethodDeclaration) {
                final MethodDeclaration md = t.asMethodDeclaration();
                member.addName(md);
                member.isMethod();
                member.addReturnType(md.getType());
                member.addModifiers(md);
                member.addBody(md);
            }
            final NodeList<Parameter> parameters = callable.getParameters();
            List<JsonResult> parameterList = new ArrayList<>();
            parameters.forEach((p) -> {
                JsonResult param = new JsonResult();
                param.addName(p);
                param.addType(p.getType());
                param.addModifiers(p);
                parameterList.add(param);
            });
            member.addParameters(parameterList);
        }
        membersList.add(member);
    });
    jsonResult.addMembers(membersList);
    return jsonResult;
}
Also used : JsonObject(com.google.gson.JsonObject) Package(org.structr.javaparser.entity.Package) XPathExpressionException(javax.xml.xpath.XPathExpressionException) UnsolvedSymbolException(com.github.javaparser.symbolsolver.javaparsermodel.UnsolvedSymbolException) LoggerFactory(org.slf4j.LoggerFactory) SecurityContext(org.structr.common.SecurityContext) StringUtils(org.apache.commons.lang3.StringUtils) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) Actions(org.structr.schema.action.Actions) FrameworkException(org.structr.common.error.FrameworkException) App(org.structr.core.app.App) NodeWithSimpleName(com.github.javaparser.ast.nodeTypes.NodeWithSimpleName) Type(com.github.javaparser.ast.type.Type) Document(org.w3c.dom.Document) Map(java.util.Map) CompilationUnit(com.github.javaparser.ast.CompilationUnit) JavaInterface(org.structr.javaparser.entity.JavaInterface) NodeList(com.github.javaparser.ast.NodeList) Folder(org.structr.web.entity.Folder) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) Set(java.util.Set) GraphObject(org.structr.core.GraphObject) ResolvedValueDeclaration(com.github.javaparser.resolution.declarations.ResolvedValueDeclaration) Functions(org.structr.core.function.Functions) JsonArray(com.google.gson.JsonArray) List(java.util.List) StructrModule(org.structr.module.StructrModule) JarTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.JarTypeSolver) SAXException(org.xml.sax.SAXException) LicenseManager(org.structr.api.service.LicenseManager) Optional(java.util.Optional) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) QName(javax.xml.namespace.QName) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NodeWithOptionalBlockStmt(com.github.javaparser.ast.nodeTypes.NodeWithOptionalBlockStmt) StructrApp(org.structr.core.app.StructrApp) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) Method(org.structr.javaparser.entity.Method) PackageDeclaration(com.github.javaparser.ast.PackageDeclaration) XPath(javax.xml.xpath.XPath) XPathConstants(javax.xml.xpath.XPathConstants) Parameter(com.github.javaparser.ast.body.Parameter) HashMap(java.util.HashMap) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) ArrayList(java.util.ArrayList) PropertyMap(org.structr.core.property.PropertyMap) File(org.structr.web.entity.File) JavaClass(org.structr.javaparser.entity.JavaClass) JsonPrimitive(com.google.gson.JsonPrimitive) ClassOrInterface(org.structr.javaparser.entity.ClassOrInterface) LinkedHashSet(java.util.LinkedHashSet) InputSource(org.xml.sax.InputSource) Logger(org.slf4j.Logger) NodeWithModifiers(com.github.javaparser.ast.nodeTypes.NodeWithModifiers) Files(java.nio.file.Files) AbstractSchemaNode(org.structr.core.entity.AbstractSchemaNode) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) CallableDeclaration(com.github.javaparser.ast.body.CallableDeclaration) AddJarsToIndexFunction(org.structr.javaparser.entity.AddJarsToIndexFunction) AtomicLong(java.util.concurrent.atomic.AtomicLong) XPathFactory(javax.xml.xpath.XPathFactory) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) StringReader(java.io.StringReader) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) SymbolReference(com.github.javaparser.symbolsolver.model.resolution.SymbolReference) Paths(java.nio.file.Paths) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) Module(org.structr.javaparser.entity.Module) JavaParser(com.github.javaparser.JavaParser) InputStream(java.io.InputStream) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) NodeList(com.github.javaparser.ast.NodeList) ArrayList(java.util.ArrayList) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) CallableDeclaration(com.github.javaparser.ast.body.CallableDeclaration) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) NodeList(com.github.javaparser.ast.NodeList) List(java.util.List) ArrayList(java.util.ArrayList) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) PackageDeclaration(com.github.javaparser.ast.PackageDeclaration)

Example 7 with BodyDeclaration

use of com.github.javaparser.ast.body.BodyDeclaration in project AndroidLife by CaMnter.

the class FinalRClassBuilder method addResourceType.

/**
 * 复制资源 内部 class 到 R2 class 中
 *
 * 这里是给 R2 的 TypeSpec 添加生成局域
 *
 * @param supportedTypes 支持的类型( "array", "attr", "bool", "color", "dimen", "drawable", "id",
 * "integer", "string" )
 * @param result R2 的 TypeSpec
 * @param node R 的 接口或者类 ClassOrInterfaceDeclaration
 */
private static void addResourceType(List<String> supportedTypes, TypeSpec.Builder result, ClassOrInterfaceDeclaration node) {
    // 判断是否是资源内部类( "array", "attr", "bool", "color", "dimen", "drawable", "id", "integer", "string" )
    if (!supportedTypes.contains(node.getNameAsString())) {
        return;
    }
    // 创建 R2 的内部类 TypeSpec,为了进行资源复制
    String type = node.getNameAsString();
    TypeSpec.Builder resourceType = TypeSpec.classBuilder(type).addModifiers(PUBLIC, STATIC, FINAL);
    /*
         * 遍历 R 内部类的每个资源 field
         * 然后给 R2 的内部类 TypeSpec 添加 field 生成语句
         */
    for (BodyDeclaration field : node.getMembers()) {
        if (field instanceof FieldDeclaration) {
            addResourceField(resourceType, ((FieldDeclaration) field).getVariables().get(0), getSupportAnnotationClass(type));
        }
    }
    // R2 的内部类 TypeSpec 添加到 R2 TypeSpec 内
    result.addType(resourceType.build());
}
Also used : BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 8 with BodyDeclaration

use of com.github.javaparser.ast.body.BodyDeclaration 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 9 with BodyDeclaration

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

the class LocationSearcher method searchLocationFromFile.

private static Location searchLocationFromFile(final SearchContext ctx, final String fqcn, final File targetFile) throws IOException {
    final CompilationUnit compilationUnit = JavaParser.parse(targetFile, StandardCharsets.UTF_8);
    final List<TypeDeclaration<?>> types = compilationUnit.getTypes();
    for (final TypeDeclaration<?> type : types) {
        if (ctx.kind.equals(SearchKind.CLASS)) {
            final SimpleName simpleName = type.getName();
            final String typeName = simpleName.getIdentifier();
            final String name = ClassNameUtils.getSimpleName(fqcn);
            final Optional<Position> begin = simpleName.getBegin();
            if (typeName.equals(name) && begin.isPresent()) {
                final Position position = begin.get();
                return new Location(targetFile.getCanonicalPath(), position.line, position.column);
            }
        }
        final List<BodyDeclaration<?>> members = type.getMembers();
        ConstructorDeclaration constructor = null;
        MethodDeclaration method = null;
        for (final BodyDeclaration<?> member : members) {
            if (member instanceof FieldDeclaration && ctx.name != null && ctx.kind.equals(SearchKind.FIELD)) {
                final Location variable = getFieldLocation(ctx, targetFile, (FieldDeclaration) member);
                if (variable != null) {
                    return variable;
                }
            } else if (member instanceof ConstructorDeclaration && ctx.name != null && ctx.kind.equals(SearchKind.METHOD)) {
                final ConstructorDeclaration declaration = (ConstructorDeclaration) member;
                final SimpleName simpleName = declaration.getName();
                final String name = simpleName.getIdentifier();
                final Optional<Position> begin = simpleName.getBegin();
                if (name.equals(ctx.name) && begin.isPresent()) {
                    final Position position = begin.get();
                    final List<Parameter> parameters = declaration.getParameters();
                    // TODO check FQCN types
                    if (ctx.arguments.size() == parameters.size()) {
                        return new Location(targetFile.getCanonicalPath(), position.line, position.column);
                    } else {
                        if (constructor == null) {
                            constructor = declaration;
                        }
                    }
                }
            } else if (member instanceof MethodDeclaration && ctx.name != null && ctx.kind.equals(SearchKind.METHOD)) {
                final MethodDeclaration declaration = (MethodDeclaration) member;
                final SimpleName simpleName = declaration.getName();
                final String name = simpleName.getIdentifier();
                final Optional<Position> begin = simpleName.getBegin();
                if (name.equals(ctx.name) && begin.isPresent()) {
                    final Position position = begin.get();
                    final List<Parameter> parameters = declaration.getParameters();
                    if (ctx.arguments.size() == parameters.size()) {
                        return new Location(targetFile.getCanonicalPath(), position.line, position.column);
                    } else {
                        if (method == null) {
                            method = declaration;
                        }
                    }
                }
            }
        }
        if (constructor != null) {
            final Position pos = constructor.getName().getBegin().get();
            return new Location(targetFile.getCanonicalPath(), pos.line, pos.column);
        }
        if (method != null) {
            final Position pos = method.getName().getBegin().get();
            return new Location(targetFile.getCanonicalPath(), pos.line, pos.column);
        }
    }
    return null;
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Optional(java.util.Optional) Position(com.github.javaparser.Position) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) SimpleName(com.github.javaparser.ast.expr.SimpleName) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) Parameter(com.github.javaparser.ast.body.Parameter) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) List(java.util.List) ArrayList(java.util.ArrayList) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration)

Example 10 with BodyDeclaration

use of com.github.javaparser.ast.body.BodyDeclaration 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

BodyDeclaration (com.github.javaparser.ast.body.BodyDeclaration)29 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)10 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)9 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)9 CompilationUnit (com.github.javaparser.ast.CompilationUnit)7 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)7 EnumDeclaration (com.github.javaparser.ast.body.EnumDeclaration)7 NodeList (com.github.javaparser.ast.NodeList)6 TypeDeclaration (com.github.javaparser.ast.body.TypeDeclaration)6 ArrayList (java.util.ArrayList)6 EnumConstantDeclaration (com.github.javaparser.ast.body.EnumConstantDeclaration)5 BlockComment (com.github.javaparser.ast.comments.BlockComment)5 Comment (com.github.javaparser.ast.comments.Comment)5 JavadocComment (com.github.javaparser.ast.comments.JavadocComment)5 LineComment (com.github.javaparser.ast.comments.LineComment)5 List (java.util.List)5 Parameter (com.github.javaparser.ast.body.Parameter)4 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)3 Expression (com.github.javaparser.ast.expr.Expression)3 Type (com.github.javaparser.ast.type.Type)3