private JavaMethod createMethod (MethodDeclaration method) { String className = classStack.peek().getName(); String name = method.getName(); boolean isStatic = ModifierSet.hasModifier(method.getModifiers(), ModifierSet.STATIC); String returnType = method.getType().toString(); ArrayList<Argument> arguments = new ArrayList<Argument>(); if (method.getParameters() != null) { for (Parameter parameter : method.getParameters()) { arguments.add(new Argument(getArgumentType(parameter), parameter.getId().getName())); } } return new JavaMethod(className, name, isStatic, returnType, null, arguments, method.getBeginLine(), method.getEndLine()); }
declaration.getModifiers().toArray(new Modifier[]{})); declaration.getJavadocComment().ifPresent(newField::setJavadocComment); for (Comment comment : declaration.getAllContainedComments()) { newField.setComment(comment); String name = declaration.getNameAsString(); if (oldMethod.get(name) == null) { MethodDeclaration newMethod = old.getType(0) .addMethod(name, declaration.getModifiers().toArray(new Modifier[]{})); declaration.getJavadocComment().ifPresent(newMethod::setJavadocComment); for (Comment comment : declaration.getAllContainedComments()) { newMethod.setComment(comment); for (AnnotationExpr annotationExpr : declaration.getAnnotations()) { newMethod.addAnnotation(annotationExpr.clone());
String methodName = method.getNameAsString(); if (!methodName.endsWith(methodNameSuffix)) { String fieldUpdaterFieldName = fieldUpdaterFieldName(variableName); method.setBody(fieldUpdaterLazySet(fieldUpdaterFieldName, newValueName)); } else if (methodName.startsWith("cas")) { usesFieldUpdater = true; String fieldUpdaterFieldName = fieldUpdaterFieldName(variableName); String expectedValueName = "expect"; method.setBody( fieldUpdaterCompareAndSet(fieldUpdaterFieldName, expectedValueName, newValueName)); } else if (methodName.startsWith("sv")) { method.setBody(fieldAssignment(variableName, newValueName)); } else if (methodName.startsWith("lv") || methodName.startsWith("lp")) { method.setBody(returnField(variableName)); } else { throw new IllegalStateException("Unhandled method: " + methodName);
@Test public void accessClassFieldThroughThis() throws ParseException { CompilationUnit cu = parseSample("AccessClassMemberThroughThis"); com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "AccessClassMemberThroughThis"); MethodDeclaration method = Navigator.demandMethod(clazz, "getLabel2"); ReturnStmt returnStmt = (ReturnStmt) method.getBody().get().getStatements().get(0); Expression expression = returnStmt.getExpression().get(); ResolvedType ref = JavaParserFacade.get(new ReflectionTypeSolver()).getType(expression); assertEquals("java.lang.String", ref.describe()); }
@Test public void issue257() throws FileNotFoundException { String pathToSourceFile = adaptPath("src/test/resources/issue257/A.java.txt"); CompilationUnit cu = JavaParser.parse(new File(pathToSourceFile)); Statement statement = cu.getClassByName("A").get().getMethodsByName("run").get(0).getBody().get().getStatement(0); ExpressionStmt expressionStmt = (ExpressionStmt)statement; Expression expression = expressionStmt.getExpression(); JavaParserFacade.get(typeSolver).getType(expression); }
@Override public List<Parameter> parametersExposedToChild(Node child) { if (wrappedNode.getBody().isPresent() && child == wrappedNode.getBody().get()) { return wrappedNode.getParameters(); } return Collections.emptyList(); } }
@Override public void visit(MethodDeclaration method, ClassBuilder builder) { super.visit(method, builder); TypeFactory factory = new TypeFactory(packageName, imports); if (method.getParentNode().isPresent() && method.getParentNode().get() instanceof ClassOrInterfaceDeclaration && isClass((ClassOrInterfaceDeclaration) method.getParentNode().get()) && isGetter(method.getNameAsString(), method.getType().asString()) && !skipFieldOrGetter(method.getModifiers())) { builder.withGetter(new Method( method.getNameAsString(), factory.build(method.getType().asString(), method.getType() instanceof PrimitiveType) )); } }
@Override public void visit(MethodDeclaration n, Void arg) { TestStep step = new TestStep(); step.stepMethod = n.getName().toString(); if (n.getJavadocComment().isPresent()) { step.stepDetails = n.getJavadocComment().get().getContent().replaceAll("\\s*\\*\\s+", "\n").trim(); } else { step.stepDetails = ""; } if (n.getAnnotationByName("Given").isPresent()) { step.stepName = getAnnotationString(n.getAnnotationByName("Given").get()); } else if (n.getAnnotationByName("When").isPresent()) { step.stepName = getAnnotationString(n.getAnnotationByName("When").get()); } else if (n.getAnnotationByName("Then").isPresent()) { step.stepName = getAnnotationString(n.getAnnotationByName("Then").get()); } else { step.stepName = n.getDeclarationAsString().split("throws ")[0].trim(); } steps.add(step); }
/** * @param methodDeclaration * @param position * @return true if given method starts at given position, false otherwise */ public static boolean isMethodDeclarationAtLine(MethodDeclaration methodDeclaration, Integer position) { if (methodDeclaration.getName().getBegin().isPresent()) { if (position == methodDeclaration.getName().getBegin().get().line) { return true; } } return false; }
throw new IllegalArgumentException("The given node is not a name"); if (!name.getParentNode().isPresent()) { throw new IllegalArgumentException("We cannot understand the role of a name if it has no parent"); if (whenParentIs(Name.class, name, (p, c) -> p.getQualifier().isPresent() && p.getQualifier().get() == c)) { return classifyRole(name.getParentNode().get()); return NameRole.REFERENCE; if (whenParentIs(MethodDeclaration.class, name, (p, c) -> p.getName() == c)) { return NameRole.DECLARATION; return NameRole.REFERENCE; if (whenParentIs(MethodDeclaration.class, name, (p, c) -> p.getName() == c)) { return NameRole.DECLARATION; if (whenParentIs(MethodDeclaration.class, name, (p, c) -> p.getType() == c || p.getThrownExceptions().contains(c))) { return NameRole.REFERENCE;
public void visit(MethodDeclaration n, Void arg) { // only display public methods if (!n.getModifiers().contains(Modifier.PUBLIC)) { return; } javadoc.append(String.format("\nh4. Operation: %s\n", n.getNameAsString())); try { Comment comment = n.getComment().get(); javadoc.append(formatJavadoc(comment.getContent())); } catch (NoSuchElementException e) { } } }
methodDeclaration.getModifiers(), methodDeclaration.getName(), methodDeclaration.getParameters().stream().map(p -> p.getName()).collect(Collectors.toList()), methodDeclaration.getType())); ClassOrInterfaceDeclaration classDeclaration = compilationUnitNode.getClassByName("A").get(); System.out.println(String.format("Class declaration: name=%s, nMembers=%s", classDeclaration.getName(), classDeclaration.getMembers().size()));
String name = n.getNameAsString(); isGetter = isArraySetter = false; do { // to provide break exit Character.isUpperCase(name.charAt(3)) && !name.equals("getTypeIndexID")) { List<Parameter> ps = n.getParameters(); if (isGetter) { if (ps.size() > 1) break; String bodyString = n.getBody().get().toString(printWithoutComments); int i = bodyString.indexOf("jcasType.ll_cas.ll_"); if (i < 0) break; i = s.indexOf("jcasType.ll_cas.ll_"); if (i < 0) { reportUnrecognizedV2Code("Found \"jcasType.ll_cas.ll_[set or get]...FSForRef(\" but didn't find following \"jcasType.ll_cas_ll_\"\n" + n.toString()); break; reportUnrecognizedV2Code("Found \"jcasType.ll_cas.ll_[set or get]\" but didn't find following \"Value\"\n" + n.toString()); break; // give up reportUnrecognizedV2Code("Didn't find \"...jcasType).casFeatCode_\"\n" + n.toString()); break; reportUnrecognizedV2Code("Found \"...jcasType).casFeatCode_\" but didn't find subsequent word\n" + n.toString()); break; for (Statement stmt : n.getBody().get().getStatements()) {
public void visit(MethodDeclaration n, Void arg) { try { n.getAnnotationByClass(Test.class).get(); } catch (NoSuchElementException e) { return; } javadoc.append(String.format("\nh4. Test: %s\n", n.getNameAsString())); try { Comment comment = n.getComment().get(); javadoc.append(formatJavadoc(comment.getContent())); } catch (NoSuchElementException e) { } } }
@Test public void resolveArrayType() throws ParseException { TypeSolver typeSolver = new ReflectionTypeSolver(); CompilationUnit cu = parseSample("SymbolResolverExample"); new JavaSymbolSolver(typeSolver).inject(cu); MethodDeclaration methodDeclaration = cu.getClassByName("A").get().getMethods().get(0); ResolvedType resolvedType = methodDeclaration.getType().resolve(); assertEquals("A[]", resolvedType.describe()); } }
@Test public void issue113providedCodeDoesNotCrash() throws FileNotFoundException { String pathToSourceFile = adaptPath("src/test/resources/issue113/com/foo/Widget.java"); CompilationUnit cu = JavaParser.parse(new File(pathToSourceFile)); JavaParserFacade parserFacade = JavaParserFacade.get(typeSolver); MethodDeclaration methodDeclaration = cu.getNodesByType(MethodDeclaration.class).stream() .filter(node -> node.getName().getIdentifier().equals("doSomething")).findAny().orElse(null); methodDeclaration.getNodesByType(MethodCallExpr.class).forEach(parserFacade::solve); }
@Override public void visit(final MethodDeclaration m, final Context ctx) { if (!script) { boolean mvc = m.getAnnotations().stream() .map(it -> it.getName().getName()) .filter(Route.METHODS::contains) .findFirst() .isPresent(); if (mvc) { nodes.add(Maps.immutableEntry(m, m.getBody())); } } }
public MethodImp parse(final String signature) { final StringBuilder sb = new StringBuilder(); sb.append("class A {\n"); if (!signature.contains("(")) { sb.append(signature.contains("()") ? signature : signature.concat("()")); } else { sb.append(signature); } if (!signature.contains("{}")) { sb.append(" {}"); } sb.append("\n}"); final MethodImp method = new MethodImp("NOT_DEFINED"); final CompilationUnit compilationUnit = JavaParser.parse(sb.toString()); compilationUnit.getClassByName("A").ifPresent(clazz -> { clazz.getMethods().forEach(md -> { method.name(md.getNameAsString()); method.visibility(this.getVisibility(md)); method.modifiers(this.getModifiers(md)); method.returnType(this.getReturnType(md)); method.exceptions(this.getExceptions(md)); md.getAnnotations().forEach(an -> this.getAnnotationMethod(an) .ifPresent(x -> method.annotation(x))); md.getParameters().forEach(p -> this.getField(p) .ifPresent(x -> method.parameters().add(x))); }); }); return method; }
protected void prepareReturnStmt(MethodDeclaration methodDeclaration, ReturnStmt returnStmt) { returnStmt.getExpression().ifPresent(expression -> { String returnStmtStr = expression.toString(); Matcher matcher = RETURN_STMT_PATTERN.matcher(returnStmtStr); if (!returnMap.containsKey(methodDeclaration.getNameAsString())) { returnMap.put(methodDeclaration.getNameAsString(), DfCollectionUtil.newArrayList()); } returnMap.get(methodDeclaration.getNameAsString()).add(matcher.find() ? matcher.group(0) : "##unanalyzable##"); }); } }