本文整理汇总了Java中com.github.javaparser.ast.stmt.ReturnStmt类的典型用法代码示例。如果您正苦于以下问题:Java ReturnStmt类的具体用法?Java ReturnStmt怎么用?Java ReturnStmt使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ReturnStmt类属于com.github.javaparser.ast.stmt包,在下文中一共展示了ReturnStmt类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getNameMethod
import com.github.javaparser.ast.stmt.ReturnStmt; //导入依赖的package包/类
/**
* Creates the Name method
*/
private MethodDeclaration getNameMethod() {
MethodDeclaration getName = new MethodDeclaration(
ModifierSet.PUBLIC,
Collections.singletonList(OVERRIDE_ANNOTATION),
null,
createReferenceType("String", 0),
"getName",
null, 0,
null,
null
);
BlockStmt methodBody = new BlockStmt(
Collections.singletonList(new ReturnStmt(
new StringLiteralExpr("CV " + definedMethod.getMethodName())))
);
getName.setBody(methodBody);
return getName;
}
示例2: getDescriptionMethod
import com.github.javaparser.ast.stmt.ReturnStmt; //导入依赖的package包/类
/**
* Creates the description method
*/
private MethodDeclaration getDescriptionMethod() {
MethodDeclaration getDescription = new MethodDeclaration(
ModifierSet.PUBLIC,
Collections.singletonList(OVERRIDE_ANNOTATION),
null,
createReferenceType("String", 0),
"getDescription",
null, 0,
null,
null
);
BlockStmt methodBody = new BlockStmt(
Collections.singletonList(new ReturnStmt(
new StringLiteralExpr(this.definedMethod.getDescription())))
);
getDescription.setBody(methodBody);
return getDescription;
}
示例3: getInputOrOutputSocketBody
import com.github.javaparser.ast.stmt.ReturnStmt; //导入依赖的package包/类
private BlockStmt getInputOrOutputSocketBody(List<DefinedParamType> paramTypes,
ClassOrInterfaceType socketType, String
inputOrOutputPostfix) {
List<Expression> passedExpressions = new ArrayList<>();
for (DefinedParamType inputParam : paramTypes) {
if (inputParam.isIgnored()) {
continue;
}
passedExpressions.add(getSocketListParam(inputParam, socketType, inputOrOutputPostfix));
}
BlockStmt returnStatement = new BlockStmt(
Arrays.asList(new ReturnStmt(
new ArrayCreationExpr(socketType, 1, new ArrayInitializerExpr(passedExpressions)))
)
);
return returnStatement;
}
示例4: solveParameterOfLambdaInMethodCallExpr
import com.github.javaparser.ast.stmt.ReturnStmt; //导入依赖的package包/类
@Test
public void solveParameterOfLambdaInMethodCallExpr() throws ParseException {
CompilationUnit cu = parseSample("Lambda");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Agenda");
MethodDeclaration method = Navigator.demandMethod(clazz, "lambdaMap");
ReturnStmt returnStmt = Navigator.findReturnStmt(method);
MethodCallExpr methodCallExpr = (MethodCallExpr) returnStmt.getExpression().get();
LambdaExpr lambdaExpr = (LambdaExpr) methodCallExpr.getArguments().get(0);
Context context = new LambdaExprContext(lambdaExpr, typeSolver);
Optional<Value> ref = context.solveSymbolAsValue("p", typeSolver);
assertTrue(ref.isPresent());
assertEquals("? super java.lang.String", ref.get().getType().describe());
}
示例5: genericCollectionWithWildcardsAndExtensions
import com.github.javaparser.ast.stmt.ReturnStmt; //导入依赖的package包/类
@Test
public void genericCollectionWithWildcardsAndExtensions() throws ParseException {
CompilationUnit cu = parseSample("GenericCollectionWithExtension");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Foo");
MethodDeclaration method = Navigator.demandMethod(clazz, "bar");
ReturnStmt returnStmt = Navigator.findReturnStmt(method);
TypeSolver typeSolver = new ReflectionTypeSolver();
Expression returnStmtExpr = returnStmt.getExpression().get();
JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
ResolvedType type = javaParserFacade.getType(returnStmtExpr);
assertEquals(false, type.isTypeVariable());
assertEquals("boolean", type.describe());
}
示例6: genericCollectionWithWildcards
import com.github.javaparser.ast.stmt.ReturnStmt; //导入依赖的package包/类
@Test
public void genericCollectionWithWildcards() throws ParseException {
CompilationUnit cu = parseSample("GenericCollection");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Foo");
MethodDeclaration method = Navigator.demandMethod(clazz, "bar");
ReturnStmt returnStmt = Navigator.findReturnStmt(method);
TypeSolver typeSolver = new ReflectionTypeSolver();
Expression returnStmtExpr = returnStmt.getExpression().get();
JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
ResolvedType type = javaParserFacade.getType(returnStmtExpr);
assertEquals(false, type.isTypeVariable());
assertEquals("boolean", type.describe());
}
示例7: lambdaMap
import com.github.javaparser.ast.stmt.ReturnStmt; //导入依赖的package包/类
@Test
public void lambdaMap() throws ParseException {
CompilationUnit cu = parseSample("Lambda");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Agenda");
MethodDeclaration m1 = Navigator.demandMethod(clazz, "lambdaMap");
MethodDeclaration m2 = Navigator.demandMethod(clazz, "lambdaMap2");
ReturnStmt returnStmt1 = Navigator.findReturnStmt(m1);
ReturnStmt returnStmt2 = Navigator.findReturnStmt(m2);
Expression e1 = returnStmt1.getExpression().get();
Expression e2 = returnStmt2.getExpression().get();
JavaParserFacade javaParserFacade = JavaParserFacade.get(new ReflectionTypeSolver());
ResolvedType type1 = javaParserFacade.getType(e1);
ResolvedType type2 = javaParserFacade.getType(e2);
assertEquals("java.util.stream.Stream<java.lang.String>", type1.describe());
assertEquals("java.util.stream.Stream<java.util.stream.IntStream>", type2.describe());
}
示例8: createGetter
import com.github.javaparser.ast.stmt.ReturnStmt; //导入依赖的package包/类
/**
* Create a getter for this field, <b>will only work if this field declares only 1 identifier and if this field is
* already added to a ClassOrInterfaceDeclaration</b>
*
* @return the {@link MethodDeclaration} created
* @throws IllegalStateException if there is more than 1 variable identifier or if this field isn't attached to a
* class or enum
*/
public MethodDeclaration createGetter() {
if (getVariables().size() != 1)
throw new IllegalStateException("You can use this only when the field declares only 1 variable name");
ClassOrInterfaceDeclaration parentClass = getParentNodeOfType(ClassOrInterfaceDeclaration.class);
EnumDeclaration parentEnum = getParentNodeOfType(EnumDeclaration.class);
if ((parentClass == null && parentEnum == null) || (parentClass != null && parentClass.isInterface()))
throw new IllegalStateException(
"You can use this only when the field is attached to a class or an enum");
VariableDeclarator variable = getVariables().get(0);
String fieldName = variable.getId().getName();
String fieldNameUpper = fieldName.toUpperCase().substring(0, 1) + fieldName.substring(1, fieldName.length());
final MethodDeclaration getter;
if (parentClass != null)
getter = parentClass.addMethod("get" + fieldNameUpper, PUBLIC);
else
getter = parentEnum.addMethod("get" + fieldNameUpper, PUBLIC);
getter.setType(variable.getType());
BlockStmt blockStmt = new BlockStmt();
getter.setBody(blockStmt);
blockStmt.addStatement(new ReturnStmt(name(fieldName)));
return getter;
}
示例9: appearsInAssignmentContext
import com.github.javaparser.ast.stmt.ReturnStmt; //导入依赖的package包/类
private static boolean appearsInAssignmentContext(Expression expression) {
if (expression.getParentNode().isPresent()) {
Node parent = expression.getParentNode().get();
if (parent instanceof ExpressionStmt) {
return false;
}
if (parent instanceof MethodCallExpr) {
return false;
}
if (parent instanceof ReturnStmt) {
return false;
}
throw new UnsupportedOperationException(parent.getClass().getCanonicalName());
}
return false;
}
示例10: getterDeclaration
import com.github.javaparser.ast.stmt.ReturnStmt; //导入依赖的package包/类
private MethodDeclaration getterDeclaration(EntityField field) {
MethodDeclaration decl = new MethodDeclaration(ModifierSet.PUBLIC,
ASTHelper.createReferenceType(field.getType().getSimpleName(), 0),
"get" + CaseConverter.pascalCase(field.getName()));
BlockStmt body = new BlockStmt();
body.setStmts(
Collections.singletonList(
new ReturnStmt(
new FieldAccessExpr(new ThisExpr(), field.getName()))));
decl.setBody(body);
return decl;
}
示例11: execute
import com.github.javaparser.ast.stmt.ReturnStmt; //导入依赖的package包/类
public void execute(PathResolver pathResolver) throws Exception {
CompilationUnit cu = new CompilationUnit();
cu.setPackage(new PackageDeclaration(ASTHelper.createNameExpr(pkgName)));
cu.setImports(Arrays.asList(
new ImportDeclaration(ASTHelper.createNameExpr("org.seasar.doma.jdbc.Config"), false, false),
new ImportDeclaration(ASTHelper.createNameExpr("org.seasar.doma.jdbc.dialect.Dialect"), false, false),
new ImportDeclaration(ASTHelper.createNameExpr("org.seasar.doma.jdbc.dialect.H2Dialect"), false, false),
new ImportDeclaration(ASTHelper.createNameExpr("javax.sql.DataSource"), false, false)
));
ClassOrInterfaceDeclaration type = new ClassOrInterfaceDeclaration(ModifierSet.PUBLIC, false, "DomaConfig");
type.setImplements(Collections.singletonList(new ClassOrInterfaceType("Config")));
ASTHelper.addTypeDeclaration(cu, type);
MethodDeclaration getDataSourceMethod = new MethodDeclaration(ModifierSet.PUBLIC, ASTHelper.createReferenceType("DataSource", 0), "getDataSource");
getDataSourceMethod.setAnnotations(Collections.singletonList(OVERRIDE_ANNOTATION));
BlockStmt getDataSourceBody = new BlockStmt();
ASTHelper.addStmt(getDataSourceBody, new ReturnStmt(new NullLiteralExpr()));
getDataSourceMethod.setBody(getDataSourceBody);
ASTHelper.addMember(type, getDataSourceMethod);
MethodDeclaration getDialectMethod = new MethodDeclaration(ModifierSet.PUBLIC, ASTHelper.createReferenceType("Dialect", 0), "getDialect");
getDialectMethod.setAnnotations(Collections.singletonList(OVERRIDE_ANNOTATION));
BlockStmt getDialectBody = new BlockStmt();
ObjectCreationExpr newDialect = new ObjectCreationExpr(null, new ClassOrInterfaceType("H2Dialect"), null);
ASTHelper.addStmt(getDialectBody, new ReturnStmt(newDialect));
getDialectMethod.setBody(getDialectBody);
ASTHelper.addMember(type, getDialectMethod);
try (OutputStreamWriter writer = new OutputStreamWriter(
pathResolver.destinationAsStream(destination))) {
writer.write(cu.toString());
}
}
示例12: visit
import com.github.javaparser.ast.stmt.ReturnStmt; //导入依赖的package包/类
@Override
public ResultType visit( MethodDeclaration declaration, MethodDeclaration arg ) {
List<Statement> statements = declaration.getBody().getStmts();
Statement lastStatement = statements.get( statements.size() - 1 );
if ( lastStatement instanceof ReturnStmt ) try {
Expression expr = ( ( ReturnStmt ) lastStatement ).getExpr();
if ( expr instanceof ClassExpr )
// we could return the correct class type here, but that would cause misleading auto-completions
// because the runtime of the expression is a Class without with the type parameter erased
return new ResultType( Class.class, false );
else if ( expr instanceof ObjectCreationExpr )
return new ResultType( typeDiscoverer.classForName(
( ( ObjectCreationExpr ) expr ).getType().getName() ), false );
else if ( expr instanceof ArrayCreationExpr )
return new ResultType( Array.class, false );
else if ( expr.getClass().equals( StringLiteralExpr.class ) )
return new ResultType( String.class, false );
else if ( expr.getClass().equals( NameExpr.class ) ) {
Map<String, Class<?>> typeByVariableName = typeDiscoverer.getTypesByVariableName( statements );
String name = ( ( NameExpr ) expr ).getName();
@Nullable Class<?> variableType = typeByVariableName.get( name );
if ( variableType == null ) {
// attempt to return a class matching the apparent-variable name
return new ResultType( typeDiscoverer.classForName( name ), true );
} else {
return new ResultType( variableType, false );
}
} else
return ResultType.VOID;
} catch ( ClassNotFoundException ignore ) {
// class does not exist
}
return ResultType.VOID;
}
示例13: visit
import com.github.javaparser.ast.stmt.ReturnStmt; //导入依赖的package包/类
@Override public void visit(ReturnStmt n, Void arg) {
Optional<Expression> expression = n.getExpression();
if(expression.isPresent()) {
Expression xp = expression.get();
if(isOldFieldName(xp.toString())) {
n.setExpression(new NameExpr(m_fieldName));
}
}
}
示例14: visit
import com.github.javaparser.ast.stmt.ReturnStmt; //导入依赖的package包/类
@Override
public void visit(final ReturnStmt n, final Object arg) {
printer.printLn("ReturnStmt");
printJavaComment(n.getComment(), arg);
printer.print("return");
if (n.getExpr() != null) {
printer.print(" ");
n.getExpr().accept(this, arg);
}
printer.print(";");
}
示例15: visit
import com.github.javaparser.ast.stmt.ReturnStmt; //导入依赖的package包/类
@Override
public JCTree visit(final ReturnStmt n, final Object arg) {
//ARG0: JCExpression expr
JCExpression arg0 = null;
if (n.getExpr() != null) {
arg0 = (JCExpression) n.getExpr().accept(this, arg);
}
return new AJCReturn(make.Return(arg0), ((n.getComment() != null) ? n.getComment().getContent() : null));
}