org.eclipse.jdt.core.dom.BodyDeclaration Java Examples

The following examples show how to use org.eclipse.jdt.core.dom.BodyDeclaration. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Utils.java    From txtUML with Eclipse Public License 1.0 6 votes vote down vote up
public static void checkModifiers(ProblemCollector collector, BodyDeclaration elem,
		Predicate<Modifier> allowSpecific) {
	for (Object obj : elem.modifiers()) {
		if (!(obj instanceof Modifier)) {
			continue;
		}
		Modifier modifier = (Modifier) obj;
		boolean valid;
		if (allowSpecific.test(modifier)) {
			valid = true;
		} else {
			valid = modifier.isPrivate() || modifier.isPublic() || modifier.isProtected() || modifier.isFinal();
		}
		if (!valid) {
			collector.report(INVALID_MODIFIER.create(collector.getSourceInfo(), modifier));
		}
	}
}
 
Example #2
Source File: ExtractMethodRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private void initializeDestinations() {
	List<ASTNode> result= new ArrayList<ASTNode>();
	BodyDeclaration decl= fAnalyzer.getEnclosingBodyDeclaration();
	ASTNode current= ASTResolving.findParentType(decl.getParent());
	if (fAnalyzer.isValidDestination(current)) {
		result.add(current);
	}
	if (current != null && (decl instanceof MethodDeclaration || decl instanceof Initializer || decl instanceof FieldDeclaration)) {
		ITypeBinding binding= ASTNodes.getEnclosingType(current);
		ASTNode next= ASTResolving.findParentType(current.getParent());
		while (next != null && binding != null && binding.isNested()) {
			if (fAnalyzer.isValidDestination(next)) {
				result.add(next);
			}
			current= next;
			binding= ASTNodes.getEnclosingType(current);
			next= ASTResolving.findParentType(next.getParent());
		}
	}
	fDestinations= result.toArray(new ASTNode[result.size()]);
	fDestination= fDestinations[fDestinationIndex];
}
 
Example #3
Source File: ReplaceTypeCodeWithStateStrategy.java    From JDeodorant with MIT License 6 votes vote down vote up
private Set<MethodDeclaration> getMethodDeclarationsWithinAnonymousClassDeclarations(MethodDeclaration methodDeclaration) {
	Set<MethodDeclaration> methods = new LinkedHashSet<MethodDeclaration>();
	ExpressionExtractor expressionExtractor = new ExpressionExtractor();
	List<Expression> classInstanceCreations = expressionExtractor.getClassInstanceCreations(methodDeclaration.getBody());
	for(Expression expression : classInstanceCreations) {
		ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation)expression;
		AnonymousClassDeclaration anonymousClassDeclaration = classInstanceCreation.getAnonymousClassDeclaration();
		if(anonymousClassDeclaration != null) {
			List<BodyDeclaration> bodyDeclarations = anonymousClassDeclaration.bodyDeclarations();
			for(BodyDeclaration bodyDeclaration : bodyDeclarations) {
				if(bodyDeclaration instanceof MethodDeclaration)
					methods.add((MethodDeclaration)bodyDeclaration);
			}
		}
	}
	return methods;
}
 
Example #4
Source File: DelegateCreator.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private TagElement getDelegateJavadocTag(BodyDeclaration declaration) throws JavaModelException {
	Assert.isNotNull(declaration);

	String msg= RefactoringCoreMessages.DelegateCreator_use_member_instead;
	int firstParam= msg.indexOf("{0}"); //$NON-NLS-1$
	Assert.isTrue(firstParam != -1);

	List<ASTNode> fragments= new ArrayList<>();
	TextElement text= getAst().newTextElement();
	text.setText(msg.substring(0, firstParam).trim());
	fragments.add(text);

	fragments.add(createJavadocMemberReferenceTag(declaration, getAst()));

	text= getAst().newTextElement();
	text.setText(msg.substring(firstParam + 3).trim());
	fragments.add(text);

	final TagElement tag= getAst().newTagElement();
	tag.setTagName(TagElement.TAG_DEPRECATED);
	tag.fragments().addAll(fragments);
	return tag;
}
 
Example #5
Source File: Java50Fix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static void createAddDeprecatedAnnotationOperations(CompilationUnit compilationUnit, IProblemLocation[] locations, List<CompilationUnitRewriteOperation> result) {
	for (int i= 0; i < locations.length; i++) {
		IProblemLocation problem= locations[i];

		if (isMissingDeprecationProblem(problem.getProblemId())) {
			ASTNode selectedNode= problem.getCoveringNode(compilationUnit);
			if (selectedNode != null) {

				ASTNode declaringNode= getDeclaringNode(selectedNode);
				if (declaringNode instanceof BodyDeclaration) {
					BodyDeclaration declaration= (BodyDeclaration) declaringNode;
					AnnotationRewriteOperation operation= new AnnotationRewriteOperation(declaration, DEPRECATED);
					result.add(operation);
				}
			}
		}
	}
}
 
Example #6
Source File: MoveHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private static BodyDeclaration getSelectedMemberDeclaration(ICompilationUnit unit, CodeActionParams params) {
	int start = DiagnosticsHelper.getStartOffset(unit, params.getRange());
	int end = DiagnosticsHelper.getEndOffset(unit, params.getRange());
	InnovationContext context = new InnovationContext(unit, start, end - start);
	context.setASTRoot(CodeActionHandler.getASTRoot(unit));

	ASTNode node = context.getCoveredNode();
	if (node == null) {
		node = context.getCoveringNode();
	}

	while (node != null && !(node instanceof BodyDeclaration)) {
		node = node.getParent();
	}

	if (node != null && (node instanceof MethodDeclaration || node instanceof FieldDeclaration || node instanceof AbstractTypeDeclaration) && JdtFlags.isStatic((BodyDeclaration) node)) {
		return (BodyDeclaration) node;
	}

	return null;
}
 
Example #7
Source File: Java50Fix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static void createAddOverrideAnnotationOperations(CompilationUnit compilationUnit, boolean addOverrideInterfaceAnnotation, IProblemLocation[] locations, List<CompilationUnitRewriteOperation> result) {
	for (int i= 0; i < locations.length; i++) {
		IProblemLocation problem= locations[i];
		int problemId= problem.getProblemId();
		
		if (isMissingOverrideAnnotationProblem(problemId)) {
			if (!isMissingOverrideAnnotationInterfaceProblem(problemId) || addOverrideInterfaceAnnotation) {
				ASTNode selectedNode= problem.getCoveringNode(compilationUnit);
				if (selectedNode != null) {

					ASTNode declaringNode= getDeclaringNode(selectedNode);
					if (declaringNode instanceof BodyDeclaration) {
						BodyDeclaration declaration= (BodyDeclaration) declaringNode;
						AnnotationRewriteOperation operation= new AnnotationRewriteOperation(declaration, OVERRIDE);
						result.add(operation);
					}
				}
			}
		}
	}
}
 
Example #8
Source File: ExpressionExtractor.java    From JDeodorant with MIT License 6 votes vote down vote up
private List<Expression> getExpressions(AnonymousClassDeclaration anonymousClassDeclaration) {
	List<Expression> expressionList = new ArrayList<Expression>();
	List<BodyDeclaration> bodyDeclarations = anonymousClassDeclaration.bodyDeclarations();
	for(BodyDeclaration bodyDeclaration : bodyDeclarations) {
		if(bodyDeclaration instanceof MethodDeclaration) {
			MethodDeclaration methodDeclaration = (MethodDeclaration)bodyDeclaration;
			Block body = methodDeclaration.getBody();
			if(body != null) {
				List<Statement> statements = body.statements();
				for(Statement statement : statements) {
					expressionList.addAll(getExpressions(statement));
				}
			}
		}
	}
	return expressionList;
}
 
Example #9
Source File: SuperTypeRefactoringProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the fields which reference the specified ast node.
 *
 * @param node
 *            the ast node
 * @param project
 *            the java project
 * @return the referencing fields
 * @throws JavaModelException
 *             if an error occurs
 */
protected final List<IField> getReferencingFields(final ASTNode node, final IJavaProject project) throws JavaModelException {
	List<IField> result= Collections.emptyList();
	if (node instanceof Type) {
		final BodyDeclaration parent= (BodyDeclaration) ASTNodes.getParent(node, BodyDeclaration.class);
		if (parent instanceof FieldDeclaration) {
			final List<VariableDeclarationFragment> fragments= ((FieldDeclaration) parent).fragments();
			result= new ArrayList<IField>(fragments.size());
			VariableDeclarationFragment fragment= null;
			for (final Iterator<VariableDeclarationFragment> iterator= fragments.iterator(); iterator.hasNext();) {
				fragment= iterator.next();
				final IField field= getCorrespondingField(fragment);
				if (field != null)
					result.add(field);
			}
		}
	}
	return result;
}
 
Example #10
Source File: ReadableFlattener.java    From junion with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
	public void preVisit(ASTNode node) {
//		int li = comp.getLineNumber(node.getStartPosition());
//		if(li > 0) {
//			while(li > currentLine) {
//				fBuffer.append("//" + li + "," + currentLine);
//				lineBreak2();
//			}
//		}
		
		if (node instanceof BodyDeclaration) {
			tabs += 4;
			lineBreak();
		}
		super.preVisit(node);
	}
 
Example #11
Source File: HeaderTypeDeclarationWriter.java    From juniversal with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
private void writeNestedTypes() {
	ArrayList<TypeDeclaration> publicTypes = new ArrayList<>();
	ArrayList<TypeDeclaration> protectedTypes = new ArrayList<>();
	ArrayList<TypeDeclaration> privateTypes = new ArrayList<>();

	for (BodyDeclaration bodyDeclaration : (List<BodyDeclaration>) typeDeclaration.bodyDeclarations()) {
		if (bodyDeclaration instanceof TypeDeclaration) {
			TypeDeclaration nestedTypeDeclaration = (TypeDeclaration) bodyDeclaration;
			AccessLevel accessLevel = ASTUtil.getAccessModifier(nestedTypeDeclaration.modifiers());

			if (accessLevel == AccessLevel.PUBLIC || accessLevel == AccessLevel.PACKAGE)
				publicTypes.add(nestedTypeDeclaration);
			else if (accessLevel == AccessLevel.PROTECTED)
				protectedTypes.add(nestedTypeDeclaration);
			else
				privateTypes.add(nestedTypeDeclaration);
		}
	}

	writeNestedTypeDeclarationsForAccessLevel(publicTypes, AccessLevel.PUBLIC);
	writeNestedTypeDeclarationsForAccessLevel(protectedTypes, AccessLevel.PROTECTED);
	writeNestedTypeDeclarationsForAccessLevel(privateTypes, AccessLevel.PRIVATE);
}
 
Example #12
Source File: ExtractMethodRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException {
	pm.beginTask(RefactoringCoreMessages.ExtractMethodRefactoring_checking_new_name, 2);
	pm.subTask(EMPTY);

	RefactoringStatus result = checkMethodName();
	result.merge(checkParameterNames());
	result.merge(checkVarargOrder());
	pm.worked(1);
	if (pm.isCanceled()) {
		throw new OperationCanceledException();
	}

	BodyDeclaration node = fAnalyzer.getEnclosingBodyDeclaration();
	if (node != null) {
		fAnalyzer.checkInput(result, fMethodName, fDestination);
		pm.worked(1);
	}
	pm.done();
	return result;
}
 
Example #13
Source File: JdtFlags.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
public static boolean isStatic(BodyDeclaration bodyDeclaration) {
	if (isNestedInterfaceOrAnnotation(bodyDeclaration)) {
		return true;
	}
	int nodeType= bodyDeclaration.getNodeType();
	if (!(nodeType == ASTNode.METHOD_DECLARATION || nodeType == ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION) &&
			isInterfaceOrAnnotationMember(bodyDeclaration)) {
		return true;
	}
	if (bodyDeclaration instanceof EnumConstantDeclaration) {
		return true;
	}
	if (bodyDeclaration instanceof EnumDeclaration && bodyDeclaration.getParent() instanceof AbstractTypeDeclaration) {
		return true;
	}
	return Modifier.isStatic(bodyDeclaration.getModifiers());
}
 
Example #14
Source File: UMLModelASTReader.java    From RefactoringMiner with MIT License 6 votes vote down vote up
private UMLAnonymousClass processAnonymousClassDeclaration(CompilationUnit cu, AnonymousClassDeclaration anonymous, String packageName, String binaryName, String codePath, String sourceFile) {
	List<BodyDeclaration> bodyDeclarations = anonymous.bodyDeclarations();
	LocationInfo locationInfo = generateLocationInfo(cu, sourceFile, anonymous, CodeElementType.ANONYMOUS_CLASS_DECLARATION);
	UMLAnonymousClass anonymousClass = new UMLAnonymousClass(packageName, binaryName, codePath, locationInfo);
	
	for(BodyDeclaration bodyDeclaration : bodyDeclarations) {
		if(bodyDeclaration instanceof FieldDeclaration) {
			FieldDeclaration fieldDeclaration = (FieldDeclaration)bodyDeclaration;
			List<UMLAttribute> attributes = processFieldDeclaration(cu, fieldDeclaration, false, sourceFile);
    		for(UMLAttribute attribute : attributes) {
    			attribute.setClassName(anonymousClass.getCodePath());
    			anonymousClass.addAttribute(attribute);
    		}
		}
		else if(bodyDeclaration instanceof MethodDeclaration) {
			MethodDeclaration methodDeclaration = (MethodDeclaration)bodyDeclaration;
			UMLOperation operation = processMethodDeclaration(cu, methodDeclaration, packageName, false, sourceFile);
			operation.setClassName(anonymousClass.getCodePath());
			anonymousClass.addOperation(operation);
		}
	}
	
	return anonymousClass;
}
 
Example #15
Source File: JavaASTUtils.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Finds an annotation of the given type (as a fully-qualified name) on a
 * declaration (type, method, field, etc.). If no such annotation exists,
 * returns <code>null</code>.
 */
@SuppressWarnings("unchecked")
public static Annotation findAnnotation(BodyDeclaration decl,
    String annotationTypeName) {
  if (annotationTypeName == null) {
    throw new IllegalArgumentException("annotationTypeName cannot be null");
  }

  List<ASTNode> modifiers = (List<ASTNode>) decl.getStructuralProperty(decl.getModifiersProperty());
  for (ASTNode modifier : modifiers) {
    if (modifier instanceof Annotation) {
      Annotation annotation = (Annotation) modifier;
      String typeName = getAnnotationTypeName(annotation);
      if (annotationTypeName.equals(typeName)) {
        return annotation;
      }
    }
  }
  return null;
}
 
Example #16
Source File: MoveHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private static MethodDeclaration getSelectedMethodDeclaration(ICompilationUnit unit, CodeActionParams params) {
	int start = DiagnosticsHelper.getStartOffset(unit, params.getRange());
	int end = DiagnosticsHelper.getEndOffset(unit, params.getRange());
	InnovationContext context = new InnovationContext(unit, start, end - start);
	context.setASTRoot(CodeActionHandler.getASTRoot(unit));

	ASTNode node = context.getCoveredNode();
	if (node == null) {
		node = context.getCoveringNode();
	}

	while (node != null && !(node instanceof BodyDeclaration)) {
		node = node.getParent();
	}

	if (node != null && node instanceof MethodDeclaration) {
		return (MethodDeclaration) node;
	}

	return null;
}
 
Example #17
Source File: LambdaExpressionsFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
static boolean isFunctionalAnonymous(ClassInstanceCreation node) {
	ITypeBinding typeBinding= node.resolveTypeBinding();
	if (typeBinding == null)
		return false;
	ITypeBinding[] interfaces= typeBinding.getInterfaces();
	if (interfaces.length != 1)
		return false;
	if (interfaces[0].getFunctionalInterfaceMethod() == null)
		return false;

	AnonymousClassDeclaration anonymTypeDecl= node.getAnonymousClassDeclaration();
	if (anonymTypeDecl == null || anonymTypeDecl.resolveBinding() == null)
		return false;
	
	List<BodyDeclaration> bodyDeclarations= anonymTypeDecl.bodyDeclarations();
	// cannot convert if there are fields or additional methods
	if (bodyDeclarations.size() != 1)
		return false;
	BodyDeclaration bodyDeclaration= bodyDeclarations.get(0);
	if (!(bodyDeclaration instanceof MethodDeclaration))
		return false;

	MethodDeclaration methodDecl= (MethodDeclaration) bodyDeclaration;
	IMethodBinding methodBinding= methodDecl.resolveBinding();

	if (methodBinding == null)
		return false;
	// generic lambda expressions are not allowed
	if (methodBinding.isGenericMethod())
		return false;

	// lambda cannot refer to 'this'/'super' literals
	if (SuperThisReferenceFinder.hasReference(methodDecl))
		return false;
	
	if (!isInTargetTypeContext(node))
		return false;
	
	return true;
}
 
Example #18
Source File: ASTNodeSearchUtil.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static List<BodyDeclaration> getBodyDeclarationList(IType iType, CompilationUnit cuNode) throws JavaModelException {
	if (iType.isAnonymous()) {
		return getClassInstanceCreationNode(iType, cuNode).getAnonymousClassDeclaration().bodyDeclarations();
	} else {
		return getAbstractTypeDeclarationNode(iType, cuNode).bodyDeclarations();
	}
}
 
Example #19
Source File: SharedUtils.java    From txtUML with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @return the annotation node if it is present on the declaration or null
 */
public static Annotation obtainAnnotation(BodyDeclaration declaration, Class<?> annotationClass) {
	for (Object mod : declaration.modifiers()) {
		IExtendedModifier modifier = (IExtendedModifier) mod;
		if (modifier.isAnnotation()) {
			Annotation annotation = (Annotation) modifier;
			if (identicalAnnotations(annotation, annotationClass)) {
				return annotation;
			}
		}
	}
	return null;
}
 
Example #20
Source File: HierarchyProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected static void copyJavadocNode(final ASTRewrite rewrite, final BodyDeclaration oldDeclaration, final BodyDeclaration newDeclaration) {
	final Javadoc predecessor= oldDeclaration.getJavadoc();
	if (predecessor != null) {
		String newString= ASTNodes.getNodeSource(predecessor, false, true);
		if (newString != null) {
			newDeclaration.setJavadoc((Javadoc) rewrite.createStringPlaceholder(newString, ASTNode.JAVADOC));
		}
	}
}
 
Example #21
Source File: TargetProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public ASTNode[] getInvocations(BodyDeclaration declaration, IProgressMonitor pm) {
	BodyData data= fBodies.get(declaration);
	Assert.isNotNull(data);
	fastDone(pm);
	return data.getInvocations();
}
 
Example #22
Source File: ASTResolving.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the lambda expression node which encloses the given <code>node</code>, or
 * <code>null</code> if none.
 * 
 * @param node the node
 * @return the enclosing lambda expression node for the given <code>node</code>, or
 *         <code>null</code> if none
 * 
 * @since 3.10
 */
public static LambdaExpression findEnclosingLambdaExpression(ASTNode node) {
	node= node.getParent();
	while (node != null) {
		if (node instanceof LambdaExpression) {
			return (LambdaExpression) node;
		}
		if (node instanceof BodyDeclaration || node instanceof AnonymousClassDeclaration) {
			return null;
		}
		node= node.getParent();
	}
	return null;
}
 
Example #23
Source File: AssignToVariableAssistProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private int findFieldInsertIndex(List<BodyDeclaration> decls, int currPos) {
	for (int i= decls.size() - 1; i >= 0; i--) {
		ASTNode curr= decls.get(i);
		if (curr instanceof FieldDeclaration && currPos > curr.getStartPosition() + curr.getLength()) {
			return i + 1;
		}
	}
	return 0;
}
 
Example #24
Source File: JavaStatementPostfixContext.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private VariableDeclarationFragment addFieldDeclaration(ASTRewrite rewrite, org.eclipse.jdt.core.dom.ASTNode newTypeDecl, int modifiers, String varName, String qualifiedName, String value) {

		ChildListPropertyDescriptor property = ASTNodes.getBodyDeclarationsProperty(newTypeDecl);
		List<BodyDeclaration> decls = ASTNodes.getBodyDeclarations(newTypeDecl);
		AST ast = newTypeDecl.getAST();
		
		VariableDeclarationFragment newDeclFrag = ast.newVariableDeclarationFragment();
		newDeclFrag.setName(ast.newSimpleName(varName));
		
		Type type = createType(Signature.createTypeSignature(qualifiedName, true), ast);
		
		if (value != null && value.trim().length() > 0) {
			Expression e = createExpression(value);
			Expression ne = (Expression) org.eclipse.jdt.core.dom.ASTNode.copySubtree(ast, e);
			newDeclFrag.setInitializer(ne);
			
		} else {
			if (Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)) {
				newDeclFrag.setInitializer(ASTNodeFactory.newDefaultExpression(ast, type, 0));
			}
		}

		FieldDeclaration newDecl = ast.newFieldDeclaration(newDeclFrag);
		newDecl.setType(type);
		newDecl.modifiers().addAll(ASTNodeFactory.newModifiers(ast, modifiers));

		int insertIndex = findFieldInsertIndex(decls, getCompletionOffset(), modifiers);
		rewrite.getListRewrite(newTypeDecl, property).insertAt(newDecl, insertIndex, null);
		
		return newDeclFrag;
	}
 
Example #25
Source File: LocalVariableIndex.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Computes the maximum number of local variable declarations in the
 * given body declaration.
 *
 * @param declaration the body declaration. Must either be a method
 *  declaration, or an initializer, or a field declaration.
 * @return the maximum number of local variables
 */
public static int perform(BodyDeclaration declaration) {
	Assert.isTrue(declaration != null);
	switch (declaration.getNodeType()) {
		case ASTNode.METHOD_DECLARATION:
		case ASTNode.FIELD_DECLARATION:
		case ASTNode.INITIALIZER:
			return internalPerform(declaration);
		default:
			throw new IllegalArgumentException(declaration.toString());
	}
}
 
Example #26
Source File: AbstractMethodCorrectionProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private int findMethodInsertIndex(List<BodyDeclaration> decls, int currPos) {
	int nDecls= decls.size();
	for (int i= 0; i < nDecls; i++) {
		BodyDeclaration curr= decls.get(i);
		if (curr instanceof MethodDeclaration && currPos < curr.getStartPosition() + curr.getLength()) {
			return i + 1;
		}
	}
	return nDecls;
}
 
Example #27
Source File: DelegateMethodCreator.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected ASTNode createDocReference(final BodyDeclaration declaration) throws JavaModelException {
	fDocMethodReference= getAst().newMethodRef();
	fDocMethodReference.setName(getAst().newSimpleName(getNewElementName()));
	if (isMoveToAnotherFile()) {
		fDocMethodReference.setQualifier(createDestinationTypeName());
	}
	createArguments((MethodDeclaration) declaration, fDocMethodReference.parameters(), false);
	return fDocMethodReference;
}
 
Example #28
Source File: ASTNodes.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static int getOrderPreference(BodyDeclaration member, MembersOrderPreferenceCache store) {
	int memberType= member.getNodeType();
	int modifiers= member.getModifiers();

	switch (memberType) {
		case ASTNode.TYPE_DECLARATION:
		case ASTNode.ENUM_DECLARATION :
		case ASTNode.ANNOTATION_TYPE_DECLARATION :
			return store.getCategoryIndex(MembersOrderPreferenceCache.TYPE_INDEX) * 2;
		case ASTNode.FIELD_DECLARATION:
			if (Modifier.isStatic(modifiers)) {
				int index= store.getCategoryIndex(MembersOrderPreferenceCache.STATIC_FIELDS_INDEX) * 2;
				if (Modifier.isFinal(modifiers)) {
					return index; // first final static, then static
				}
				return index + 1;
			}
			return store.getCategoryIndex(MembersOrderPreferenceCache.FIELDS_INDEX) * 2;
		case ASTNode.INITIALIZER:
			if (Modifier.isStatic(modifiers)) {
				return store.getCategoryIndex(MembersOrderPreferenceCache.STATIC_INIT_INDEX) * 2;
			}
			return store.getCategoryIndex(MembersOrderPreferenceCache.INIT_INDEX) * 2;
		case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION:
			return store.getCategoryIndex(MembersOrderPreferenceCache.METHOD_INDEX) * 2;
		case ASTNode.METHOD_DECLARATION:
			if (Modifier.isStatic(modifiers)) {
				return store.getCategoryIndex(MembersOrderPreferenceCache.STATIC_METHODS_INDEX) * 2;
			}
			if (((MethodDeclaration) member).isConstructor()) {
				return store.getCategoryIndex(MembersOrderPreferenceCache.CONSTRUCTORS_INDEX) * 2;
			}
			return store.getCategoryIndex(MembersOrderPreferenceCache.METHOD_INDEX) * 2;
		default:
			return 100;
	}
}
 
Example #29
Source File: DelegateFieldCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected ASTNode createDocReference(BodyDeclaration declaration) {
	MemberRef ref= getAst().newMemberRef();
	ref.setName(getAst().newSimpleName(getNewElementName()));

	if (isMoveToAnotherFile())
		ref.setQualifier(createDestinationTypeName());
	return ref;
}
 
Example #30
Source File: AbstractMethodCorrectionProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private int findConstructorInsertIndex(List<BodyDeclaration> decls) {
	int nDecls= decls.size();
	int lastMethod= 0;
	for (int i= nDecls - 1; i >= 0; i--) {
		BodyDeclaration curr= decls.get(i);
		if (curr instanceof MethodDeclaration) {
			if (((MethodDeclaration) curr).isConstructor()) {
				return i + 1;
			}
			lastMethod= i;
		}
	}
	return lastMethod;
}