public static IMethod[] removeGeneratedMethods(IMethod[] methods) throws Exception { List<IMethod> result = new ArrayList<IMethod>(); for (IMethod m : methods) { if (m.getNameRange().getLength() > 0 && !m.getNameRange().equals(m.getSourceRange())) result.add(m); } return result.size() == methods.length ? methods : result.toArray(new IMethod[result.size()]); }
private int getMethodLineNumber(final IType type, IMethod method) throws JavaModelException { String source= type.getCompilationUnit().getSource(); String sourceUpToMethod= source.substring(0, method.getSourceRange().getOffset()); Pattern lineEnd= Pattern.compile("$", Pattern.MULTILINE | Pattern.DOTALL); return lineEnd.split(sourceUpToMethod).length; }
public StubTypeContext getStubTypeContext() { try { if (fContextCuStartEnd == null) fContextCuStartEnd= TypeContextChecker.createStubTypeContext(getCu(), fBaseCuRewrite.getRoot(), fMethod.getSourceRange().getOffset()); } catch (CoreException e) { //cannot do anything here throw new RuntimeException(e); } return fContextCuStartEnd; }
public StubTypeContext getStubTypeContext() { try { if (fContextCuStartEnd == null) fContextCuStartEnd= TypeContextChecker.createStubTypeContext(getCu(), fBaseCuRewrite.getRoot(), fMethod.getSourceRange().getOffset()); } catch (CoreException e) { //cannot do anything here throw new RuntimeException(e); } return fContextCuStartEnd; }
public StubTypeContext getStubTypeContext() { try { if (fContextCuStartEnd == null) fContextCuStartEnd= TypeContextChecker.createStubTypeContext(getCu(), fBaseCuRewrite.getRoot(), fMethod.getSourceRange().getOffset()); } catch (CoreException e) { //cannot do anything here throw new RuntimeException(e); } return fContextCuStartEnd; }
private IJavaElement findSibling(IMethod curr, IJavaElement[] members) throws JavaModelException { IJavaElement res= null; int methodStart= curr.getSourceRange().getOffset(); for (int i= members.length-1; i >= 0; i--) { IMember member= (IMember) members[i]; if (methodStart >= member.getSourceRange().getOffset()) { return res; } res= member; } return null; }
CalleeAnalyzerVisitor(IMethod method, CompilationUnit compilationUnit, IProgressMonitor progressMonitor) { fSearchResults = new CallSearchResultCollector(); this.fMethod = method; this.fCompilationUnit= compilationUnit; this.fProgressMonitor = progressMonitor; try { ISourceRange sourceRange = method.getSourceRange(); this.fMethodStartPosition = sourceRange.getOffset(); this.fMethodEndPosition = fMethodStartPosition + sourceRange.getLength(); } catch (JavaModelException jme) { JavaPlugin.log(jme); } }
private int compareInTheSameType(IMethodBinding firstMethodBinding, IMethodBinding secondMethodBinding) { try { IMethod firstMethod= (IMethod)firstMethodBinding.getJavaElement(); IMethod secondMethod= (IMethod)secondMethodBinding.getJavaElement(); if (firstMethod == null || secondMethod == null) { return 0; } ISourceRange firstSourceRange= firstMethod.getSourceRange(); ISourceRange secondSourceRange= secondMethod.getSourceRange(); if (!SourceRange.isAvailable(firstSourceRange) || !SourceRange.isAvailable(secondSourceRange)) { return firstMethod.getElementName().compareTo(secondMethod.getElementName()); } else { return firstSourceRange.getOffset() - secondSourceRange.getOffset(); } } catch (JavaModelException e) { return 0; } } }
private int compareInTheSameType(IMethodBinding firstMethodBinding, IMethodBinding secondMethodBinding) { try { IMethod firstMethod= (IMethod)firstMethodBinding.getJavaElement(); IMethod secondMethod= (IMethod)secondMethodBinding.getJavaElement(); if (firstMethod == null || secondMethod == null) { return 0; } ISourceRange firstSourceRange= firstMethod.getSourceRange(); ISourceRange secondSourceRange= secondMethod.getSourceRange(); if (!SourceRange.isAvailable(firstSourceRange) || !SourceRange.isAvailable(secondSourceRange)) { return firstMethod.getElementName().compareTo(secondMethod.getElementName()); } else { return firstSourceRange.getOffset() - secondSourceRange.getOffset(); } } catch (JavaModelException e) { return 0; } } }
private int compareInTheSameType(IMethodBinding firstMethodBinding, IMethodBinding secondMethodBinding) { try { IMethod firstMethod= (IMethod)firstMethodBinding.getJavaElement(); IMethod secondMethod= (IMethod)secondMethodBinding.getJavaElement(); if (firstMethod == null || secondMethod == null) { return 0; } ISourceRange firstSourceRange= firstMethod.getSourceRange(); ISourceRange secondSourceRange= secondMethod.getSourceRange(); if (!SourceRange.isAvailable(firstSourceRange) || !SourceRange.isAvailable(secondSourceRange)) { return firstMethod.getElementName().compareTo(secondMethod.getElementName()); } else { return firstSourceRange.getOffset() - secondSourceRange.getOffset(); } } catch (JavaModelException e) { return 0; } } }
ISourceRange range = method.getSourceRange(); if (range.getOffset() != -1 && range.getLength() != 0 ) { if (uniqueKey != null) {
ISourceRange range = method.getSourceRange(); if (range.getOffset() != -1 && range.getLength() != 0 ) { if (uniqueKey != null) {
IJavaElement element = delta.getElement(); if(element.getElementType() != IJavaElement.COMPILATION_UNIT) return; ICompilationUnit compilationUnit = (ICompilationUnit)element; try { IType type = compilationUnit.findPrimaryType(); IMethod[] methods = type.getMethods(); for(IMethod method : methods) { ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource(compilationUnit); parser.setSourceRange(method.getSourceRange().getOffset(), method.getSourceRange().getLength()); //parser.setKind(ASTParser.K_CLASS_BODY_DECLARATIONS); //parser.setSource(method.getSource().toCharArray()); //parser.setProject(method.getJavaProject()); parser.setResolveBindings(true); CompilationUnit cu = (CompilationUnit)parser.createAST(null); cu.accept(new ASTMethodVisitor()); // If the visitor visit the right VariableDeclarationFragment, // then the right IMethod is the current 'method' variable } } catch(JavaModelException e) { e.printStackTrace(); }
@SuppressWarnings("rawtypes") @Override public TestItem parseTestItem(IMethod method) throws JavaModelException { final TestItem item = super.parseTestItem(method); // Check if the method has annotated with @DisplayName final Optional<IAnnotation> annotation = TestFrameworkUtils.getAnnotation(method, DISPLAY_NAME_ANNOTATION_JUNIT5); if (annotation.isPresent()) { item.setDisplayName((String) annotation.get().getMemberValuePairs()[0].getValue()); } // Get the parameter type information final List<String> result = new LinkedList<>(); final CompilationUnit astRoot = CoreASTProvider.getInstance().getAST( method.getDeclaringType().getCompilationUnit(), CoreASTProvider.WAIT_YES, new NullProgressMonitor()); final ASTNode name = NodeFinder.perform(astRoot, method.getSourceRange()); if (name instanceof MethodDeclaration) { final List parameterList = ((MethodDeclaration) name).parameters(); for (final Object obj : parameterList) { if (obj instanceof SingleVariableDeclaration) { result.add(((SingleVariableDeclaration) obj).getType().resolveBinding().getQualifiedName()); } } } item.setParamTypes(result); return item; } }
parser.setIgnoreMethodBodies(true); CompilationUnit otherCU= (CompilationUnit) parser.createAST(null); ASTNode otherMethod= NodeFinder.perform(otherCU, iMethod.getSourceRange()); if (otherMethod instanceof MethodDeclaration) { MethodDeclaration otherMD= (MethodDeclaration) otherMethod;
private static void checkMethodInType(IType destinationType, RefactoringStatus result, IMethod method) throws JavaModelException { IMethod[] destinationTypeMethods= destinationType.getMethods(); IMethod found= findMethod(method, destinationTypeMethods); if (found != null){ RefactoringStatusContext context= JavaStatusContext.create(destinationType.getCompilationUnit(), found.getSourceRange()); String message= Messages.format(RefactoringCoreMessages.MemberCheckUtil_signature_exists, new String[]{method.getElementName(), JavaModelUtil.getFullyQualifiedName(destinationType)}); result.addError(message, context); } else { IMethod similar= Checks.findMethod(method, destinationType); if (similar != null){ String message= Messages.format(RefactoringCoreMessages.MemberCheckUtil_same_param_count, new String[]{method.getElementName(), JavaModelUtil.getFullyQualifiedName(destinationType)}); RefactoringStatusContext context= JavaStatusContext.create(destinationType.getCompilationUnit(), similar.getSourceRange()); result.addWarning(message, context); } } }
@Override public int compare(DelegateEntry firstEntry, DelegateEntry secondEntry) { IVariableBinding firstVariable= firstEntry.field; IVariableBinding secondVariable= secondEntry.field; if (firstVariable.equals(secondVariable)) { try { IMethod firstMethod= (IMethod)firstEntry.delegateMethod.getJavaElement(); IMethod secondMethod= (IMethod)secondEntry.delegateMethod.getJavaElement(); ISourceRange firstSourceRange= firstMethod.getSourceRange(); ISourceRange secondSourceRange= secondMethod.getSourceRange(); if (!SourceRange.isAvailable(firstSourceRange) || !SourceRange.isAvailable(secondSourceRange)) { return firstMethod.getElementName().compareTo(secondMethod.getElementName()); } else { return firstSourceRange.getOffset() - secondSourceRange.getOffset(); } } catch (JavaModelException e) { return 0; } } IField firstField= (IField)firstVariable.getJavaElement(); IField secondField= (IField)secondVariable.getJavaElement(); try { return firstField.getSourceRange().getOffset() - secondField.getSourceRange().getOffset(); } catch (JavaModelException e) { return 0; } } }
@Override public int compare(DelegateEntry firstEntry, DelegateEntry secondEntry) { IVariableBinding firstVariable= firstEntry.field; IVariableBinding secondVariable= secondEntry.field; if (firstVariable.equals(secondVariable)) { try { IMethod firstMethod= (IMethod)firstEntry.delegateMethod.getJavaElement(); IMethod secondMethod= (IMethod)secondEntry.delegateMethod.getJavaElement(); ISourceRange firstSourceRange= firstMethod.getSourceRange(); ISourceRange secondSourceRange= secondMethod.getSourceRange(); if (!SourceRange.isAvailable(firstSourceRange) || !SourceRange.isAvailable(secondSourceRange)) { return firstMethod.getElementName().compareTo(secondMethod.getElementName()); } else { return firstSourceRange.getOffset() - secondSourceRange.getOffset(); } } catch (JavaModelException e) { return 0; } } IField firstField= (IField)firstVariable.getJavaElement(); IField secondField= (IField)secondVariable.getJavaElement(); try { return firstField.getSourceRange().getOffset() - secondField.getSourceRange().getOffset(); } catch (JavaModelException e) { return 0; } } }
private static void checkMethodInType(IType destinationType, RefactoringStatus result, IMethod method) throws JavaModelException { IMethod[] destinationTypeMethods= destinationType.getMethods(); IMethod found= findMethod(method, destinationTypeMethods); if (found != null){ RefactoringStatusContext context= JavaStatusContext.create(destinationType.getCompilationUnit(), found.getSourceRange()); String message= Messages.format(RefactoringCoreMessages.MemberCheckUtil_signature_exists, new String[]{ BasicElementLabels.getJavaElementName(method.getElementName()), getQualifiedLabel(destinationType)}); result.addError(message, context); } else { IMethod similar= Checks.findMethod(method, destinationType); if (similar != null){ String message= Messages.format(RefactoringCoreMessages.MemberCheckUtil_same_param_count, new String[]{ BasicElementLabels.getJavaElementName(method.getElementName()), getQualifiedLabel(destinationType)}); RefactoringStatusContext context= JavaStatusContext.create(destinationType.getCompilationUnit(), similar.getSourceRange()); result.addWarning(message, context); } } }
private static void checkMethodInType(IType destinationType, RefactoringStatus result, IMethod method) throws JavaModelException { IMethod[] destinationTypeMethods= destinationType.getMethods(); IMethod found= findMethod(method, destinationTypeMethods); if (found != null){ RefactoringStatusContext context= JavaStatusContext.create(destinationType.getCompilationUnit(), found.getSourceRange()); String message= Messages.format(RefactoringCoreMessages.MemberCheckUtil_signature_exists, new String[]{ BasicElementLabels.getJavaElementName(method.getElementName()), getQualifiedLabel(destinationType)}); result.addError(message, context); } else { IMethod similar= Checks.findMethod(method, destinationType); if (similar != null){ String message= Messages.format(RefactoringCoreMessages.MemberCheckUtil_same_param_count, new String[]{ BasicElementLabels.getJavaElementName(method.getElementName()), getQualifiedLabel(destinationType)}); RefactoringStatusContext context= JavaStatusContext.create(destinationType.getCompilationUnit(), similar.getSourceRange()); result.addWarning(message, context); } } }