public IResource getLaunchableResource(IEditorPart editorPart) { ITypeRoot element = JavaUI.getEditorInputTypeRoot(editorPart.getEditorInput()); if (element != null) { try { return element.getCorrespondingResource(); } catch (JavaModelException e) { } } return null; }
public ColoredSourceFile getSourceFile() { ITypeRoot java = getInputJavaElement(); assert java != null; assert java.getResource() instanceof IFile; IFile file = (IFile) java.getResource(); if (file != null) { IFeatureModel featureModel; try { featureModel = FeatureModelManager.getInstance().getFeatureModel(file.getProject()); } catch (FeatureModelNotFoundException e) { e.printStackTrace(); assert false : e; return null; } return ColoredSourceFile.getColoredSourceFile(file, featureModel); } return null; }
public IJavaElement searchInClass(Declaration ceylonDeclaration) { try { this.ceylonDeclaration = ceylonDeclaration; return visit(typeRoot.getPrimaryElement()); } catch (Exception e) { e.printStackTrace(); } return null; }
private IJavaElement declarationMatched(IJavaElement javaElement, IBindingProvider mirror) { if (mirror != null) { parser.setProject(typeRoot.getJavaProject()); IBinding[] bindings = parser.createBindings(new IJavaElement[] {javaElement}, null); if (bindings.length > 0 && bindings[0] != null) { if (mirror instanceof JDTMethod && bindings[0] instanceof ITypeBinding) { // Case of a constructor : let's go to the constructor and not to the type. ITypeBinding typeBinding = (ITypeBinding) bindings[0]; for (IMethodBinding methodBinding : typeBinding.getDeclaredMethods()) { // if (methodBinding.isConstructor()) { if (CharOperation.equals( methodBinding.getKey().toCharArray(), mirror.getBindingKey())) { return methodBinding.getJavaElement(); } // } } } if (CharOperation.equals(bindings[0].getKey().toCharArray(), mirror.getBindingKey())) { return javaElement; } } } return null; }
private void insertAllMissingMethodTags(ASTRewrite rewriter, MethodDeclaration methodDecl) { AST ast = methodDecl.getAST(); Javadoc javadoc = methodDecl.getJavadoc(); ListRewrite tagsRewriter = rewriter.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY); List<TypeParameter> typeParams = methodDecl.typeParameters(); ASTNode root = methodDecl.getRoot(); if (root instanceof CompilationUnit) { ITypeRoot typeRoot = ((CompilationUnit) root).getTypeRoot(); if (typeRoot != null && !StubUtility.shouldGenerateMethodTypeParameterTags(typeRoot.getJavaProject())) typeParams = Collections.emptyList(); } List<String> typeParamNames = new ArrayList<>(); for (int i = typeParams.size() - 1; i >= 0; i--) { TypeParameter decl = typeParams.get(i); String name = '<' + decl.getName().getIdentifier() + '>'; if (findTag(javadoc, TagElement.TAG_PARAM, name) == null) { TagElement newTag = ast.newTagElement(); newTag.setTagName(TagElement.TAG_PARAM); TextElement text = ast.newTextElement(); text.setText(name); newTag.fragments().add(text); insertTabStop(rewriter, newTag.fragments(), "typeParam" + i); // $NON-NLS-1$ insertTag(tagsRewriter, newTag, getPreviousTypeParamNames(typeParams, decl)); } typeParamNames.add(name); } List<SingleVariableDeclaration> params = methodDecl.parameters(); for (int i = params.size() - 1; i >= 0; i--) { SingleVariableDeclaration decl = params.get(i); String name = decl.getName().getIdentifier(); if (findTag(javadoc, TagElement.TAG_PARAM, name) == null) { TagElement newTag = ast.newTagElement(); newTag.setTagName(TagElement.TAG_PARAM); newTag.fragments().add(ast.newSimpleName(name)); insertTabStop(rewriter, newTag.fragments(), "methParam" + i); // $NON-NLS-1$ Set<String> sameKindLeadingNames = getPreviousParamNames(params, decl); sameKindLeadingNames.addAll(typeParamNames); insertTag(tagsRewriter, newTag, sameKindLeadingNames); } } if (!methodDecl.isConstructor()) { Type type = methodDecl.getReturnType2(); if (!type.isPrimitiveType() || (((PrimitiveType) type).getPrimitiveTypeCode() != PrimitiveType.VOID)) { if (findTag(javadoc, TagElement.TAG_RETURN, null) == null) { TagElement newTag = ast.newTagElement(); newTag.setTagName(TagElement.TAG_RETURN); insertTabStop(rewriter, newTag.fragments(), "return"); // $NON-NLS-1$ insertTag(tagsRewriter, newTag, null); } } } List<Type> thrownExceptions = methodDecl.thrownExceptionTypes(); for (int i = thrownExceptions.size() - 1; i >= 0; i--) { Type exception = thrownExceptions.get(i); ITypeBinding binding = exception.resolveBinding(); if (binding != null) { String name = binding.getName(); if (findThrowsTag(javadoc, name) == null) { TagElement newTag = ast.newTagElement(); newTag.setTagName(TagElement.TAG_THROWS); TextElement excNode = ast.newTextElement(); excNode.setText(ASTNodes.getQualifiedTypeName(exception)); newTag.fragments().add(excNode); insertTabStop(rewriter, newTag.fragments(), "exception" + i); // $NON-NLS-1$ insertTag(tagsRewriter, newTag, getPreviousExceptionNames(thrownExceptions, exception)); } } } }
private ClipboardData getClipboardData(ITypeRoot inputElement, int offset, int length) { CompilationUnit astRoot = SharedASTProvider.getAST(inputElement, SharedASTProvider.WAIT_ACTIVE_ONLY, null); if (astRoot == null) { return null; } // do process import if selection spans over import declaration or package List<ImportDeclaration> list = astRoot.imports(); if (!list.isEmpty()) { if (offset < ((ASTNode) list.get(list.size() - 1)).getStartPosition()) { return null; } } else if (astRoot.getPackage() != null) { if (offset < ((ASTNode) astRoot.getPackage()).getStartPosition()) { return null; } } ArrayList<SimpleName> typeImportsRefs = new ArrayList<SimpleName>(); ArrayList<SimpleName> staticImportsRefs = new ArrayList<SimpleName>(); ImportReferencesCollector.collect( astRoot, inputElement.getJavaProject(), new Region(offset, length), typeImportsRefs, staticImportsRefs); if (typeImportsRefs.isEmpty() && staticImportsRefs.isEmpty()) { return null; } HashSet<String> namesToImport = new HashSet<String>(typeImportsRefs.size()); for (int i = 0; i < typeImportsRefs.size(); i++) { Name curr = typeImportsRefs.get(i); IBinding binding = curr.resolveBinding(); if (binding != null && binding.getKind() == IBinding.TYPE) { ITypeBinding typeBinding = (ITypeBinding) binding; if (typeBinding.isArray()) { typeBinding = typeBinding.getElementType(); } if (typeBinding.isTypeVariable() || typeBinding.isCapture() || typeBinding.isWildcardType()) { // can be removed when bug 98473 is fixed continue; } if (typeBinding.isMember() || typeBinding.isTopLevel()) { String name = Bindings.getRawQualifiedName(typeBinding); if (name.length() > 0) { namesToImport.add(name); } } } } HashSet<String> staticsToImport = new HashSet<String>(staticImportsRefs.size()); for (int i = 0; i < staticImportsRefs.size(); i++) { Name curr = staticImportsRefs.get(i); IBinding binding = curr.resolveBinding(); if (binding != null) { StringBuffer buf = new StringBuffer(Bindings.getImportName(binding)); if (binding.getKind() == IBinding.METHOD) { buf.append("()"); // $NON-NLS-1$ } staticsToImport.add(buf.toString()); } } if (namesToImport.isEmpty() && staticsToImport.isEmpty()) { return null; } String[] typeImports = namesToImport.toArray(new String[namesToImport.size()]); String[] staticImports = staticsToImport.toArray(new String[staticsToImport.size()]); return new ClipboardData(inputElement, typeImports, staticImports); }
@Override public void beforeAllMethods(ITypeRoot root, CompilationUnit cUnit) { JMLElement rootXML = null; try { rootXML = XMLFromSource.createXML(cUnit, root.getSource(), true, true); } catch (JavaModelException e) { e.printStackTrace(); } // for use of NLP XMLOutputter output = new XMLOutputter(rootXML); // could make this pretty later - at the moment building XML just to parse it... try { InputStream istream = new ByteArrayInputStream(output.getString().getBytes()); Document jdoc = new SAXBuilder().build(istream); istream.close(); NLPResult result = new NLPResult(); for (Element classElement : jdoc.getRootElement().getChildren()) { for (Element methodElement : classElement.getChildren()) { // get the start and end lines of this method Integer startLine = new Integer(methodElement.getAttributeValue("line")); Integer endLine = new Integer(methodElement.getAttributeValue("endLine")); // build set of variable names in scope Set<String> variableNames = new HashSet<String>(); Element paramsElement = methodElement.getChild("params"); if (paramsElement != null) for (Element paramElement : paramsElement.getChildren()) variableNames.add(paramElement.getAttributeValue("name")); for (Element declarationElement : methodElement.getChildren("declaration")) variableNames.add(declarationElement.getAttributeValue("name")); Machine intRangeMachine = new IntRangeMachine(variableNames); // analyse each of the comments for (Element commentElement : methodElement.getChildren("comment")) { Map<String, String> frame = intRangeMachine.recognise(commentElement.getTextNormalize()); if (frame != null) { String op = frame.get("op"); String a1 = frame.containsKey("a1") ? frame.get("a1") : "0"; PositiveNegativeLattice expected = null; if (op.equals("eq") && a1.equals("0")) { expected = PositiveNegativeLattice.ZERO; } else if ((op.equals("gt") && a1.equals("0")) || (op.equals("ge") && a1.equals("1"))) { expected = PositiveNegativeLattice.POS; } else if ((op.equals("lt") && a1.equals("0")) || (op.equals("le") && a1.equals("1"))) { expected = PositiveNegativeLattice.NEG; } if (expected != null) { int commentLineNo = -1; int lineFrom = new Integer(methodElement.getAttributeValue("line")); int lineTo = new Integer(methodElement.getAttributeValue("endLine")); String varName = frame.get("a0"); String methodName = methodElement.getAttributeValue("name"); result.addComment( AnalysisType.SRA, new RangeAnalysisComment( commentLineNo, lineFrom, lineTo, expected, varName, methodName)); } } } } } for (AnalysisType c : result.getTypes()) { try { c.classFile .getConstructor(CommentCollection.class) .newInstance(result.getAnnotations(c)) .runAnalysis(getReporter(), getInput(), root, cUnit); } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) { e.printStackTrace(); } } } catch (JDOMException | IOException e) { e.printStackTrace(); } }
public Change createChange(IProgressMonitor pm) throws CoreException { if (fDeleteSource && fCurrentMode == Mode.INLINE_ALL) { TextChange change = fChangeManager.get((ICompilationUnit) fSourceProvider.getTypeRoot()); TextEdit delete = fSourceProvider.getDeleteEdit(); TextEditGroup description = new TextEditGroup( RefactoringCoreMessages.InlineMethodRefactoring_edit_delete, new TextEdit[] {delete}); TextEdit root = change.getEdit(); if (root != null) { // TODO instead of finding the right insert position the call inliner should // reuse the AST & rewriter of the source provide and we should rewrite the // whole AST at the end. However, since recursive calls aren't allowed there // shouldn't be a text edit overlap. // root.addChild(delete); TextChangeCompatibility.insert(root, delete); } else { change.setEdit(delete); } change.addTextEditGroup(description); } final Map arguments = new HashMap(); String project = null; IJavaProject javaProject = fInitialTypeRoot.getJavaProject(); if (javaProject != null) project = javaProject.getElementName(); int flags = RefactoringDescriptor.STRUCTURAL_CHANGE | JavaRefactoringDescriptor.JAR_REFACTORING | JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT; final IMethodBinding binding = fSourceProvider.getDeclaration().resolveBinding(); final ITypeBinding declaring = binding.getDeclaringClass(); if (!Modifier.isPrivate(binding.getModifiers())) flags |= RefactoringDescriptor.MULTI_CHANGE; final String description = Messages.format( RefactoringCoreMessages.InlineMethodRefactoring_descriptor_description_short, BasicElementLabels.getJavaElementName(binding.getName())); final String header = Messages.format( RefactoringCoreMessages.InlineMethodRefactoring_descriptor_description, new String[] { BindingLabelProvider.getBindingLabel(binding, JavaElementLabels.ALL_FULLY_QUALIFIED), BindingLabelProvider.getBindingLabel(declaring, JavaElementLabels.ALL_FULLY_QUALIFIED) }); final JDTRefactoringDescriptorComment comment = new JDTRefactoringDescriptorComment(project, this, header); comment.addSetting( Messages.format( RefactoringCoreMessages.InlineMethodRefactoring_original_pattern, BindingLabelProvider.getBindingLabel(binding, JavaElementLabels.ALL_FULLY_QUALIFIED))); if (fDeleteSource) comment.addSetting(RefactoringCoreMessages.InlineMethodRefactoring_remove_method); if (fCurrentMode == Mode.INLINE_ALL) comment.addSetting(RefactoringCoreMessages.InlineMethodRefactoring_replace_references); final InlineMethodDescriptor descriptor = RefactoringSignatureDescriptorFactory.createInlineMethodDescriptor( project, description, comment.asString(), arguments, flags); arguments.put( JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT, JavaRefactoringDescriptorUtil.elementToHandle(project, fInitialTypeRoot)); arguments.put( JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION, new Integer(fSelectionStart).toString() + " " + new Integer(fSelectionLength).toString()); // $NON-NLS-1$ arguments.put(ATTRIBUTE_DELETE, Boolean.valueOf(fDeleteSource).toString()); arguments.put(ATTRIBUTE_MODE, new Integer(fCurrentMode == Mode.INLINE_ALL ? 1 : 0).toString()); return new DynamicValidationRefactoringChange( descriptor, RefactoringCoreMessages.InlineMethodRefactoring_edit_inlineCall, fChangeManager.getAllChanges()); }