List of usage examples for org.eclipse.jdt.core.dom IVariableBinding isEqualTo
public boolean isEqualTo(IBinding binding);
From source file:com.google.devtools.j2cpp.gen.CppStatementGenerator.java
License:Open Source License
private boolean hasNilCheckParent(Expression e, IVariableBinding sym) { ASTNode parent = e.getParent();//w w w . j a v a 2 s.c o m while (parent != null) { if (parent instanceof IfStatement) { Expression condition = ((IfStatement) parent).getExpression(); if (condition instanceof InfixExpression) { InfixExpression infix = (InfixExpression) condition; IBinding lhs = Types.getBinding(infix.getLeftOperand()); if (lhs != null && infix.getRightOperand() instanceof NullLiteral) { return sym.isEqualTo(lhs); } IBinding rhs = Types.getBinding(infix.getRightOperand()); if (rhs != null && infix.getLeftOperand() instanceof NullLiteral) { return sym.isEqualTo(rhs); } } } parent = parent.getParent(); if (parent instanceof MethodDeclaration) { break; } } return false; }
From source file:com.google.devtools.j2cpp.translate.AnonymousClassConverter.java
License:Open Source License
/** * Convert the anonymous class into an inner class. Fields are added for * final variables that are referenced, and a constructor is added. * * Note: endVisit is used for a depth-first traversal, to make it easier * to scan their containing nodes for references. *//* w ww . ja v a 2 s.co m*/ @Override @SuppressWarnings("unchecked") public void endVisit(AnonymousClassDeclaration node) { ASTNode parent = node.getParent(); ClassInstanceCreation newInvocation = null; EnumConstantDeclaration enumConstant = null; List<Expression> parentArguments; String newClassName; ITypeBinding innerType; boolean isStatic = staticParent(node); int modifiers = isStatic ? Modifier.STATIC : 0; if (parent instanceof ClassInstanceCreation) { newInvocation = (ClassInstanceCreation) parent; parentArguments = newInvocation.arguments(); innerType = Types.getTypeBinding(newInvocation); newClassName = innerType.getName(); innerType = RenamedTypeBinding.rename(newClassName, innerType.getDeclaringClass(), innerType, modifiers); } else if (parent instanceof EnumConstantDeclaration) { enumConstant = (EnumConstantDeclaration) parent; parentArguments = enumConstant.arguments(); innerType = Types.getTypeBinding(node); newClassName = Types.getTypeBinding(node).getName(); innerType = RenamedTypeBinding.rename(newClassName, innerType.getDeclaringClass(), innerType, modifiers); } else { throw new AssertionError("unknown anonymous class declaration parent: " + parent.getClass().getName()); } // Create a type declaration for this anonymous class. AST ast = node.getAST(); TypeDeclaration typeDecl = ast.newTypeDeclaration(); if (isStatic) { typeDecl.modifiers().add(ast.newModifier(ModifierKeyword.STATIC_KEYWORD)); } Types.addBinding(typeDecl, innerType); typeDecl.setName(ast.newSimpleName(newClassName)); Types.addBinding(typeDecl.getName(), innerType); typeDecl.setSourceRange(node.getStartPosition(), node.getLength()); Type superType = Types.makeType(Types.mapType(innerType.getSuperclass())); typeDecl.setSuperclassType(superType); for (ITypeBinding interfaceType : innerType.getInterfaces()) { typeDecl.superInterfaceTypes().add(Types.makeType(Types.mapType(interfaceType))); } for (Object bodyDecl : node.bodyDeclarations()) { BodyDeclaration decl = (BodyDeclaration) bodyDecl; typeDecl.bodyDeclarations().add(NodeCopier.copySubtree(ast, decl)); } typeDecl.accept(new InitializationNormalizer()); // Fix up references to external types, if necessary. Set<IVariableBinding> methodVars = getMethodVars(node); final List<ReferenceDescription> references = findReferences(node, methodVars); final List<Expression> invocationArgs = parentArguments; if (!references.isEmpty() || !invocationArgs.isEmpty()) { // is there anything to fix-up? List<IVariableBinding> innerVars = getInnerVars(references); if (!innerVars.isEmpty() || !invocationArgs.isEmpty()) { GeneratedMethodBinding defaultConstructor = addInnerVars(typeDecl, innerVars, references, parentArguments); Types.addBinding(parent, defaultConstructor); for (IVariableBinding var : innerVars) { if (!isConstant(var)) { parentArguments.add(makeFieldRef(var, ast)); } } assert defaultConstructor.getParameterTypes().length == parentArguments.size(); typeDecl.accept(new ASTVisitor() { @Override public void endVisit(SimpleName node) { IVariableBinding var = Types.getVariableBinding(node); if (var != null) { for (ReferenceDescription ref : references) { if (var.isEqualTo(ref.binding)) { if (ref.innerField != null) { setProperty(node, makeFieldRef(ref.innerField, node.getAST())); } else { // In-line constant. Object o = var.getConstantValue(); assert o != null; Expression literal = makeLiteral(o, var.getType().getQualifiedName(), node.getAST()); setProperty(node, literal); } return; } } } } }); } } // If invocation, replace anonymous class invocation with the new constructor. if (newInvocation != null) { newInvocation.setAnonymousClassDeclaration(null); newInvocation.setType(Types.makeType(innerType)); IMethodBinding oldBinding = Types.getMethodBinding(newInvocation); if (oldBinding == null) { oldBinding = newInvocation.resolveConstructorBinding(); } if (oldBinding != null) { GeneratedMethodBinding invocationBinding = new GeneratedMethodBinding(oldBinding); invocationBinding.setDeclaringClass(innerType); Types.addBinding(newInvocation, invocationBinding); } } else { enumConstant.setAnonymousClassDeclaration(null); } // Add type declaration to enclosing type. ITypeBinding outerType = innerType.getDeclaringClass(); if (outerType.isAnonymous()) { // Get outerType node. ASTNode n = parent.getParent(); while (!(n instanceof AnonymousClassDeclaration) && !(n instanceof TypeDeclaration)) { n = n.getParent(); } if (n instanceof AnonymousClassDeclaration) { AnonymousClassDeclaration outerDecl = (AnonymousClassDeclaration) n; outerDecl.bodyDeclarations().add(typeDecl); } } else { AbstractTypeDeclaration outerDecl = (AbstractTypeDeclaration) unit.findDeclaringNode(outerType); outerDecl.bodyDeclarations().add(typeDecl); } Symbols.scanAST(typeDecl); super.endVisit(node); }
From source file:de.ovgu.cide.export.physical.ahead.JakFeatureRefactorer.java
License:Open Source License
private void replaceParamterAccess(final MethodDeclaration method, final SingleVariableDeclaration parameter, FieldDeclaration parameterField) { final IVariableBinding oldParameterBinding = parameter.resolveBinding(); method.getBody().accept(new ASTVisitor() { public boolean visit(SimpleName node) { if (oldParameterBinding.isEqualTo(node.resolveBinding())) node.setIdentifier(getParameterFieldName(method, parameter)); return true; }/*from w w w.j a va 2s . c o m*/ }); }
From source file:org.eclipse.imp.analysis.reachingdefs.UseDefAnalyzer.java
License:Open Source License
private Set<ASTNode> findDefsForRef(ASTNode ref, IVariableBinding varBinding, IEstimateEnvironment<DefinitionSet> reachingDefs) { DefinitionSet defs = reachingDefs.getEstimate(fVariableFactory.createEntryLabel(ref)); Set<ASTNode> result = new HashSet<ASTNode>(); for (Iterator<DefinitionLiteral> iter = defs.iterator(); iter.hasNext();) { DefinitionLiteral def = (DefinitionLiteral) iter.next(); if (varBinding.isEqualTo(def.getVarBinding())) result.add(def.getLabel());/*from w w w . j a va2s . c o m*/ } return result; }