List of usage examples for org.eclipse.jdt.core.dom Block statements
ASTNode.NodeList statements
To view the source code for org.eclipse.jdt.core.dom Block statements.
Click Source Link
From source file:net.sf.j2s.core.astvisitors.ASTJ2SDocVisitor.java
License:Open Source License
private int getPreviousStartPosition(Block node) { int previousStart = 0; ASTNode blockParent = node.getParent(); if (blockParent != null) { if (blockParent instanceof Statement) { Statement sttmt = (Statement) blockParent; previousStart = sttmt.getStartPosition(); if (sttmt instanceof Block) { Block parentBlock = (Block) sttmt; for (Iterator iter = parentBlock.statements().iterator(); iter.hasNext();) { Statement element = (Statement) iter.next(); if (element == node) { break; }/*from www.j a va2 s. c o m*/ previousStart = element.getStartPosition() + element.getLength(); } } else if (sttmt instanceof IfStatement) { IfStatement ifSttmt = (IfStatement) sttmt; if (ifSttmt.getElseStatement() == node) { Statement thenSttmt = ifSttmt.getThenStatement(); previousStart = thenSttmt.getStartPosition() + thenSttmt.getLength(); } } } else if (blockParent instanceof MethodDeclaration) { MethodDeclaration method = (MethodDeclaration) blockParent; previousStart = method.getStartPosition(); } else if (blockParent instanceof Initializer) { Initializer initializer = (Initializer) blockParent; previousStart = initializer.getStartPosition(); } else if (blockParent instanceof CatchClause) { CatchClause catchClause = (CatchClause) blockParent; previousStart = catchClause.getStartPosition(); } } return previousStart; }
From source file:net.sf.j2s.core.astvisitors.ASTScriptVisitor.java
License:Open Source License
public boolean visit(MethodDeclaration node) { // methodBuffer = new StringBuffer(); if (getJ2STag(node, "@j2sIgnore") != null) { return false; }/*from ww w . j av a 2s . c om*/ IMethodBinding mBinding = node.resolveBinding(); if (Bindings.isMethodInvoking(mBinding, "net.sf.j2s.ajax.SimpleRPCRunnable", "ajaxRun")) { if (getJ2STag(node, "@j2sKeep") == null) { return false; } } String[] pipeMethods = new String[] { "pipeSetup", "pipeThrough", "through", "pipeMonitoring", "pipeMonitoringInterval", "pipeWaitClosingInterval", "setPipeHelper" }; for (int i = 0; i < pipeMethods.length; i++) { if (Bindings.isMethodInvoking(mBinding, "net.sf.j2s.ajax.SimplePipeRunnable", pipeMethods[i])) { if (getJ2STag(node, "@j2sKeep") == null) { return false; } } } if (Bindings.isMethodInvoking(mBinding, "net.sf.j2s.ajax.CompoundPipeSession", "convert")) { if (getJ2STag(node, "@j2sKeep") == null) { return false; } } if (mBinding != null) { methodDeclareStack.push(mBinding.getKey()); } if (node.getBody() == null) { /* * Abstract or native method */ if (isMethodNativeIgnored(node)) { return false; } } /* * To skip those methods or constructors which are just overriding with * default super methods or constructors. */ Block body = node.getBody(); boolean needToCheckArgs = false; List argsList = null; if (body != null && containsOnlySuperCall(body)) { List sts = body.statements(); Object statement = sts.get(sts.size() - 1); if (statement instanceof ReturnStatement) { ReturnStatement ret = (ReturnStatement) statement; Expression exp = ret.getExpression(); if (exp instanceof SuperMethodInvocation) { SuperMethodInvocation superRet = (SuperMethodInvocation) exp; if (superRet.getName().toString().equals(node.getName().toString())) { // same method name needToCheckArgs = true; argsList = superRet.arguments(); } } } else if (statement instanceof ExpressionStatement) { ExpressionStatement sttmt = (ExpressionStatement) statement; Expression exp = sttmt.getExpression(); if (exp instanceof SuperMethodInvocation) { SuperMethodInvocation superRet = (SuperMethodInvocation) exp; if (superRet.getName().toString().equals(node.getName().toString())) { // same method name needToCheckArgs = true; argsList = superRet.arguments(); } } } else if (statement instanceof SuperConstructorInvocation) { SuperConstructorInvocation superConstructor = (SuperConstructorInvocation) statement; needToCheckArgs = true; argsList = superConstructor.arguments(); if (argsList.size() == 0) { IMethodBinding constructorBinding = superConstructor.resolveConstructorBinding(); ITypeBinding declaringClass = constructorBinding.getDeclaringClass(); if ("java.lang.Object".equals(declaringClass.getQualifiedName())) { needToCheckArgs = false; } } } // } else if (node.isConstructor() && (body != null && body.statements().size() == 0)) { // IMethodBinding superConstructorExisted = Bindings.findConstructorInHierarchy(mBinding.getDeclaringClass(), mBinding); // if (superConstructorExisted != null) { // needToCheckArgs = true; // argsList = new ArrayList(); // } } if (needToCheckArgs) { List params = node.parameters(); if (params.size() == argsList.size()) { // same parameters count boolean isOnlySuper = true; for (Iterator iter = params.iterator(), itr = argsList.iterator(); iter.hasNext();) { ASTNode astNode = (ASTNode) iter.next(); ASTNode argNode = (ASTNode) itr.next(); if (astNode instanceof SingleVariableDeclaration && argNode instanceof SimpleName) { SingleVariableDeclaration varDecl = (SingleVariableDeclaration) astNode; String paramID = varDecl.getName().getIdentifier(); String argID = ((SimpleName) argNode).getIdentifier(); if (!paramID.equals(argID)) { // not with the same order, break out isOnlySuper = false; break; } } else { isOnlySuper = false; break; } } if (isOnlySuper && getJ2STag(node, "@j2sKeep") == null) { return false; } } } if ((node.getModifiers() & Modifier.PRIVATE) != 0) { if (mBinding != null) { boolean isReferenced = MethodReferenceASTVisitor.checkReference(node.getRoot(), mBinding.getKey()); if (!isReferenced && getJ2STag(node, "@j2sKeep") == null) { return false; } } } if (node.isConstructor()) { if (getJ2STag(node, "@j2sOverride") != null) { buffer.append("Clazz.overrideConstructor ("); } else { buffer.append("Clazz.makeConstructor ("); } } else { if ((node.getModifiers() & Modifier.STATIC) != 0) { /* replace full class name with short variable name */ buffer.append("cla$$"); //buffer.append(fullClassName); buffer.append("."); //buffer.append(methods[i].getName()); node.getName().accept(this); buffer.append(" = "); } if (getJ2STag(node, "@j2sOverride") != null) { buffer.append("Clazz.overrideMethod ("); } else { boolean isOK2AutoOverriding = canAutoOverride(node); if (isOK2AutoOverriding) { buffer.append("Clazz.overrideMethod ("); } else { buffer.append("Clazz.defineMethod ("); } } } /* replace full class name with short variable name */ buffer.append("cla$$"); if (node.isConstructor()) { buffer.append(", "); } else { buffer.append(", \""); String identifier = getJ2SName(node.getName()); if (checkKeyworkViolation(identifier)) { buffer.append('$'); } buffer.append(identifier); buffer.append("\", "); } buffer.append("\r\n"); boolean isPrivate = (node.getModifiers() & Modifier.PRIVATE) != 0; if (isPrivate) { buffer.append("($fz = "); } buffer.append("function ("); List parameters = node.parameters(); visitList(parameters, ", "); buffer.append(") "); if (node.isConstructor()) { boolean isSuperCalled = false; List statements = node.getBody().statements(); if (statements.size() > 0) { ASTNode firstStatement = (ASTNode) statements.get(0); if (firstStatement instanceof SuperConstructorInvocation || firstStatement instanceof ConstructorInvocation) { isSuperCalled = true; } } if (getJ2STag(node, "@j2sIgnoreSuperConstructor") != null) { isSuperCalled = true; } boolean existedSuperClass = false; IMethodBinding binding = node.resolveBinding(); if (binding != null) { ITypeBinding declaringClass = binding.getDeclaringClass(); ITypeBinding superclass = declaringClass.getSuperclass(); String qualifiedName = discardGenericType(superclass.getQualifiedName()); existedSuperClass = superclass != null && !"java.lang.Object".equals(qualifiedName) && !"java.lang.Enum".equals(qualifiedName); } if (!isSuperCalled && existedSuperClass) { buffer.append("{\r\n"); buffer.append("Clazz.superConstructor (this, "); buffer.append(assureQualifiedName(shortenQualifiedName(getFullClassName()))); boolean constructorVarargs = isConstructorVarargs(binding, true); if (constructorVarargs) { buffer.append(", [[]]);\r\n"); } else { buffer.append(", []);\r\n"); } boolean read = checkJ2STags(node, false); if (!read) { blockLevel++; visitList(statements, ""); //buffer.append("}"); endVisit(node.getBody()); } else { buffer.append("}"); } } else { boolean read = checkJ2STags(node, true); if (!read) { node.getBody().accept(this); } } } else if (node.getBody() == null) { blockLevel++; boolean read = checkJ2STags(node, true); if (!read) { buffer.append("{\r\n"); visitNativeJavadoc(node.getJavadoc(), null, false); buffer.append("}"); } List normalVars = ((ASTVariableVisitor) getAdaptable(ASTVariableVisitor.class)).normalVars; for (int i = normalVars.size() - 1; i >= 0; i--) { ASTFinalVariable var = (ASTFinalVariable) normalVars.get(i); if (var.blockLevel >= blockLevel) { normalVars.remove(i); } } blockLevel--; } else { boolean read = checkJ2STags(node, true); if (!read) { node.getBody().accept(this); } } if (isPrivate) { buffer.append(", $fz.isPrivate = true, $fz)"); } if (parameters.size() != 0) { buffer.append(", \""); for (Iterator iter = parameters.iterator(); iter.hasNext();) { SingleVariableDeclaration element = (SingleVariableDeclaration) iter.next(); boolean isArray = false; IBinding resolveBinding = element.getName().resolveBinding(); if (resolveBinding instanceof IVariableBinding) { IVariableBinding varBinding = (IVariableBinding) resolveBinding; if (varBinding != null) { isArray = varBinding.getType().isArray(); if (isArray) { //buffer.append("Array"); buffer.append("~A"); } } } if (!isArray) { Type type = element.getType(); if (type.isPrimitiveType()) { PrimitiveType pType = (PrimitiveType) type; if (pType.getPrimitiveTypeCode() == PrimitiveType.BOOLEAN) { buffer.append("~B"); // Boolean } else if (pType.getPrimitiveTypeCode() == PrimitiveType.CHAR) { buffer.append("~S"); // String for char } else { buffer.append("~N"); // Number } } else if (type.isArrayType()) { buffer.append("~A"); // Array } else { ITypeBinding binding = type.resolveBinding(); if (binding != null) { if (binding.isTypeVariable()) { buffer.append("~O"); } else { String name = binding.getQualifiedName(); name = shortenQualifiedName(name); if ("String".equals(name)) { buffer.append("~S"); } else if ("Object".equals(name)) { buffer.append("~O"); } else { buffer.append(name); } } } else { buffer.append(type); } } } if (iter.hasNext()) { buffer.append(","); } } buffer.append("\""); } buffer.append(");\r\n"); return false; }
From source file:net.sf.j2s.core.astvisitors.ASTScriptVisitor.java
License:Open Source License
private boolean containsOnlySuperCall(Block body) { boolean isOnlyOneCall = false; List ss = body.statements(); int size = ss.size(); if (size == 1) { isOnlyOneCall = true;/* w w w . j a va2 s.co m*/ } else { /* * If all method invocations before super call is filtered, then super call * is still considered as the only one. * * For example, the filtered methods may be: * checkWidget(); * checkDevice(); */ String[] filterMethods = getFilterMethods(); if (filterMethods.length > 0 && size > 1) { Object obj = ss.get(size - 1); if (obj instanceof ExpressionStatement) { ExpressionStatement smt = (ExpressionStatement) obj; Expression e = smt.getExpression(); if (e instanceof SuperMethodInvocation) { // the last is super call isOnlyOneCall = true; for (int i = 0; i < size - 1; i++) { // check previous calls Object statement = ss.get(i); MethodInvocation method = null; if (statement instanceof ExpressionStatement) { ExpressionStatement sttmt = (ExpressionStatement) statement; Expression exp = sttmt.getExpression(); if (exp instanceof MethodInvocation) { method = (MethodInvocation) exp; } } else if (statement instanceof IfStatement) { // if (...) checkWidget(); IfStatement ifSss = (IfStatement) statement; if (ifSss.getElseStatement() == null) { Statement thenStatement = ifSss.getThenStatement(); if (thenStatement instanceof Block) { Block block = (Block) thenStatement; List statements = block.statements(); if (statements.size() == 1) { thenStatement = (Statement) statements.get(0); } } if (thenStatement instanceof ExpressionStatement) { ExpressionStatement expStmt = (ExpressionStatement) thenStatement; Expression exp = expStmt.getExpression(); if (exp instanceof MethodInvocation) { method = (MethodInvocation) exp; } } } } if (method != null) { boolean isFiltered = false; IMethodBinding methodBinding = method.resolveMethodBinding(); for (int j = 0; j < filterMethods.length; j += 2) { if ("*".equals(filterMethods[i + 1])) { if (methodBinding == null) { continue; } ITypeBinding type = methodBinding.getDeclaringClass(); if (type != null && filterMethods[i].equals(type.getQualifiedName())) { isFiltered = true; break; } } else if (Bindings.isMethodInvoking(methodBinding, filterMethods[j], filterMethods[j + 1])) { isFiltered = true; break; } } if (isFiltered) { continue; } } isOnlyOneCall = false; break; } } } } } return isOnlyOneCall; }
From source file:net.sf.j2s.core.astvisitors.SWTDependencyASTVisitor.java
License:Open Source License
public boolean visit(IfStatement node) { if (node.getElseStatement() == null) { Statement thenStatement = node.getThenStatement(); if (thenStatement instanceof Block) { Block block = (Block) thenStatement; List statements = block.statements(); if (statements.size() == 1) { thenStatement = (Statement) statements.get(0); }//from w w w. jav a 2s .c o m } if (thenStatement instanceof ExpressionStatement) { ExpressionStatement expStmt = (ExpressionStatement) thenStatement; Expression exp = expStmt.getExpression(); if (isMethodInvoking(exp, "org.eclipse.swt.widgets.Widget", "error")) { return false; } if (isMethodInvoking(exp, "org.eclipse.swt.SWT", "error")) { return false; } if (isMethodInvoking(exp, "org.eclipse.swt.widgets.Display", "error")) { return false; } } } return super.visit(node); }
From source file:net.sf.j2s.core.astvisitors.SWTScriptVisitor.java
License:Open Source License
public boolean visit(Block node) { int swtBlockWhileCount = 0; int swtDialogOpenCount = 0; boolean lastSWTBlockWhile = metSWTBlockWhile; metSWTBlockWhile = false;/*from ww w . j a va 2s . c o m*/ boolean lastDialogOpen = metDialogOpen; metDialogOpen = false; if (super.visit(node) == false) { metSWTBlockWhile = lastSWTBlockWhile; metDialogOpen = lastDialogOpen; return false; } List statements = node.statements(); for (Iterator iter = statements.iterator(); iter.hasNext();) { Statement stmt = (Statement) iter.next(); if (stmt instanceof ExpressionStatement) { ExpressionStatement expStmt = (ExpressionStatement) stmt; Expression exp = expStmt.getExpression(); String[] filterMethods = getFilterMethods(); boolean isContinue = false; for (int i = 0; i < filterMethods.length; i += 2) { if ("*".equals(filterMethods[i + 1])) { continue; } else if (Bindings.isMethodInvoking(exp, filterMethods[i], filterMethods[i + 1])) { isContinue = true; break; } } if (isContinue) { continue; } } stmt.accept(this); if (metSWTBlockWhile) { swtBlockWhileCount++; metSWTBlockWhile = false; } if (metDialogOpen) { swtDialogOpenCount++; metDialogOpen = false; } } for (int i = 0; i < swtBlockWhileCount + swtDialogOpenCount; i++) { buffer.append("});\r\n"); buffer.append("return;\r\n"); /* always return directly when dialog#open is called */ } metSWTBlockWhile = lastSWTBlockWhile; metDialogOpen = lastDialogOpen; return false; }
From source file:net.sf.j2s.core.astvisitors.SWTScriptVisitor.java
License:Open Source License
public boolean visit(IfStatement node) { if (node.getElseStatement() == null) { Statement thenStatement = node.getThenStatement(); if (thenStatement instanceof Block) { Block block = (Block) thenStatement; List statements = block.statements(); if (statements.size() == 1) { thenStatement = (Statement) statements.get(0); }/*from w ww .j a va 2 s .com*/ } if (thenStatement instanceof ExpressionStatement) { ExpressionStatement expStmt = (ExpressionStatement) thenStatement; Expression exp = expStmt.getExpression(); if (Bindings.isMethodInvoking(exp, "org.eclipse.swt.widgets.Widget", "error")) { return false; } if (Bindings.isMethodInvoking(exp, "org.eclipse.swt.SWT", "error")) { return false; } if (Bindings.isMethodInvoking(exp, "org.eclipse.swt.widgets.Display", "error")) { return false; } } } return super.visit(node); }
From source file:nl.han.ica.core.issue.solver.EncapsulateFieldSolver.java
@SuppressWarnings("unchecked") private MethodDeclaration createGetter(AST ast, FieldDeclaration field, Parameter getterNameParameter) { VariableDeclarationFragment fragment = (VariableDeclarationFragment) field.fragments().get(0); String fieldName = fragment.getName().toString(); String getterName = (String) getterNameParameter.getValue(); if (getterName.isEmpty()) { getterName = "get" + WordUtils.capitalize(fieldName); getterNameParameter.setValue(getterName); }//from w w w . j av a2 s.com MethodDeclaration method = ast.newMethodDeclaration(); method.setReturnType2((Type) ASTNode.copySubtree(ast, field.getType())); method.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC)); ReturnStatement returnStatement = ast.newReturnStatement(); returnStatement.setExpression(ast.newName(fieldName)); Block getterBlock = ast.newBlock(); getterBlock.statements().add(returnStatement); method.setBody(getterBlock); method.setName(ast.newSimpleName(getterName)); return method; }
From source file:nl.han.ica.core.issue.solver.EncapsulateFieldSolver.java
@SuppressWarnings("unchecked") private MethodDeclaration createSetter(AST ast, FieldDeclaration field, Parameter setterNameParameter) { MethodDeclaration method = ast.newMethodDeclaration(); VariableDeclarationFragment fragment = (VariableDeclarationFragment) field.fragments().get(0); String fieldName = fragment.getName().toString(); String setterName = (String) setterNameParameter.getValue(); if (setterName.isEmpty()) { setterName = "set" + WordUtils.capitalize(fieldName); setterNameParameter.setValue(setterName); }// w w w . j a v a2 s .co m method.setReturnType2(null); method.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC)); SingleVariableDeclaration singleVariableDeclaration = ast.newSingleVariableDeclaration(); singleVariableDeclaration.setType((Type) ASTNode.copySubtree(ast, field.getType())); singleVariableDeclaration.setName(ast.newSimpleName(fieldName)); method.parameters().add(singleVariableDeclaration); Block setterBlock = ast.newBlock(); Assignment assignment = ast.newAssignment(); FieldAccess fieldAccess = ast.newFieldAccess(); fieldAccess.setName(ast.newSimpleName(fieldName)); fieldAccess.setExpression(ast.newThisExpression()); assignment.setLeftHandSide(fieldAccess); assignment.setRightHandSide(ast.newSimpleName(fieldName)); ExpressionStatement expressionStatement = ast.newExpressionStatement(assignment); setterBlock.statements().add(expressionStatement); method.setBody(setterBlock); method.setName(ast.newSimpleName(setterName)); return method; }
From source file:nz.ac.massey.cs.care.refactoring.executers.IntroduceFactoryRefactoring.java
License:Open Source License
/** * Creates and returns a new MethodDeclaration that represents the factory * method to be used in place of direct calls to the constructor in question. * @param ast An AST used as a factory for various AST nodes * @param ctorBinding binding for the constructor being wrapped * @param unitRewriter the ASTRewrite to be used * @return the new method declaration/*from www .ja v a 2 s . c o m*/ */ private MethodDeclaration createFactoryMethod(AST ast, IMethodBinding ctorBinding, ASTRewrite unitRewriter) { MethodDeclaration newMethod = ast.newMethodDeclaration(); SimpleName newMethodName = ast.newSimpleName(fNewMethodName); ClassInstanceCreation newCtorCall = ast.newClassInstanceCreation(); ReturnStatement ret = ast.newReturnStatement(); Block body = ast.newBlock(); List<Statement> stmts = body.statements(); String retTypeName = null; if (fReturnType != null) { retTypeName = fReturnType; } else { retTypeName = "java.lang.Object";//ctorBinding.getName(); } createFactoryMethodSignature(ast, newMethod); newMethod.setName(newMethodName); newMethod.setBody(body); ITypeBinding[] ctorOwnerTypeParameters = fCtorBinding.getDeclaringClass().getTypeParameters(); setMethodReturnType(newMethod, retTypeName, ctorOwnerTypeParameters, ast); newMethod.modifiers().addAll(ASTNodeFactory.newModifiers(ast, Modifier.STATIC | Modifier.PUBLIC)); setCtorTypeArguments(newCtorCall, ctorBinding.getName(), ctorOwnerTypeParameters, ast); createFactoryMethodConstructorArgs(ast, newCtorCall); ret.setExpression(newCtorCall); stmts.add(ret); return newMethod; }
From source file:org.asup.dk.compiler.rpj.writer.JDTCallableUnitWriter.java
License:Open Source License
@SuppressWarnings("unchecked") public void writePrototype(QPrototype<?> prototype) { MethodDeclaration methodDeclaration = getAST().newMethodDeclaration(); getTarget().bodyDeclarations().add(methodDeclaration); methodDeclaration/* www .j av a2s. c om*/ .setName(getAST().newSimpleName(getCompilationUnit().normalizeTermName(prototype.getName()))); methodDeclaration.modifiers().add(getAST().newModifier(ModifierKeyword.PUBLIC_KEYWORD)); // writeSuppressWarning(methodDeclaration); if (prototype.getDelegate() != null) { Type type = getJavaType(prototype.getDelegate()); methodDeclaration.setReturnType2(type); } if (prototype.getEntry() != null) { int p = 0; for (QEntryParameter<?> entryParameter : prototype.getEntry().getParameters()) { QTerm parameterDelegate = entryParameter.getDelegate(); SingleVariableDeclaration singleVar = getAST().newSingleVariableDeclaration(); String parameterName = parameterDelegate.getName(); if (parameterName == null) parameterName = "arg" + p; singleVar.setName(getAST().newSimpleName(getCompilationUnit().normalizeTermName(parameterName))); if (parameterDelegate instanceof QDataTerm) { QDataTerm<?> dataTerm = (QDataTerm<?>) parameterDelegate; // primitive if (dataTerm.isConstant()) singleVar.setType(getJavaPrimitive(dataTerm)); else { Type type = getJavaType(dataTerm); singleVar.setType(type); } } else if (parameterDelegate instanceof QDataSetTerm) { Type dataSet = getAST().newSimpleType(getAST().newSimpleName(QDataSet.class.getSimpleName())); ParameterizedType parType = getAST().newParameterizedType(dataSet); parType.typeArguments().add(getAST().newWildcardType()); singleVar.setType(parType); } methodDeclaration.parameters().add(singleVar); p++; } } Block block = getAST().newBlock(); methodDeclaration.setBody(block); // write java AST JDTStatementWriter statementWriter = getCompilationUnit().getContext().make(JDTStatementWriter.class); statementWriter.setAST(getAST()); statementWriter.getBlocks().push(block); if (prototype.getDelegate() != null) { ReturnStatement returnStatement = getAST().newReturnStatement(); // returnStatement.setExpression(getAST().newNullLiteral()); // block.statements().add(returnStatement); block.statements().add(getReturnStatement(returnStatement, prototype, methodDeclaration)); } statementWriter.getBlocks().pop(); }