use of com.github.javaparser.ast.body.BodyDeclaration in project checker-framework by typetools.
the class AnnotationFileParser method putNewElement.
// Used only by getMembers().
/**
* If {@code typeElt} contains an element for {@code member}, adds to {@code elementsToDecl} a
* mapping from member's element to member. Does nothing if a mapping already exists.
*
* <p>Otherwise (if there is no element for {@code member}), adds to {@code fakeOverrideDecls}
* zero or more mappings. Each mapping is from an element that {@code member} would override to
* {@code member}.
*
* <p>This method does not read or write field {@link annotationFileAnnos}.
*
* @param elementsToDecl the mapping that is side-effected by this method
* @param fakeOverrideDecls fake overrides, also side-effected by this method
* @param typeElt the class in which {@code member} is declared
* @param member the stub file declaration of a method
* @param typeDeclName used only for debugging
* @param astNode where to report errors
*/
private void putNewElement(Map<Element, BodyDeclaration<?>> elementsToDecl, Map<Element, List<BodyDeclaration<?>>> fakeOverrideDecls, TypeElement typeElt, BodyDeclaration<?> member, String typeDeclName, NodeWithRange<?> astNode) {
if (member instanceof MethodDeclaration) {
MethodDeclaration method = (MethodDeclaration) member;
Element elt = findElement(typeElt, method, /*noWarn=*/
true);
if (elt != null) {
putIfAbsent(elementsToDecl, elt, method);
} else {
ExecutableElement overriddenMethod = fakeOverriddenMethod(typeElt, method);
if (overriddenMethod == null) {
// Didn't find the element and it isn't a fake override. Issue a warning.
findElement(typeElt, method, /*noWarn=*/
false);
} else {
List<BodyDeclaration<?>> l = fakeOverrideDecls.computeIfAbsent(overriddenMethod, __ -> new ArrayList<>());
l.add(member);
}
}
} else if (member instanceof ConstructorDeclaration) {
Element elt = findElement(typeElt, (ConstructorDeclaration) member);
if (elt != null) {
putIfAbsent(elementsToDecl, elt, member);
}
} else if (member instanceof FieldDeclaration) {
FieldDeclaration fieldDecl = (FieldDeclaration) member;
for (VariableDeclarator var : fieldDecl.getVariables()) {
Element varelt = findElement(typeElt, var);
if (varelt != null) {
putIfAbsent(elementsToDecl, varelt, fieldDecl);
}
}
} else if (member instanceof EnumConstantDeclaration) {
Element elt = findElement(typeElt, (EnumConstantDeclaration) member, astNode);
if (elt != null) {
putIfAbsent(elementsToDecl, elt, member);
}
} else if (member instanceof ClassOrInterfaceDeclaration) {
Element elt = findElement(typeElt, (ClassOrInterfaceDeclaration) member);
if (elt != null) {
putIfAbsent(elementsToDecl, elt, member);
}
} else if (member instanceof EnumDeclaration) {
Element elt = findElement(typeElt, (EnumDeclaration) member);
if (elt != null) {
putIfAbsent(elementsToDecl, elt, member);
}
} else {
stubDebug(String.format("Ignoring element of type %s in %s", member.getClass(), typeDeclName));
}
}
use of com.github.javaparser.ast.body.BodyDeclaration in project checker-framework by typetools.
the class ToIndexFileConverter method visit.
@Override
public Void visit(ObjectCreationExpr expr, AElement elem) {
ClassOrInterfaceType type = expr.getType();
AClass clazz = scene.classes.getVivify(type.getNameAsString());
Expression scope = expr.getScope().orElse(null);
List<Type> typeArgs = expr.getTypeArguments().orElse(null);
List<Expression> args = expr.getArguments();
NodeList<BodyDeclaration<?>> bodyDecls = expr.getAnonymousClassBody().orElse(null);
if (scope != null) {
scope.accept(this, elem);
}
if (args != null) {
for (Expression arg : args) {
arg.accept(this, elem);
}
}
if (typeArgs != null) {
for (Type typeArg : typeArgs) {
typeArg.accept(this, elem);
}
}
type.accept(this, clazz);
if (bodyDecls != null) {
for (BodyDeclaration<?> decl : bodyDecls) {
decl.accept(this, clazz);
}
}
return null;
}
use of com.github.javaparser.ast.body.BodyDeclaration in project checker-framework by typetools.
the class JointJavacJavaParserVisitor method visitClassMembers.
/**
* Given a list of class members for javac and JavaParser, visits each javac member with its
* corresponding JavaParser member. Skips synthetic javac members.
*
* @param javacMembers a list of trees forming the members of a javac {@code ClassTree}
* @param javaParserMembers a list of nodes forming the members of a JavaParser {@code
* ClassOrInterfaceDeclaration} or an {@code ObjectCreationExpr} with an anonymous class body
* that corresponds to {@code javacMembers}
*/
private void visitClassMembers(List<? extends Tree> javacMembers, List<BodyDeclaration<?>> javaParserMembers) {
PeekingIterator<Tree> javacIter = Iterators.peekingIterator(javacMembers.iterator());
PeekingIterator<BodyDeclaration<?>> javaParserIter = Iterators.peekingIterator(javaParserMembers.iterator());
while (javacIter.hasNext() || javaParserIter.hasNext()) {
// Skip javac's synthetic no-argument constructors.
if (javacIter.hasNext() && isNoArgumentConstructor(javacIter.peek()) && (!javaParserIter.hasNext() || !isNoArgumentConstructor(javaParserIter.peek()))) {
javacIter.next();
continue;
}
// VariableDeclarators. Match the declarators with the VariableTrees.
if (javaParserIter.hasNext() && javaParserIter.peek().isFieldDeclaration()) {
for (VariableDeclarator decl : javaParserIter.next().asFieldDeclaration().getVariables()) {
assert javacIter.hasNext();
assert javacIter.peek().getKind() == Tree.Kind.VARIABLE;
javacIter.next().accept(this, decl);
}
continue;
}
assert javacIter.hasNext();
assert javaParserIter.hasNext();
javacIter.next().accept(this, javaParserIter.next());
}
assert !javacIter.hasNext();
assert !javaParserIter.hasNext();
}
use of com.github.javaparser.ast.body.BodyDeclaration in project eap-additional-testsuite by jboss-set.
the class ParsedTests method readInternalClassMethods.
private static void readInternalClassMethods(String file, FileData fd) throws IOException {
InputStream in = null;
CompilationUnit cu = null;
try {
in = new FileInputStream(file);
cu = JavaParser.parse(in);
if (internalClassMethods.get(fd.packageName.replaceAll("/", ".") + "." + fd.fileName.replaceAll("\\.java", "")) == null) {
internalClassMethods.put(fd.packageName.replaceAll("/", ".") + "." + fd.fileName.replaceAll("\\.java", ""), new HashMap<String, ArrayList<String[]>>());
}
NodeList<TypeDeclaration<?>> types = cu.getTypes();
for (TypeDeclaration<?> type : types) {
NodeList<BodyDeclaration<?>> members = type.getMembers();
for (BodyDeclaration<?> member : members) {
if (member instanceof MethodDeclaration) {
MethodDeclaration method = (MethodDeclaration) member;
HashMap<String, ArrayList<String[]>> arg = internalClassMethods.get(fd.packageName.replaceAll("/", ".") + "." + fd.fileName.replaceAll("\\.java", ""));
// System.out.println("yyy " + method.getParameters().size() + " " + method.getParameters());
String[] params = new String[method.getParameters().size()];
for (int i = 0; i < method.getParameters().size(); i++) {
params[i] = method.getParameters().get(i).getTypeAsString();
}
if (!arg.keySet().contains(method.getName().toString())) {
ArrayList<String[]> paramArrayList = new ArrayList<>();
paramArrayList.add(params);
arg.put(method.getName().toString(), paramArrayList);
ArrayList<String[]> paramRTArrayList = new ArrayList<>();
paramRTArrayList.add(new String[] { method.getTypeAsString() });
arg.put(method.getName().toString() + "_Return_Type", paramRTArrayList);
} else {
arg.get(method.getName().toString()).add(params);
arg.get(method.getName().toString() + "_Return_Type").add(new String[] { method.getTypeAsString() });
}
}
if (member instanceof ConstructorDeclaration) {
ConstructorDeclaration construct = (ConstructorDeclaration) member;
HashMap<String, ArrayList<String[]>> arg = internalClassMethods.get(fd.packageName.replaceAll("/", ".") + "." + fd.fileName.replaceAll("\\.java", ""));
String[] params = new String[construct.getParameters().size()];
for (int i = 0; i < construct.getParameters().size(); i++) {
params[i] = construct.getParameters().get(i).getTypeAsString();
}
if (!arg.keySet().contains(construct.getName().toString() + "_Constructor")) {
ArrayList<String[]> paramCTArrayList = new ArrayList<>();
paramCTArrayList.add(params);
arg.put(construct.getName().toString() + "_Constructor", paramCTArrayList);
} else {
arg.get(construct.getName().toString() + "_Constructor").add(params);
}
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
in.close();
}
}
use of com.github.javaparser.ast.body.BodyDeclaration in project eap-additional-testsuite by jboss-set.
the class ParsedTests method readInternalClassFields.
private static void readInternalClassFields(String file, FileData fd) throws IOException {
InputStream in = null;
CompilationUnit cu = null;
try {
in = new FileInputStream(file);
cu = JavaParser.parse(in);
if (internalClassFields.get(fd.packageName.replaceAll("/", ".") + "." + fd.fileName.replaceAll("\\.java", "")) == null) {
internalClassFields.put(fd.packageName.replaceAll("/", ".") + "." + fd.fileName.replaceAll("\\.java", ""), new HashMap<String, String>());
}
NodeList<TypeDeclaration<?>> types = cu.getTypes();
for (TypeDeclaration<?> type : types) {
NodeList<BodyDeclaration<?>> members = type.getMembers();
for (BodyDeclaration<?> member : members) {
if (member instanceof FieldDeclaration) {
FieldDeclaration field = (FieldDeclaration) member;
HashMap<String, String> arg = internalClassFields.get(fd.packageName.replaceAll("/", ".") + "." + fd.fileName.replaceAll("\\.java", ""));
if (!field.getModifiers().contains("private")) {
arg.put(field.getVariables().get(0).getName().toString(), field.getVariables().get(0).getType().toString());
}
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
in.close();
}
}
Aggregations