|
| 1 | +/******************************************************************************* |
| 2 | + * Java2Script Pacemaker (http://j2s.sourceforge.net) |
| 3 | + * |
| 4 | + * Copyright (c) 2006 ognize.com and others. |
| 5 | + * All rights reserved. This program and the accompanying materials |
| 6 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 7 | + * which accompanies this distribution, and is available at |
| 8 | + * http://www.eclipse.org/legal/epl-v10.html |
| 9 | + * |
| 10 | + * Contributors: |
| 11 | + * ognize.com - initial API and implementation |
| 12 | + *******************************************************************************/ |
| 13 | + |
| 14 | +package net.sf.j2s.core.astvisitors; |
| 15 | + |
| 16 | +import java.util.List; |
| 17 | +import org.eclipse.jdt.core.dom.Block; |
| 18 | +import org.eclipse.jdt.core.dom.Expression; |
| 19 | +import org.eclipse.jdt.core.dom.ExpressionStatement; |
| 20 | +import org.eclipse.jdt.core.dom.IMethodBinding; |
| 21 | +import org.eclipse.jdt.core.dom.ITypeBinding; |
| 22 | +import org.eclipse.jdt.core.dom.IfStatement; |
| 23 | +import org.eclipse.jdt.core.dom.MethodDeclaration; |
| 24 | +import org.eclipse.jdt.core.dom.MethodInvocation; |
| 25 | +import org.eclipse.jdt.core.dom.Statement; |
| 26 | + |
| 27 | +/** |
| 28 | + * @author josson smith |
| 29 | + * |
| 30 | + * 2006-8-5 |
| 31 | + */ |
| 32 | +public class SWTDependencyASTVisitor extends DependencyASTVisitor { |
| 33 | + |
| 34 | + protected String[] getFilterMethods() { |
| 35 | + return new String[] { |
| 36 | + "org.eclipse.swt.widgets.Widget", "checkSubclass", |
| 37 | + "org.eclipse.swt.widgets.Dialog", "checkSubclass", |
| 38 | + "org.eclipse.swt.widgets.Widget", "checkWidget", |
| 39 | + "org.eclipse.swt.widgets.Display", "checkDevice", |
| 40 | + "org.eclipse.swt.graphics.Device", "checkDevice" |
| 41 | + }; |
| 42 | + } |
| 43 | + |
| 44 | + /* (non-Javadoc) |
| 45 | + * @see net.sf.j2s.core.astvisitors.ASTScriptVisitor#endVisit(org.eclipse.jdt.core.dom.MethodDeclaration) |
| 46 | + */ |
| 47 | + public void endVisit(MethodDeclaration node) { |
| 48 | + IMethodBinding methodBinding = node.resolveBinding(); |
| 49 | + String[] filterMethods = getFilterMethods(); |
| 50 | + for (int i = 0; i < filterMethods.length; i += 2) { |
| 51 | + if (isMethodInvoking(methodBinding, filterMethods[i], filterMethods[i + 1])) { |
| 52 | + return ; |
| 53 | + } |
| 54 | + } |
| 55 | + super.endVisit(node); |
| 56 | + } |
| 57 | + |
| 58 | + private boolean isMethodInvoking(IMethodBinding methodBinding, String className, String methodName) { |
| 59 | + if (methodName.equals(methodBinding.getName())) { |
| 60 | + IMethodBinding findMethodInHierarchy = Bindings.findMethodInHierarchy(methodBinding.getDeclaringClass(), methodName, null); |
| 61 | + IMethodBinding last = findMethodInHierarchy; |
| 62 | + int count = 0; |
| 63 | + while (findMethodInHierarchy != null && (count++) < 10) { |
| 64 | + last = findMethodInHierarchy; |
| 65 | + ITypeBinding superclass = last.getDeclaringClass().getSuperclass(); |
| 66 | + if (superclass == null) { |
| 67 | + break; |
| 68 | + } |
| 69 | + findMethodInHierarchy = |
| 70 | + Bindings.findMethodInHierarchy(superclass, methodName, null); |
| 71 | + } |
| 72 | + if (last == null) { |
| 73 | + last = methodBinding; |
| 74 | + } |
| 75 | + if (className.equals(last.getDeclaringClass().getQualifiedName())) { |
| 76 | + return true; |
| 77 | + } |
| 78 | + } |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + private boolean isMethodInvoking(Expression exp, String className, String methodName) { |
| 83 | + if (exp instanceof MethodInvocation) { |
| 84 | + MethodInvocation method = (MethodInvocation) exp; |
| 85 | + IMethodBinding methodBinding = method.resolveMethodBinding(); |
| 86 | + if (isMethodInvoking(methodBinding, className, methodName)) { |
| 87 | + return true; |
| 88 | + } |
| 89 | + /* |
| 90 | + IMethodBinding methodBinding = method.resolveMethodBinding(); |
| 91 | + if (methodName.equals(methodBinding.getName()) &&className.equals( |
| 92 | + methodBinding.getDeclaringClass().getQualifiedName())) { |
| 93 | + return true; |
| 94 | + } |
| 95 | + */ |
| 96 | + } |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + public boolean visit(IfStatement node) { |
| 101 | + if (node.getElseStatement() == null) { |
| 102 | + Statement thenStatement = node.getThenStatement(); |
| 103 | + if (thenStatement instanceof Block) { |
| 104 | + Block block = (Block) thenStatement; |
| 105 | + List statements = block.statements(); |
| 106 | + if (statements.size() == 1) { |
| 107 | + thenStatement = (Statement) statements.get(0); |
| 108 | + } |
| 109 | + } |
| 110 | + if (thenStatement instanceof ExpressionStatement) { |
| 111 | + ExpressionStatement expStmt = (ExpressionStatement) thenStatement; |
| 112 | + Expression exp = expStmt.getExpression(); |
| 113 | + if (isMethodInvoking(exp, "org.eclipse.swt.widgets.Widget", "error")) { |
| 114 | + return false; |
| 115 | + } |
| 116 | + if (isMethodInvoking(exp, "org.eclipse.swt.SWT", "error")) { |
| 117 | + return false; |
| 118 | + } |
| 119 | + if (isMethodInvoking(exp, "org.eclipse.swt.widgets.Display", "error")) { |
| 120 | + return false; |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + return super.visit(node); |
| 125 | + } |
| 126 | +} |
0 commit comments