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;
}
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());
}
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();
}
}
}
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;
}
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);
}
Aggregations