List of usage examples for org.eclipse.jdt.core.dom SwitchCase setExpression
public void setExpression(Expression expression)
From source file:de.dentrassi.varlink.generator.JdtGenerator.java
License:Open Source License
@SuppressWarnings("unchecked") private void createImpl(final AST ast, final CompilationUnit cu, final Interface iface, final String name) { // create type final String implName = name + "Impl"; final TypeDeclaration td = ast.newTypeDeclaration(); cu.types().add(td);//from ww w. j a v a 2 s . c om td.setName(ast.newSimpleName(implName)); make(td, PUBLIC_KEYWORD); final Type parentType = ast.newSimpleType(ast.newName(name)); td.superInterfaceTypes().add(parentType); // create factory createImplFactory(ast, td); // create fields createField(td, "de.dentrassi.varlink.spi.Connection", "connection", PRIVATE_KEYWORD, FINAL_KEYWORD); createField(td, "de.dentrassi.varlink.internal.VarlinkImpl", "varlink", PRIVATE_KEYWORD, FINAL_KEYWORD); // create constructor final MethodDeclaration ctor = ast.newMethodDeclaration(); td.bodyDeclarations().add(ctor); ctor.setConstructor(true); ctor.setName(ast.newSimpleName(implName)); make(ctor, PRIVATE_KEYWORD); createParameter(ctor, "de.dentrassi.varlink.spi.Connection", "connection", FINAL_KEYWORD); createParameter(ctor, "de.dentrassi.varlink.internal.VarlinkImpl", "varlink", FINAL_KEYWORD); // constructor body { final Block body = ast.newBlock(); ctor.setBody(body); createThisAssignment(body, "connection"); createThisAssignment(body, "varlink"); } // error mapper { final MethodDeclaration md = ast.newMethodDeclaration(); make(md, PUBLIC_KEYWORD); td.bodyDeclarations().add(md); md.setName(ast.newSimpleName("checkError")); createParameter(md, "de.dentrassi.varlink.spi.CallResponse", "response", FINAL_KEYWORD); final Block body = ast.newBlock(); md.setBody(body); final MethodInvocation mi = ast.newMethodInvocation(); mi.setExpression(ast.newName("de.dentrassi.varlink.spi.Errors")); mi.setName(ast.newSimpleName("checkErrors")); mi.arguments().add(ast.newSimpleName("response")); final ExpressionMethodReference ref = ast.newExpressionMethodReference(); ref.setExpression(ast.newThisExpression()); ref.setName(ast.newSimpleName("mapError")); mi.arguments().add(ref); body.statements().add(ast.newExpressionStatement(mi)); } { final MethodDeclaration md = ast.newMethodDeclaration(); make(md, PUBLIC_KEYWORD); td.bodyDeclarations().add(md); md.setName(ast.newSimpleName("mapError")); createParameter(md, "java.lang.String", "error", FINAL_KEYWORD); createParameter(md, "de.dentrassi.varlink.spi.CallResponse", "response", FINAL_KEYWORD); md.setReturnType2(ast.newSimpleType(ast.newName("java.lang.RuntimeException"))); final Block body = ast.newBlock(); md.setBody(body); final SwitchStatement sw = ast.newSwitchStatement(); body.statements().add(sw); sw.setExpression(ast.newSimpleName("error")); errors(iface).forEach(error -> { final String errorName = errorTypeName(error); final String fullErrorName = iface.getName() + "." + errorName; final SwitchCase sc = ast.newSwitchCase(); sc.setExpression(JdtHelper.newStringLiteral(ast, fullErrorName)); sw.statements().add(sc); final FieldAccess fa = ast.newFieldAccess(); fa.setExpression(ast.newThisExpression()); fa.setName(ast.newSimpleName("varlink")); final MethodInvocation fromJson = ast.newMethodInvocation(); fromJson.setExpression(fa); fromJson.setName(ast.newSimpleName("fromJson")); // type name final TypeLiteral typeLiteral = ast.newTypeLiteral(); typeLiteral.setType(ast.newSimpleType(ast.newName(errorName + ".Parameters"))); fromJson.arguments().add(typeLiteral); // parameters final MethodInvocation parameters = ast.newMethodInvocation(); parameters.setExpression(ast.newSimpleName("response")); parameters.setName(ast.newSimpleName("getParameters")); fromJson.arguments().add(parameters); // new exception final ClassInstanceCreation cic = ast.newClassInstanceCreation(); cic.setType(ast.newSimpleType(ast.newName(errorName))); cic.arguments().add(fromJson); // return final ReturnStatement ret = ast.newReturnStatement(); ret.setExpression(cic); sw.statements().add(ret); }); { final SwitchCase sc = ast.newSwitchCase(); sc.setExpression(null); sw.statements().add(sc); final ReturnStatement ret = ast.newReturnStatement(); ret.setExpression(ast.newNullLiteral()); sw.statements().add(ret); } } // async creator /* * @Override public Async async() { return new Async() { * * @Override public CompletableFuture<List<Netdev>> list() { return * executeList(); } }; } */ { final MethodDeclaration md = ast.newMethodDeclaration(); td.bodyDeclarations().add(md); md.setName(ast.newSimpleName("async")); addSimpleAnnotation(md, "Override"); make(md, PUBLIC_KEYWORD); md.setReturnType2(ast.newSimpleType(ast.newName("Async"))); final Block body = ast.newBlock(); md.setBody(body); // inner class final ReturnStatement ret = ast.newReturnStatement(); body.statements().add(ret); final ClassInstanceCreation cic = ast.newClassInstanceCreation(); cic.setType(ast.newSimpleType(ast.newName("Async"))); ret.setExpression(cic); final AnonymousClassDeclaration acc = ast.newAnonymousClassDeclaration(); cic.setAnonymousClassDeclaration(acc); forMethods(ast, iface, (m, amd) -> { acc.bodyDeclarations().add(amd); amd.setName(ast.newSimpleName(m.getName())); make(amd, PUBLIC_KEYWORD); makeAsync(amd); final Block asyncBody = ast.newBlock(); amd.setBody(asyncBody); final ReturnStatement asyncRet = ast.newReturnStatement(); asyncBody.statements().add(asyncRet); final MethodInvocation mi = ast.newMethodInvocation(); mi.setName(ast.newSimpleName(internalMethodName(m.getName()))); for (final String argName : m.getParameters().keySet()) { mi.arguments().add(ast.newSimpleName(argName)); } asyncRet.setExpression(mi); }); } // internal methods forMethods(ast, iface, (m, md) -> { make(md, PROTECTED_KEYWORD); td.bodyDeclarations().add(md); md.setName(ast.newSimpleName(internalMethodName(m.getName()))); makeAsync(md); createInternalMethod(td, m, md); }); }
From source file:java5totext.input.JDTVisitor.java
License:Open Source License
@Override public void endVisit(org.eclipse.jdt.core.dom.SwitchCase node) { SwitchCase element = (SwitchCase) this.binding.get(node); this.initializeNode(element, node); element.setDefault(node.isDefault()); if (this.binding.get(node.getExpression()) != null) element.setExpression((Expression) this.binding.get(node.getExpression())); }
From source file:net.sf.eclipsecs.ui.quickfixes.coding.MissingSwitchDefaultQuickfix.java
License:Open Source License
/** * {@inheritDoc}/*w w w . j a v a 2 s . c o m*/ */ protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartOffset) { return new ASTVisitor() { public boolean visit(SwitchStatement node) { if (containsPosition(lineInfo, node.getStartPosition())) { SwitchCase defNode = node.getAST().newSwitchCase(); defNode.setExpression(null); node.statements().add(defNode); node.statements().add(node.getAST().newBreakStatement()); } return true; // also visit children } }; }
From source file:org.asup.dk.compiler.rpj.writer.JDTStatementWriter.java
License:Open Source License
@SuppressWarnings("unchecked") @Override//from w w w . j a v a2 s .co m public boolean visit(QMonitor statement) { Block block = blocks.peek(); // -> try TryStatement tryStatement = ast.newTryStatement(); blocks.push(tryStatement.getBody()); if (statement.getBody() != null) statement.getBody().accept(this); // catch CatchClause catchClause = ast.newCatchClause(); SingleVariableDeclaration exceptionDeclaration = ast.newSingleVariableDeclaration(); Type exception = ast .newSimpleType(ast.newSimpleName(OperatingSystemRuntimeException.class.getSimpleName())); exceptionDeclaration.setType(exception); exceptionDeclaration.setName(ast.newSimpleName("e")); catchClause.setException(exceptionDeclaration); tryStatement.catchClauses().add(catchClause); // -> catch blocks.push(catchClause.getBody()); // switch SwitchStatement switchStatement = ast.newSwitchStatement(); MethodInvocation methodInvocation = ast.newMethodInvocation(); methodInvocation.setExpression(ast.newSimpleName("e")); methodInvocation.setName(ast.newSimpleName("toString")); switchStatement.setExpression(methodInvocation); blocks.peek().statements().add(switchStatement); for (QOnError error : statement.getOnErrors()) { if (error.getBody() != null) { // Case SwitchCase switchCase = ast.newSwitchCase(); StringLiteral caseLiteral = ast.newStringLiteral(); if (error.getError() != null) caseLiteral.setLiteralValue(error.getError()); else caseLiteral.setLiteralValue("*ALL"); switchCase.setExpression(caseLiteral); switchStatement.statements().add(switchCase); // Case body // -> Case Block caseBlock = ast.newBlock(); blocks.push(caseBlock); if (error.getBody() != null) error.getBody().accept(this); // copy case block to switch statement for (int i = 0; i < caseBlock.statements().size(); i++) { switchStatement.statements().add(caseBlock.statements().remove(i)); } BreakStatement switchBreak = ast.newBreakStatement(); caseBlock.statements().add(switchBreak); // <- case blocks.pop(); } } // <-catch blocks.pop(); // <-try blocks.pop(); block.statements().add(tryStatement); return false; }
From source file:org.autorefactor.refactoring.ASTBuilder.java
License:Open Source License
/** * Builds a new {@link SwitchCase} instance. * * @param expr/* w ww . j a va 2 s. c o m*/ * the case expression * @return a new switch case statement */ public SwitchCase case0(Expression expr) { final SwitchCase sc = ast.newSwitchCase(); sc.setExpression(expr); return sc; }
From source file:org.decojer.cavaj.transformers.TrControlFlowStmts.java
License:Open Source License
@Nullable private Statement transformSwitch(@Nonnull final Switch switchStruct) { switch (switchStruct.getKind()) { case NO_DEFAULT: case WITH_DEFAULT: { final BB head = switchStruct.getHead(); final SwitchStatement switchStatement = (SwitchStatement) head.getFinalStmt(); if (switchStatement == null) { assert false; return null; }/*from w w w. j a va2 s . c om*/ final Op op = getOp(switchStatement); for (final E out : head.getOuts()) { if (!out.isSwitchCase()) { continue; } boolean defaultAdded = false; // prevent [null, null] - double defaults final Object value = out.getValue(); if (!(value instanceof Object[])) { assert false; continue; } for (final Object caseValue : (Object[]) value) { if (out.isSwitchDefault() && switchStruct.getKind() == Switch.Kind.NO_DEFAULT) { continue; } final SwitchCase switchCase = setOp(getAst().newSwitchCase(), op); if (caseValue == null) { // necessary: expression initialized to null for default case if (defaultAdded) { continue; } switchCase.setExpression(null); defaultAdded = true; } else { if (caseValue instanceof Character) { switchCase.setExpression(newLiteral(T.CHAR, caseValue, getCfg().getM(), op)); } else if (caseValue instanceof String) { switchCase.setExpression(newLiteral(getCfg().getDu().getT(String.class), caseValue, getCfg().getM(), op)); } else if (caseValue instanceof F) { switchCase.setExpression(newSimpleName(((F) caseValue).getName(), getAst())); } else { switchCase.setExpression(newLiteral(T.INT, caseValue, getCfg().getM(), op)); } } switchStatement.statements().add(switchCase); } final List<Statement> switchStatements = switchStatement.statements(); assert switchStatements != null; transformSequence(switchStruct, out, switchStatements); } // remove final break statement from final switch-case final Object object = switchStatement.statements().get(switchStatement.statements().size() - 1); if (object instanceof BreakStatement) { ((BreakStatement) object).delete(); } return switchStatement; } default: log.warn(getM() + ": Unknown switch type '" + switchStruct.getKind() + "'!"); return null; } }
From source file:org.eclipse.jdt.core.dom.ASTConverter.java
License:Open Source License
public SwitchCase convert(org.eclipse.jdt.internal.compiler.ast.CaseStatement statement) { SwitchCase switchCase = new SwitchCase(this.ast); org.eclipse.jdt.internal.compiler.ast.Expression constantExpression = statement.constantExpression; if (constantExpression == null) { switchCase.setExpression(null); } else {/*from w w w. j av a 2 s .c om*/ switchCase.setExpression(convert(constantExpression)); } switchCase.setSourceRange(statement.sourceStart, statement.sourceEnd - statement.sourceStart + 1); retrieveColonPosition(switchCase); return switchCase; }
From source file:org.eclipse.modisco.java.discoverer.internal.io.java.JDTVisitor.java
License:Open Source License
@Override public void endVisit(final org.eclipse.jdt.core.dom.SwitchCase node) { SwitchCase element = (SwitchCase) this.binding.get(node); initializeNode(element, node);/*from w w w . j a v a 2 s.c om*/ element.setDefault(node.isDefault()); if (this.binding.get(node.getExpression()) != null) { element.setExpression(JDTVisitorUtils.completeExpression(this.binding.get(node.getExpression()), this)); } }
From source file:org.jboss.forge.roaster.model.impl.statements.SwitchStatementImpl.java
License:Open Source License
@Override public org.eclipse.jdt.core.dom.SwitchStatement materialize(AST ast) { if (opts != null) { return opts; }// w w w . java 2 s . c om opts = ast.newSwitchStatement(); opts.setExpression(wireAndGetExpression(expression, this, ast)); for (Statement<O, SwitchStatement<O, P>, ?> stat : statements) { if (SwitchMockStatement.class.isInstance(stat)) { SwitchMockStatement mock = (SwitchMockStatement) stat; SwitchCase opt = ast.newSwitchCase(); if (mock.getOption() == null) { opt.setExpression(null); } else { opt.setExpression(wireAndGetExpression(mock.getOption(), this, ast)); } opts.statements().add(opt); } else { opts.statements().add(wireAndGetStatement(stat, this, ast)); } } return opts; }
From source file:org.smeup.sys.dk.compiler.rpj.writer.JDTStatementWriter.java
License:Open Source License
@SuppressWarnings("unchecked") @Override/*from w w w. ja va 2 s. co m*/ public boolean visit(QMonitor statement) { Block block = blocks.peek(); // -> try TryStatement tryStatement = ast.newTryStatement(); blocks.push(tryStatement.getBody()); if (statement.getBody() != null) statement.getBody().accept(this); String exceptionName = "e" + blocks.size(); // catch CatchClause catchClause = ast.newCatchClause(); SingleVariableDeclaration exceptionDeclaration = ast.newSingleVariableDeclaration(); Type exception = ast .newSimpleType(ast.newSimpleName(OperatingSystemMessageException.class.getSimpleName())); exceptionDeclaration.setType(exception); exceptionDeclaration.setName(ast.newSimpleName(exceptionName)); catchClause.setException(exceptionDeclaration); tryStatement.catchClauses().add(catchClause); // -> catch blocks.push(catchClause.getBody()); // switch SwitchStatement switchStatement = ast.newSwitchStatement(); MethodInvocation methodInvocation = ast.newMethodInvocation(); methodInvocation.setExpression(ast.newSimpleName(exceptionName)); methodInvocation.setName(ast.newSimpleName("getMessageName")); switchStatement.setExpression(methodInvocation); boolean defaultError = true; boolean caseStatement = false; for (QOnError error : statement.getOnErrors()) { if (error.getBody() == null) continue; if (error.getErrors().isEmpty() || error.getErrors().contains("CPF0000") || error.getErrors().contains("*ALL")) { // Case SwitchCase switchCase = ast.newSwitchCase(); switchCase.setExpression(null); switchStatement.statements().add(switchCase); defaultError = false; } else { for (String caseError : error.getErrors()) { StringLiteral caseLiteral = ast.newStringLiteral(); caseLiteral.setLiteralValue(caseError); // Case SwitchCase switchCase = ast.newSwitchCase(); switchCase.setExpression(caseLiteral); switchStatement.statements().add(switchCase); } } // Case body // -> Case Block caseBlock = ast.newBlock(); blocks.push(caseBlock); error.getBody().accept(this); // copy case block to switch statement for (int i = 0; i < caseBlock.statements().size(); i++) { Object temp = caseBlock.statements().remove(i); if (temp instanceof Block) { Block tempBlock = (Block) temp; if (tempBlock.statements().isEmpty()) continue; } caseStatement = true; switchStatement.statements().add(temp); } switchStatement.statements().add(ast.newBreakStatement()); // <- case blocks.pop(); } if (defaultError) { SwitchCase switchCase = ast.newSwitchCase(); switchCase.setExpression(null); switchStatement.statements().add(switchCase); ThrowStatement throwStatement = ast.newThrowStatement(); throwStatement.setExpression(ast.newSimpleName(exceptionName)); switchStatement.statements().add(throwStatement); blocks.peek().statements().add(switchStatement); } else if (caseStatement) { blocks.peek().statements().add(switchStatement); } // <-catch blocks.pop(); // <-try blocks.pop(); block.statements().add(tryStatement); return false; }