private void getJavaMethods (ArrayList<JavaMethod> methods, TypeDeclaration type) { classStack.push(type); if (type.getMembers() != null) { for (BodyDeclaration member : type.getMembers()) { if (member instanceof ClassOrInterfaceDeclaration || member instanceof EnumDeclaration) { getJavaMethods(methods, (TypeDeclaration)member); } else { if (member instanceof MethodDeclaration) { MethodDeclaration method = (MethodDeclaration)member; if (!ModifierSet.hasModifier(((MethodDeclaration)member).getModifiers(), ModifierSet.NATIVE)) continue; methods.add(createMethod(method)); } } } } classStack.pop(); }
private void getJavaMethods (ArrayList<JavaMethod> methods, TypeDeclaration type) { classStack.push(type); if (type.getMembers() != null) { for (BodyDeclaration member : type.getMembers()) { if (member instanceof ClassOrInterfaceDeclaration || member instanceof EnumDeclaration) { getJavaMethods(methods, (TypeDeclaration)member); } else { if (member instanceof MethodDeclaration) { MethodDeclaration method = (MethodDeclaration)member; if (!ModifierSet.hasModifier(((MethodDeclaration)member).getModifiers(), ModifierSet.NATIVE)) continue; methods.add(createMethod(method)); } } } } classStack.pop(); }
/** * Returns the {@code (i+1)}'th constructor of the given type declaration, in textual order. The constructor that * appears first has the index 0, the second one the index 1, and so on. * * @param td The type declaration to search in. Note that only classes and enums have constructors. * @param index The index of the desired constructor. * @return The desired ConstructorDeclaration if it was found, and {@code null} otherwise. */ public static ConstructorDeclaration demandConstructor(TypeDeclaration<?> td, int index) { ConstructorDeclaration found = null; int i = 0; for (BodyDeclaration<?> bd : td.getMembers()) { if (bd instanceof ConstructorDeclaration) { ConstructorDeclaration cd = (ConstructorDeclaration) bd; if (i == index) { found = cd; break; } i++; } } if (found == null) { throw new IllegalStateException("No constructor with index " + index); } return found; }
private void addFieldVariables(@Nonnull TypeDeclaration typeDeclaration, @Nonnull Set<String> variables) { addFieldVariables(typeDeclaration.getMembers(), variables); }
public static MethodDeclaration demandMethod(TypeDeclaration<?> cd, String name) { MethodDeclaration found = null; for (BodyDeclaration<?> bd : cd.getMembers()) { if (bd instanceof MethodDeclaration) { MethodDeclaration md = (MethodDeclaration) bd; if (md.getNameAsString().equals(name)) { if (found != null) { throw new IllegalStateException("Ambiguous getName"); } found = md; } } } if (found == null) { throw new IllegalStateException("No method called " + name); } return found; }
public static MethodDeclaration demandMethod(TypeDeclaration<?> cd, String name) { MethodDeclaration found = null; for (BodyDeclaration<?> bd : cd.getMembers()) { if (bd instanceof MethodDeclaration) { MethodDeclaration md = (MethodDeclaration) bd; if (md.getName().getId().equals(name)) { if (found != null) { throw new IllegalStateException("Ambiguous getName"); } found = md; } } } if (found == null) { throw new IllegalStateException("No method with given name"); } return found; }
public static MethodDeclaration demandMethod(TypeDeclaration<?> cd, String name) { MethodDeclaration found = null; for (BodyDeclaration<?> bd : cd.getMembers()) { if (bd instanceof MethodDeclaration) { MethodDeclaration md = (MethodDeclaration) bd; if (md.getName().getId().equals(name)) { if (found != null) { throw new IllegalStateException("Ambiguous getName"); } found = md; } } } if (found == null) { throw new IllegalStateException("No method with given name"); } return found; }
private void getJavaMethods (ArrayList<JavaMethod> methods, TypeDeclaration type) { classStack.push(type); if (type.getMembers() != null) { for (BodyDeclaration member : type.getMembers()) { if (member instanceof ClassOrInterfaceDeclaration || member instanceof EnumDeclaration) { getJavaMethods(methods, (TypeDeclaration)member); } else { if (member instanceof MethodDeclaration) { MethodDeclaration method = (MethodDeclaration)member; if (!ModifierSet.hasModifier(((MethodDeclaration)member).getModifiers(), ModifierSet.NATIVE)) continue; methods.add(createMethod(method)); } } } } classStack.pop(); }
public static Optional<TypeDeclaration<?>> findType(TypeDeclaration<?> td, String qualifiedName) { final String typeName = getOuterTypeName(qualifiedName); Optional<TypeDeclaration<?>> type = Optional.empty(); for (Node n : td.getMembers()) { if (n instanceof TypeDeclaration && ((TypeDeclaration<?>) n).getName().getId().equals(typeName)) { type = Optional.of((TypeDeclaration<?>) n); break; } } final String innerTypeName = getInnerTypeName(qualifiedName); if (type.isPresent() && !innerTypeName.isEmpty()) { return findType(type.get(), innerTypeName); } return type; }
public static Optional<TypeDeclaration<?>> findType(TypeDeclaration<?> td, String qualifiedName) { final String typeName = getOuterTypeName(qualifiedName); Optional<TypeDeclaration<?>> type = Optional.empty(); for (Node n : td.getMembers()) { if (n instanceof TypeDeclaration && ((TypeDeclaration<?>) n).getName().getId().equals(typeName)) { type = Optional.of((TypeDeclaration<?>) n); break; } } final String innerTypeName = getInnerTypeName(qualifiedName); if (type.isPresent() && !innerTypeName.isEmpty()) { return findType(type.get(), innerTypeName); } return type; }
@Nonnull private String resolveReferencedType(@Nonnull Qualifier qualifier, @Nonnull TypeDeclaration type) { Qualifier parentQualifier = qualifier.getParentQualifier(); if (parentQualifier != null) { for (TypeDeclaration innerType : emptyIfNull(type.getMembers()).filter(TypeDeclaration.class)) { if (parentQualifier.getName().equals(innerType.getName())) { return resolveReferencedType(parentQualifier, innerType); } } } return getTypeName(type); }
/** * Looks among the type declared in the TypeDeclaration for one having the specified name. * The name can be qualified with respect to the TypeDeclaration. For example, if the class declarationd defines class D * and class D contains an internal class named E then the qualifiedName that can be resolved are "D", and "D.E". */ public static Optional<TypeDeclaration<?>> findType(TypeDeclaration<?> td, String qualifiedName) { final String typeName = getOuterTypeName(qualifiedName); Optional<TypeDeclaration<?>> type = Optional.empty(); for (Node n : td.getMembers()) { if (n instanceof TypeDeclaration && ((TypeDeclaration<?>) n).getName().getId().equals(typeName)) { type = Optional.of((TypeDeclaration<?>) n); break; } } final String innerTypeName = getInnerTypeName(qualifiedName); if (type.isPresent() && !innerTypeName.isEmpty()) { return findType(type.get(), innerTypeName); } return type; }
@Nonnull @Override public Optional<String> apply(@Nonnull Qualifier<?> typeReference) { Qualifier firstQualifier = typeReference.getFirstQualifier(); for (Node loopNode = typeReference.getNode(); ; ) { Optional<String> reference; if (TypeDeclaration.class.isInstance(loopNode)) { TypeDeclaration typeDeclaration = TypeDeclaration.class.cast(loopNode); reference = resolveInnerReference(firstQualifier, singleton(typeDeclaration)); if (reference.isPresent()) { return reference; } reference = resolveInnerReference(firstQualifier, typeDeclaration.getMembers()); if (reference.isPresent()) { return reference; } } else if (CompilationUnit.class.isInstance(loopNode)) { reference = resolveInnerReference(firstQualifier, CompilationUnit.class.cast(loopNode).getTypes()); if (reference.isPresent()) { return reference; } } loopNode = loopNode.getParentNode(); if (loopNode == null) { return absent(); } } }
@Override public Map<String, ResultType> parseStatements( List<String> statements, Collection<String> importedClasses, @Nullable ClassLoaderContext classLoaderContext ) throws ParseException { StringBuilder codeBuilder = new StringBuilder( "class X {" ); codeBuilder.append( "void run() {" ); for (String statement : statements) { codeBuilder.append( statement ); } codeBuilder.append( "} }" ); List<Statement> parsedStatements; try { CompilationUnit compilationUnit = JavaParser.parse( new StringReader( codeBuilder.toString() ), false ); MethodDeclaration methodDeclaration = ( MethodDeclaration ) compilationUnit .getTypes().get( 0 ) .getMembers().get( 0 ); parsedStatements = methodDeclaration.getBody().getStmts(); } catch ( com.github.javaparser.ParseException e ) { throw new ParseException( e.getMessage(), 0 ); } return resultTypes( new TypeDiscoverer( importedClasses, classLoaderContext ) .getTypesByVariableName( parsedStatements ) ); }
CompilationUnit cu = JavaParser.parse(javaFile); for (TypeDeclaration typeDec : cu.getTypes()) { List<BodyDeclaration> members = typeDec.getMembers(); if(members != null) { for (BodyDeclaration member : members) { //Check just members that are FieldDeclarations FieldDeclaration field = (FieldDeclaration) member; //Print the field's class typr System.out.println(field.getType()); //Print the field's name System.out.println(field.getVariables().get(0).getId().getName()); //Print the field's init value, if not null Object initValue = field.getVariables().get(0).getInit(); if(initValue != null) { System.out.println(field.getVariables().get(0).getInit().toString()); } } }
CompilationUnit cu = JavaParser.parse(file); List<TypeDeclaration> typeDeclarations = cu.getTypes(); for (TypeDeclaration typeDec : typeDeclarations) { List<BodyDeclaration> members = typeDec.getMembers(); if(members != null) { for (BodyDeclaration member : members) { if (member instanceof ConstructorDeclaration) { ConstructorDeclaration constructor = (ConstructorDeclaration) member; //Put your code here //The constructor instance contains all the information about it. constructor.getBeginLine(); //begin line constructor.getBlock(); //constructor body } } } }
private void addManualMembers(SimpleJavaGenerator gen, CompilationUnit existing) { for (TypeDeclaration<?> type : existing.getTypes()) { for (BodyDeclaration<?> member : type.getMembers()) { if (member instanceof MethodDeclaration || member instanceof FieldDeclaration || member instanceof ConstructorDeclaration) { if (!isGenerated(member)) { gen.addMember(new Member(member)); } } } } }
public void index(TypeDeclaration<?> type, int cuId) { EnumSet<Modifier> modifiers = type.getModifiers(); boolean isInterface = type instanceof ClassOrInterfaceDeclaration && ((ClassOrInterfaceDeclaration) type).isInterface(); boolean isAnnotation = type instanceof AnnotationDeclaration; boolean isEnumeration = type instanceof EnumDeclaration; boolean isClass = !isAnnotation && !isEnumeration && !isInterface; Type t = new Type(type.getNameAsString(), type.isPublic(), type.isStatic(), modifiers.contains(FINAL), modifiers.contains(ABSTRACT), isClass, isInterface, isEnumeration, isAnnotation, cuId); int typeId = typeDao.save(t); for (BodyDeclaration member : type.getMembers()) { bodyDeclarationIndexer.index(member, typeId); } } }
public SymbolReference<ResolvedTypeDeclaration> solveType(String name) { if (this.wrappedNode.getName().getId().equals(name)) { return SymbolReference.solved(JavaParserFacade.get(typeSolver).getTypeDeclaration(wrappedNode)); } // Internal classes for (BodyDeclaration<?> member : this.wrappedNode.getMembers()) { if (member instanceof com.github.javaparser.ast.body.TypeDeclaration) { com.github.javaparser.ast.body.TypeDeclaration<?> internalType = (com.github.javaparser.ast.body.TypeDeclaration<?>) member; if (internalType.getName().getId().equals(name)) { return SymbolReference.solved(JavaParserFacade.get(typeSolver).getTypeDeclaration(internalType)); } else if (name.startsWith(String.format("%s.%s", wrappedNode.getName(), internalType.getName()))) { return JavaParserFactory.getContext(internalType, typeSolver).solveType(name.substring(wrappedNode.getName().getId().length() + 1)); } else if (name.startsWith(String.format("%s.", internalType.getName()))) { return JavaParserFactory.getContext(internalType, typeSolver).solveType(name.substring(internalType.getName().getId().length() + 1)); } } } if (wrappedNode instanceof NodeWithTypeParameters) { NodeWithTypeParameters<?> nodeWithTypeParameters = (NodeWithTypeParameters<?>) wrappedNode; for (TypeParameter astTpRaw : nodeWithTypeParameters.getTypeParameters()) { TypeParameter astTp = astTpRaw; if (astTp.getName().getId().equals(name)) { return SymbolReference.solved(new JavaParserTypeParameter(astTp, typeSolver)); } } } // Look into extended classes and implemented interfaces ResolvedTypeDeclaration type = checkAncestorsForType(name, this.typeDeclaration); return ((type != null) ? SymbolReference.solved(type) : context.getParent().solveType(name)); }
private boolean updateModules() { List<TypeDeclaration> types = compilationUnit.getTypes(); for (TypeDeclaration type : types) { List<BodyDeclaration> members = type.getMembers(); for (BodyDeclaration member : members) { if (!(member instanceof MethodDeclaration)) {