List of usage examples for org.eclipse.jdt.core.dom CompilationUnit findDeclaringNode
public ASTNode findDeclaringNode(String key)
From source file:cn.ieclipse.adt.ext.jdt.SourceGenerator.java
License:Apache License
public static void applyChange(ICompilationUnit cu, CompilationUnit unit) { try {// ww w . j a v a 2s. co m ASTRewrite rewrite = ASTRewrite.create(unit.getAST()); ImportRewrite importRewrite = StubUtility.createImportRewrite(unit, false); ASTNode node = unit.findDeclaringNode(cu.getTypes()[0].getKey()); AbstractTypeDeclaration type = ((AbstractTypeDeclaration) node); ListRewrite listRewrite = rewrite.getListRewrite(node, type.getBodyDeclarationsProperty()); MultiTextEdit edit = new MultiTextEdit(); TextEdit sub = importRewrite.rewriteImports(null); edit.addChild(sub); // System.out.println(unit); org.eclipse.jface.text.Document doc = new org.eclipse.jface.text.Document(cu.getSource()); TextEdit te = rewrite.rewriteAST(doc, cu.getJavaProject().getOptions(true)); te.apply(doc); IBuffer buffer = cu.getBuffer(); buffer.setContents(doc.get()); buffer.save(null, true); // System.out.println(buffer.getContents()); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.google.gdt.eclipse.core.JavaASTUtils.java
License:Open Source License
public static MethodDeclaration findMethodDeclaration(CompilationUnit astRoot, String methodBindingKey) { ASTNode bindingDecl = astRoot.findDeclaringNode(methodBindingKey); if (bindingDecl == null || bindingDecl.getNodeType() != ASTNode.METHOD_DECLARATION) { return null; }/*from w w w . j a va 2 s .c o m*/ return (MethodDeclaration) bindingDecl; }
From source file:com.google.gwt.eclipse.core.refactoring.regionupdater.StableNodeBasedRegionUpdater.java
License:Open Source License
protected ASTNode findEquivalentStableNode(CompilationUnit newCu) throws RefactoringException { if (isCompilationUnitTheStableNode) { // The compilation unit is the stable node return newCu; } else {/*from w ww. j ava2 s . co m*/ // Find the equivalent stable node using its binding key in this AST // compilation unit ASTNode equivalentStableNode = newCu.findDeclaringNode(getUpdatedStableBindingKey()); if (equivalentStableNode == null) { throw new RefactoringException( "Could not find the equivalent stable node in the post-refactoring AST."); } return equivalentStableNode; } }
From source file:com.halware.nakedide.eclipse.ext.annot.utils.AstUtils.java
License:Open Source License
/** * Finds the {@link MethodDeclaration} within the supplied {@link CompilationUnit}, * using a method declaration key as the search. * /* w w w. j ava 2 s.co m*/ * @param compilationUnit * @param searchMethodDeclarationKey * @return */ public static <T extends ASTNode> T findDeclaration(CompilationUnit compilationUnit, String searchDeclarationKey) { if (compilationUnit == null || searchDeclarationKey == null) { return null; } ASTNode astNode = compilationUnit.findDeclaringNode(searchDeclarationKey); if (astNode == null) { return null; } if (!(astNode instanceof BodyDeclaration) && !(astNode instanceof SingleVariableDeclaration)) { return null; } return Generics.asT(astNode); }
From source file:com.intel.ide.eclipse.mpt.ast.UnresolvedElementsSubProcessor.java
License:Open Source License
private static void addNewFieldProposals(ICompilationUnit cu, CompilationUnit astRoot, ITypeBinding binding, ITypeBinding declaringTypeBinding, SimpleName simpleName, boolean isWriteAccess, Map<String, Map> proposals) throws JavaModelException { // new variables ICompilationUnit targetCU;//from ww w .ja v a 2s .c o m ITypeBinding senderDeclBinding; if (binding != null) { senderDeclBinding = binding.getTypeDeclaration(); targetCU = ASTResolving.findCompilationUnitForBinding(cu, astRoot, senderDeclBinding); } else { // binding is null for accesses without qualifier senderDeclBinding = declaringTypeBinding; targetCU = cu; } if (!senderDeclBinding.isFromSource() || targetCU == null) { return; } boolean mustBeConst = ASTResolving.isInsideModifiers(simpleName); addNewFieldForType(targetCU, binding, senderDeclBinding, simpleName, isWriteAccess, mustBeConst, proposals); if (binding == null && senderDeclBinding.isNested()) { ASTNode anonymDecl = astRoot.findDeclaringNode(senderDeclBinding); if (anonymDecl != null) { ITypeBinding bind = Bindings.getBindingOfParentType(anonymDecl.getParent()); if (!bind.isAnonymous()) { addNewFieldForType(targetCU, bind, bind, simpleName, isWriteAccess, mustBeConst, proposals); } } } }
From source file:com.intel.ide.eclipse.mpt.ast.UnresolvedElementsSubProcessor.java
License:Open Source License
private static void addNewMethodProposals(ICompilationUnit cu, CompilationUnit astRoot, Expression sender, List<Expression> arguments, boolean isSuperInvocation, ASTNode invocationNode, String methodName, Map<String, Map> missingMethodsMap) throws JavaModelException { ITypeBinding nodeParentType = Bindings.getBindingOfParentType(invocationNode); ITypeBinding binding = null;//w w w .j av a 2 s . c o m if (sender != null) { binding = sender.resolveTypeBinding(); } else { binding = nodeParentType; if (isSuperInvocation && binding != null) { binding = binding.getSuperclass(); } } if (binding != null && binding.isFromSource()) { ITypeBinding senderDeclBinding = binding.getTypeDeclaration(); ICompilationUnit targetCU = ASTResolving.findCompilationUnitForBinding(cu, astRoot, senderDeclBinding); if (targetCU != null) { String fqName = targetCU.findPrimaryType().getFullyQualifiedName(); Map<String, LinkedCorrectionProposal> proposalsMap = missingMethodsMap.get(fqName); if (proposalsMap == null) { MptPluginConsole.general(MptConstants.PARTIAL_CONVERSION_TAG, "Skip adding method for " + fqName); return; } String label; Image image = null; ITypeBinding[] parameterTypes = getParameterTypes(arguments); if (!checkParameterTypes(parameterTypes, missingMethodsMap)) return; if (parameterTypes != null) { String sig = ASTResolving.getMethodSignature(methodName, parameterTypes, false); if (ASTResolving.isUseableTypeInContext(parameterTypes, senderDeclBinding, false)) { if (nodeParentType == senderDeclBinding) { label = Messages.format( CorrectionMessages.UnresolvedElementsSubProcessor_createmethod_description, sig); // image= JavaPluginImages.get(JavaPluginImages.IMG_MISC_PRIVATE); } else { label = Messages.format( CorrectionMessages.UnresolvedElementsSubProcessor_createmethod_other_description, new Object[] { sig, BasicElementLabels.getJavaElementName(senderDeclBinding.getName()) }); // image= JavaPluginImages.get(JavaPluginImages.IMG_MISC_PUBLIC); } proposalsMap.put(sig, new NewMethodCorrectionProposal(label, targetCU, invocationNode, arguments, senderDeclBinding, IProposalRelevance.CREATE_METHOD, image)); } if (senderDeclBinding.isNested() && cu.equals(targetCU) && sender == null && Bindings .findMethodInHierarchy(senderDeclBinding, methodName, (ITypeBinding[]) null) == null) { // no covering method ASTNode anonymDecl = astRoot.findDeclaringNode(senderDeclBinding); if (anonymDecl != null) { senderDeclBinding = Bindings.getBindingOfParentType(anonymDecl.getParent()); if (!senderDeclBinding.isAnonymous() && ASTResolving .isUseableTypeInContext(parameterTypes, senderDeclBinding, false)) { String[] args = new String[] { sig, ASTResolving.getTypeSignature(senderDeclBinding) }; label = Messages.format( CorrectionMessages.UnresolvedElementsSubProcessor_createmethod_other_description, args); // image= JavaPluginImages.get(JavaPluginImages.IMG_MISC_PROTECTED); proposalsMap.put(sig, new NewMethodCorrectionProposal(label, targetCU, invocationNode, arguments, senderDeclBinding, IProposalRelevance.CREATE_METHOD, image)); } } } } } } }
From source file:edu.illinois.jflow.core.transformations.code.ExtractClosureAnalyzer.java
License:Open Source License
private ITypeBinding[] computeTypeVariables(ITypeBinding[] bindings) { Selection selection = getSelection(); Set<ITypeBinding> result = new HashSet<ITypeBinding>(); // first remove all type variables that come from outside of the method // or are covered by the selection CompilationUnit compilationUnit = (CompilationUnit) fEnclosingBodyDeclaration.getRoot(); for (int i = 0; i < bindings.length; i++) { ASTNode decl = compilationUnit.findDeclaringNode(bindings[i]); if (decl == null || (!selection.covers(decl) && decl.getParent() instanceof MethodDeclaration)) result.add(bindings[i]);//from ww w .j ava 2 s. c o m } // all all type variables which are needed since a local variable uses it for (int i = 0; i < fArguments.length; i++) { IVariableBinding arg = fArguments[i]; ITypeBinding type = arg.getType(); if (type != null && type.isTypeVariable()) { ASTNode decl = compilationUnit.findDeclaringNode(type); if (decl == null || (!selection.covers(decl) && decl.getParent() instanceof MethodDeclaration)) result.add(type); } } return result.toArray(new ITypeBinding[result.size()]); }
From source file:nz.ac.massey.cs.care.refactoring.executers.IntroduceFactoryRefactoring.java
License:Open Source License
/** * @return an array containing the argument names for the constructor * identified by <code>fCtorBinding</code>, if available, or default * names if unavailable (e.g. if the constructor resides in a binary unit). *//*from ww w .j a v a 2s .co m*/ private String[] findCtorArgNames() { int numArgs = fCtorBinding.getParameterTypes().length; String[] names = new String[numArgs]; CompilationUnit ctorUnit = (CompilationUnit) ASTNodes.getParent(fCtorOwningClass, CompilationUnit.class); MethodDeclaration ctorDecl = (MethodDeclaration) ctorUnit.findDeclaringNode(fCtorBinding.getKey()); if (ctorDecl != null) { List<SingleVariableDeclaration> formalArgs = ctorDecl.parameters(); int i = 0; for (Iterator<SingleVariableDeclaration> iter = formalArgs.iterator(); iter.hasNext(); i++) { SingleVariableDeclaration svd = iter.next(); names[i] = svd.getName().getIdentifier(); } return names; } // Have no way of getting the formal argument names; just fake it. for (int i = 0; i < numArgs; i++) names[i] = "arg" + (i + 1); //$NON-NLS-1$ return names; }
From source file:nz.ac.massey.cs.care.refactoring.executers.IntroduceFactoryRefactoring.java
License:Open Source License
/** * Creates and adds the necessary change to make the constructor method protected. * @param unitAST// w w w .ja v a2 s . c om * @param unitRewriter * @param declGD * @return false iff the constructor didn't exist (i.e. was implicit) */ private boolean protectConstructor(CompilationUnit unitAST, ASTRewrite unitRewriter, TextEditGroup declGD) { MethodDeclaration constructor = (MethodDeclaration) unitAST.findDeclaringNode(fCtorBinding.getKey()); // No need to rewrite the modifiers if the visibility is what we already want it to be. if (constructor == null || (JdtFlags.getVisibilityCode(constructor)) == fConstructorVisibility) return false; ModifierRewrite.create(unitRewriter, constructor).setVisibility(fConstructorVisibility, declGD); return true; }
From source file:nz.ac.massey.cs.care.refactoring.executers.IntroduceFactoryRefactoring.java
License:Open Source License
/** * Perform the AST rewriting necessary on the given <code>CompilationUnit</code> * to create the factory method. The method will reside on the type identified by * <code>fFactoryOwningClass</code>. * @param unitRewriter// ww w.j ava2s . c o m * @param unit * @param gd the <code>GroupDescription</code> to associate with the changes made */ private void createFactoryChange(ASTRewrite unitRewriter, CompilationUnit unit, TextEditGroup gd) { // ================================================================================ // First add the factory itself (method, class, and interface as needed/directed by user) AST ast = unit.getAST(); fFactoryMethod = createFactoryMethod(ast, fCtorBinding, unitRewriter); AbstractTypeDeclaration factoryOwner = (AbstractTypeDeclaration) unit .findDeclaringNode(fFactoryOwningClass.resolveBinding().getKey()); fImportRewriter.addImport(fCtorOwningClass.resolveBinding()); int idx = ASTNodes.getInsertionIndex(fFactoryMethod, factoryOwner.bodyDeclarations()); if (idx < 0) idx = 0; // Guard against bug in getInsertionIndex() unitRewriter.getListRewrite(factoryOwner, factoryOwner.getBodyDeclarationsProperty()) .insertAt(fFactoryMethod, idx, gd); }