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

The following examples show how to use org.eclipse.jdt.core.dom.EnumConstantDeclaration. 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: DOMFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public boolean visit(AnonymousClassDeclaration node) {
	ASTNode name;
	ASTNode parent = node.getParent();
	switch (parent.getNodeType()) {
		case ASTNode.CLASS_INSTANCE_CREATION:
			name = ((ClassInstanceCreation) parent).getType();
			if (name.getNodeType() == ASTNode.PARAMETERIZED_TYPE) {
				name = ((ParameterizedType) name).getType();
			}
			break;
		case ASTNode.ENUM_CONSTANT_DECLARATION:
			name = ((EnumConstantDeclaration) parent).getName();
			break;
		default:
			return true;
	}
	if (found(node, name) && this.resolveBinding)
		this.foundBinding = node.resolveBinding();
	return true;
}
 
Example #2
Source File: UnimplementedCodeFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static ASTNode getSelectedTypeNode(CompilationUnit root, IProblemLocation problem) {
	ASTNode selectedNode= problem.getCoveringNode(root);
	if (selectedNode == null)
		return null;

	if (selectedNode.getNodeType() == ASTNode.ANONYMOUS_CLASS_DECLARATION) { // bug 200016
		selectedNode= selectedNode.getParent();
	}

	if (selectedNode.getLocationInParent() == EnumConstantDeclaration.NAME_PROPERTY) {
		selectedNode= selectedNode.getParent();
	}
	if (selectedNode.getNodeType() == ASTNode.SIMPLE_NAME && selectedNode.getParent() instanceof AbstractTypeDeclaration) {
		return selectedNode.getParent();
	} else if (selectedNode.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION) {
		return ((ClassInstanceCreation) selectedNode).getAnonymousClassDeclaration();
	} else if (selectedNode.getNodeType() == ASTNode.ENUM_CONSTANT_DECLARATION) {
		EnumConstantDeclaration enumConst= (EnumConstantDeclaration) selectedNode;
		if (enumConst.getAnonymousClassDeclaration() != null)
			return enumConst.getAnonymousClassDeclaration();
		return enumConst;
	} else {
		return null;
	}
}
 
Example #3
Source File: UnimplementedCodeFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean isTypeBindingNull(ASTNode typeNode) {
	if (typeNode instanceof AbstractTypeDeclaration) {
		AbstractTypeDeclaration abstractTypeDeclaration= (AbstractTypeDeclaration) typeNode;
		if (abstractTypeDeclaration.resolveBinding() == null)
			return true;

		return false;
	} else if (typeNode instanceof AnonymousClassDeclaration) {
		AnonymousClassDeclaration anonymousClassDeclaration= (AnonymousClassDeclaration) typeNode;
		if (anonymousClassDeclaration.resolveBinding() == null)
			return true;

		return false;
	} else if (typeNode instanceof EnumConstantDeclaration) {
		return false;
	} else {
		return true;
	}
}
 
Example #4
Source File: ModifierRewrite.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private ListRewrite evaluateListRewrite(ASTRewrite rewrite, ASTNode declNode) {
	switch (declNode.getNodeType()) {
		case ASTNode.METHOD_DECLARATION:
			return rewrite.getListRewrite(declNode, MethodDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.FIELD_DECLARATION:
			return rewrite.getListRewrite(declNode, FieldDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.VARIABLE_DECLARATION_EXPRESSION:
			return rewrite.getListRewrite(declNode, VariableDeclarationExpression.MODIFIERS2_PROPERTY);
		case ASTNode.VARIABLE_DECLARATION_STATEMENT:
			return rewrite.getListRewrite(declNode, VariableDeclarationStatement.MODIFIERS2_PROPERTY);
		case ASTNode.SINGLE_VARIABLE_DECLARATION:
			return rewrite.getListRewrite(declNode, SingleVariableDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.TYPE_DECLARATION:
			return rewrite.getListRewrite(declNode, TypeDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.ENUM_DECLARATION:
			return rewrite.getListRewrite(declNode, EnumDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.ANNOTATION_TYPE_DECLARATION:
			return rewrite.getListRewrite(declNode, AnnotationTypeDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.ENUM_CONSTANT_DECLARATION:
			return rewrite.getListRewrite(declNode, EnumConstantDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION:
			return rewrite.getListRewrite(declNode, AnnotationTypeMemberDeclaration.MODIFIERS2_PROPERTY);
		default:
			throw new IllegalArgumentException("node has no modifiers: " + declNode.getClass().getName()); //$NON-NLS-1$
	}
}
 
Example #5
Source File: ExtractMethodInputPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private String getLabel(ASTNode node) {
	if (node instanceof AbstractTypeDeclaration) {
		return ((AbstractTypeDeclaration)node).getName().getIdentifier();
	} else if (node instanceof AnonymousClassDeclaration) {
		if (node.getLocationInParent() == ClassInstanceCreation.ANONYMOUS_CLASS_DECLARATION_PROPERTY) {
			ClassInstanceCreation creation= (ClassInstanceCreation)node.getParent();
			return Messages.format(
				RefactoringMessages.ExtractMethodInputPage_anonymous_type_label,
				BasicElementLabels.getJavaElementName(ASTNodes.asString(creation.getType())));
		} else if (node.getLocationInParent() == EnumConstantDeclaration.ANONYMOUS_CLASS_DECLARATION_PROPERTY) {
			EnumConstantDeclaration decl= (EnumConstantDeclaration)node.getParent();
			return decl.getName().getIdentifier();
		}
	}
	return "UNKNOWN"; //$NON-NLS-1$
}
 
Example #6
Source File: NewVariableCorrectionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private ASTRewrite doAddEnumConst(CompilationUnit astRoot) {
	SimpleName node= fOriginalNode;

	ASTNode newTypeDecl= astRoot.findDeclaringNode(fSenderBinding);
	if (newTypeDecl == null) {
		astRoot= ASTResolving.createQuickFixAST(getCompilationUnit(), null);
		newTypeDecl= astRoot.findDeclaringNode(fSenderBinding.getKey());
	}

	if (newTypeDecl != null) {
		AST ast= newTypeDecl.getAST();

		ASTRewrite rewrite= ASTRewrite.create(ast);

		EnumConstantDeclaration constDecl= ast.newEnumConstantDeclaration();
		constDecl.setName(ast.newSimpleName(node.getIdentifier()));

		ListRewrite listRewriter= rewrite.getListRewrite(newTypeDecl, EnumDeclaration.ENUM_CONSTANTS_PROPERTY);
		listRewriter.insertLast(constDecl, null);

		addLinkedPosition(rewrite.track(constDecl.getName()), false, KEY_NAME);

		return rewrite;
	}
	return null;
}
 
Example #7
Source File: Invocations.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static IMethodBinding resolveBinding(ASTNode invocation) {
	switch (invocation.getNodeType()) {
		case ASTNode.METHOD_INVOCATION:
			return ((MethodInvocation)invocation).resolveMethodBinding();
		case ASTNode.SUPER_METHOD_INVOCATION:
			return ((SuperMethodInvocation)invocation).resolveMethodBinding();
			
		case ASTNode.CONSTRUCTOR_INVOCATION:
			return ((ConstructorInvocation)invocation).resolveConstructorBinding();
		case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
			return ((SuperConstructorInvocation)invocation).resolveConstructorBinding();
			
		case ASTNode.CLASS_INSTANCE_CREATION:
			return ((ClassInstanceCreation)invocation).resolveConstructorBinding();
		case ASTNode.ENUM_CONSTANT_DECLARATION:
			return ((EnumConstantDeclaration)invocation).resolveConstructorBinding();
			
		default:
			throw new IllegalArgumentException(invocation.toString());
	}
}
 
Example #8
Source File: Invocations.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static ChildListPropertyDescriptor getArgumentsProperty(ASTNode invocation) {
	switch (invocation.getNodeType()) {
		case ASTNode.METHOD_INVOCATION:
			return MethodInvocation.ARGUMENTS_PROPERTY;
		case ASTNode.SUPER_METHOD_INVOCATION:
			return SuperMethodInvocation.ARGUMENTS_PROPERTY;
			
		case ASTNode.CONSTRUCTOR_INVOCATION:
			return ConstructorInvocation.ARGUMENTS_PROPERTY;
		case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
			return SuperConstructorInvocation.ARGUMENTS_PROPERTY;
			
		case ASTNode.CLASS_INSTANCE_CREATION:
			return ClassInstanceCreation.ARGUMENTS_PROPERTY;
		case ASTNode.ENUM_CONSTANT_DECLARATION:
			return EnumConstantDeclaration.ARGUMENTS_PROPERTY;
			
		default:
			throw new IllegalArgumentException(invocation.toString());
	}
}
 
Example #9
Source File: Invocations.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static List<Expression> getArguments(ASTNode invocation) {
	switch (invocation.getNodeType()) {
		case ASTNode.METHOD_INVOCATION:
			return ((MethodInvocation)invocation).arguments();
		case ASTNode.SUPER_METHOD_INVOCATION:
			return ((SuperMethodInvocation)invocation).arguments();
			
		case ASTNode.CONSTRUCTOR_INVOCATION:
			return ((ConstructorInvocation)invocation).arguments();
		case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
			return ((SuperConstructorInvocation)invocation).arguments();
			
		case ASTNode.CLASS_INSTANCE_CREATION:
			return ((ClassInstanceCreation)invocation).arguments();
		case ASTNode.ENUM_CONSTANT_DECLARATION:
			return ((EnumConstantDeclaration)invocation).arguments();
			
		default:
			throw new IllegalArgumentException(invocation.toString());
	}
}
 
Example #10
Source File: ChangeSignatureProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
protected String getFullTypeName() {
	ASTNode node= getNode();
	while (true) {
		node= node.getParent();
		if (node instanceof AbstractTypeDeclaration) {
			String typeName= ((AbstractTypeDeclaration) node).getName().getIdentifier();
			if (getNode() instanceof LambdaExpression) {
				return Messages.format(RefactoringCoreMessages.ChangeSignatureRefactoring_lambda_expression, typeName);
			}
			return typeName;
		} else if (node instanceof ClassInstanceCreation) {
			ClassInstanceCreation cic= (ClassInstanceCreation) node;
			return Messages.format(RefactoringCoreMessages.ChangeSignatureRefactoring_anonymous_subclass, BasicElementLabels.getJavaElementName(ASTNodes.asString(cic.getType())));
		} else if (node instanceof EnumConstantDeclaration) {
			EnumDeclaration ed= (EnumDeclaration) node.getParent();
			return Messages.format(RefactoringCoreMessages.ChangeSignatureRefactoring_anonymous_subclass, BasicElementLabels.getJavaElementName(ASTNodes.asString(ed.getName())));
		}
	}
}
 
Example #11
Source File: ChangeSignatureProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private OccurrenceUpdate<? extends ASTNode> createOccurrenceUpdate(ASTNode node, CompilationUnitRewrite cuRewrite, RefactoringStatus result) {
	if (BUG_89686 && node instanceof SimpleName && node.getParent() instanceof EnumConstantDeclaration)
		node= node.getParent();

	if (Invocations.isInvocationWithArguments(node))
		return new ReferenceUpdate(node, cuRewrite, result);

	else if (node instanceof SimpleName && node.getParent() instanceof MethodDeclaration)
		return new DeclarationUpdate((MethodDeclaration) node.getParent(), cuRewrite, result);

	else if (node instanceof MemberRef || node instanceof MethodRef)
		return new DocReferenceUpdate(node, cuRewrite, result);

	else if (ASTNodes.getParent(node, ImportDeclaration.class) != null)
		return new StaticImportUpdate((ImportDeclaration) ASTNodes.getParent(node, ImportDeclaration.class), cuRewrite, result);

	else if (node instanceof LambdaExpression)
		return new LambdaExpressionUpdate((LambdaExpression) node, cuRewrite, result);

	else if (node.getLocationInParent() == ExpressionMethodReference.NAME_PROPERTY)
		return new ExpressionMethodRefUpdate((ExpressionMethodReference) node.getParent(), cuRewrite, result);

	else
		return new NullOccurrenceUpdate(node, cuRewrite, result);
}
 
Example #12
Source File: UglyMathCommentsExtractor.java    From compiler with Apache License 2.0 6 votes vote down vote up
private static int getNodeStartPosition(final ASTNode node) {
    if (node instanceof MethodDeclaration) {
        MethodDeclaration decl = (MethodDeclaration) node;
        return decl.isConstructor() ? decl.getName().getStartPosition() : decl.getReturnType2().getStartPosition();
    } else if (node instanceof FieldDeclaration) {
        return ((FieldDeclaration) node).getType().getStartPosition();
    } else if (node instanceof AbstractTypeDeclaration) {
        return ((AbstractTypeDeclaration) node).getName().getStartPosition();
    } else if (node instanceof AnnotationTypeMemberDeclaration) {
        return ((AnnotationTypeMemberDeclaration) node).getName().getStartPosition();
    } else if (node instanceof EnumConstantDeclaration) {
        return ((EnumConstantDeclaration) node).getName().getStartPosition();
    } else if (node instanceof PackageDeclaration) {
        return ((PackageDeclaration) node).getName().getStartPosition();
    }
    /* TODO: Initializer */

    return node.getStartPosition();
}
 
Example #13
Source File: JavaASTFlattener.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public boolean visit(final EnumConstantDeclaration node) {
  Javadoc _javadoc = node.getJavadoc();
  boolean _tripleNotEquals = (_javadoc != null);
  if (_tripleNotEquals) {
    node.getJavadoc().accept(this);
  }
  this.appendModifiers(node, node.modifiers());
  node.getName().accept(this);
  boolean _isEmpty = node.arguments().isEmpty();
  boolean _not = (!_isEmpty);
  if (_not) {
    this.addProblem(node, "Enum constant cannot have any arguments");
    this.appendToBuffer("(");
    this.visitAllSeparatedByComma(node.arguments());
    this.appendToBuffer(")");
  }
  AnonymousClassDeclaration _anonymousClassDeclaration = node.getAnonymousClassDeclaration();
  boolean _tripleNotEquals_1 = (_anonymousClassDeclaration != null);
  if (_tripleNotEquals_1) {
    this.addProblem(node, "Enum constant cannot have any anonymous class declarations");
    node.getAnonymousClassDeclaration().accept(this);
  }
  return false;
}
 
Example #14
Source File: NewVariableCorrectionProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private ASTRewrite doAddEnumConst(CompilationUnit astRoot) {
	SimpleName node= fOriginalNode;

	ASTNode newTypeDecl= astRoot.findDeclaringNode(fSenderBinding);
	if (newTypeDecl == null) {
		astRoot= ASTResolving.createQuickFixAST(getCompilationUnit(), null);
		newTypeDecl= astRoot.findDeclaringNode(fSenderBinding.getKey());
	}

	if (newTypeDecl != null) {
		AST ast= newTypeDecl.getAST();

		ASTRewrite rewrite= ASTRewrite.create(ast);

		EnumConstantDeclaration constDecl= ast.newEnumConstantDeclaration();
		constDecl.setName(ast.newSimpleName(node.getIdentifier()));

		ListRewrite listRewriter= rewrite.getListRewrite(newTypeDecl, EnumDeclaration.ENUM_CONSTANTS_PROPERTY);
		listRewriter.insertLast(constDecl, null);

		return rewrite;
	}
	return null;
}
 
Example #15
Source File: CompilationUnitBuilder.java    From j2cl with Apache License 2.0 6 votes vote down vote up
private void convert(EnumDeclaration enumDeclaration) {
  convertAndAddType(
      enumDeclaration,
      enumType -> {
        checkState(enumType.isEnum());

        int ordinal = 0;
        for (EnumConstantDeclaration enumConstantDeclaration :
            JdtUtils.<EnumConstantDeclaration>asTypedList(enumDeclaration.enumConstants())) {
          enumType.addField(ordinal, convert(enumConstantDeclaration));
          ordinal++;
        }
        EnumMethodsCreator.applyTo(enumType);
        return null;
      });
}
 
Example #16
Source File: ModifierRewrite.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private ListRewrite evaluateListRewrite(ASTRewrite rewrite, ASTNode declNode) {
	switch (declNode.getNodeType()) {
		case ASTNode.METHOD_DECLARATION:
			return rewrite.getListRewrite(declNode, MethodDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.FIELD_DECLARATION:
			return rewrite.getListRewrite(declNode, FieldDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.VARIABLE_DECLARATION_EXPRESSION:
			return rewrite.getListRewrite(declNode, VariableDeclarationExpression.MODIFIERS2_PROPERTY);
		case ASTNode.VARIABLE_DECLARATION_STATEMENT:
			return rewrite.getListRewrite(declNode, VariableDeclarationStatement.MODIFIERS2_PROPERTY);
		case ASTNode.SINGLE_VARIABLE_DECLARATION:
			return rewrite.getListRewrite(declNode, SingleVariableDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.TYPE_DECLARATION:
			return rewrite.getListRewrite(declNode, TypeDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.ENUM_DECLARATION:
			return rewrite.getListRewrite(declNode, EnumDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.ANNOTATION_TYPE_DECLARATION:
			return rewrite.getListRewrite(declNode, AnnotationTypeDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.ENUM_CONSTANT_DECLARATION:
			return rewrite.getListRewrite(declNode, EnumConstantDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION:
			return rewrite.getListRewrite(declNode, AnnotationTypeMemberDeclaration.MODIFIERS2_PROPERTY);
		default:
			throw new IllegalArgumentException("node has no modifiers: " + declNode.getClass().getName()); //$NON-NLS-1$
	}
}
 
Example #17
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 #18
Source File: TargetProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void endVisit(EnumConstantDeclaration node) {
	if (fCurrent.hasInvocations()) {
		result.put(node, fCurrent);
	}
	endVisitBodyDeclaration();
}
 
Example #19
Source File: APIVersion.java    From apidiff with MIT License 5 votes vote down vote up
public EnumConstantDeclaration getEqualVersionConstant(EnumConstantDeclaration constant, EnumDeclaration enumReference) {
	EnumDeclaration thisVersionEnum = this.getVersionAccessibleEnum(enumReference);
	for(Object thisVersionConstant : thisVersionEnum.enumConstants()){
		if(((EnumConstantDeclaration)thisVersionConstant).getName().toString().equals(constant.getName().toString()))
			return ((EnumConstantDeclaration)thisVersionConstant);
	}

	return null;
}
 
Example #20
Source File: JdtFlags.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 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 #21
Source File: CompilationUnitBuilder.java    From j2cl with Apache License 2.0 5 votes vote down vote up
private Field convert(EnumConstantDeclaration enumConstantDeclaration) {
  IMethodBinding enumConstructorBinding = enumConstantDeclaration.resolveConstructorBinding();
  if (enumConstantDeclaration.getAnonymousClassDeclaration() != null) {
    convertAnonymousClassDeclaration(
        enumConstantDeclaration.getAnonymousClassDeclaration(), enumConstructorBinding, null);
  }

  FieldDescriptor fieldDescriptor =
      JdtUtils.createFieldDescriptor(enumConstantDeclaration.resolveVariable());

  // Since initializing custom values for JsEnum requires literals, we fold constant expressions
  // to give more options to the user. E.g. -1 is a unary expression but the expression is a
  // constant that can be evaluated at compile time, hence it makes sense to allow it.
  boolean foldConstantArguments = fieldDescriptor.getEnclosingTypeDescriptor().isJsEnum();

  MethodDescriptor methodDescriptor = JdtUtils.createMethodDescriptor(enumConstructorBinding);
  Expression initializer =
      NewInstance.Builder.from(methodDescriptor)
          .setArguments(
              convertArguments(
                  enumConstructorBinding,
                  JdtUtils.asTypedList(enumConstantDeclaration.arguments()),
                  foldConstantArguments))
          .build();

  checkArgument(fieldDescriptor.isEnumConstant());
  return Field.Builder.from(fieldDescriptor)
      .setInitializer(initializer)
      .setSourcePosition(getSourcePosition(enumConstantDeclaration))
      .setNameSourcePosition(Optional.of(getSourcePosition(enumConstantDeclaration.getName())))
      .build();
}
 
Example #22
Source File: AdvancedQuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static Expression getBooleanExpression(ASTNode node) {
	if (!(node instanceof Expression)) {
		return null;
	}

	// check if the node is a location where it can be negated
	StructuralPropertyDescriptor locationInParent= node.getLocationInParent();
	if (locationInParent == QualifiedName.NAME_PROPERTY) {
		node= node.getParent();
		locationInParent= node.getLocationInParent();
	}
	while (locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) {
		node= node.getParent();
		locationInParent= node.getLocationInParent();
	}
	Expression expression= (Expression) node;
	if (!isBoolean(expression)) {
		return null;
	}
	if (expression.getParent() instanceof InfixExpression) {
		return expression;
	}
	if (locationInParent == Assignment.RIGHT_HAND_SIDE_PROPERTY || locationInParent == IfStatement.EXPRESSION_PROPERTY
			|| locationInParent == WhileStatement.EXPRESSION_PROPERTY || locationInParent == DoStatement.EXPRESSION_PROPERTY
			|| locationInParent == ReturnStatement.EXPRESSION_PROPERTY || locationInParent == ForStatement.EXPRESSION_PROPERTY
			|| locationInParent == AssertStatement.EXPRESSION_PROPERTY || locationInParent == MethodInvocation.ARGUMENTS_PROPERTY
			|| locationInParent == ConstructorInvocation.ARGUMENTS_PROPERTY || locationInParent == SuperMethodInvocation.ARGUMENTS_PROPERTY
			|| locationInParent == EnumConstantDeclaration.ARGUMENTS_PROPERTY || locationInParent == SuperConstructorInvocation.ARGUMENTS_PROPERTY
			|| locationInParent == ClassInstanceCreation.ARGUMENTS_PROPERTY || locationInParent == ConditionalExpression.EXPRESSION_PROPERTY
			|| locationInParent == PrefixExpression.OPERAND_PROPERTY) {
		return expression;
	}
	return null;
}
 
Example #23
Source File: ASTNodeDeleteUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static ASTNode[] getNodesToDelete(IJavaElement element, CompilationUnit cuNode) throws JavaModelException {
	// fields are different because you don't delete the whole declaration but only a fragment of it
	if (element.getElementType() == IJavaElement.FIELD) {
		if (JdtFlags.isEnum((IField) element))
			return new ASTNode[] { ASTNodeSearchUtil.getEnumConstantDeclaration((IField) element, cuNode)};
		else
			return new ASTNode[] { ASTNodeSearchUtil.getFieldDeclarationFragmentNode((IField) element, cuNode)};
	}
	if (element.getElementType() == IJavaElement.TYPE && ((IType) element).isLocal()) {
		IType type= (IType) element;
		if (type.isAnonymous()) {
			if (type.getParent().getElementType() == IJavaElement.FIELD) {
				EnumConstantDeclaration enumDecl= ASTNodeSearchUtil.getEnumConstantDeclaration((IField) element.getParent(), cuNode);
				if (enumDecl != null && enumDecl.getAnonymousClassDeclaration() != null)  {
					return new ASTNode[] { enumDecl.getAnonymousClassDeclaration() };
				}
			}
			ClassInstanceCreation creation= ASTNodeSearchUtil.getClassInstanceCreationNode(type, cuNode);
			if (creation != null) {
				if (creation.getLocationInParent() == ExpressionStatement.EXPRESSION_PROPERTY) {
					return new ASTNode[] { creation.getParent() };
				} else if (creation.getLocationInParent() == VariableDeclarationFragment.INITIALIZER_PROPERTY) {
					return new ASTNode[] { creation};
				}
				return new ASTNode[] { creation.getAnonymousClassDeclaration() };
			}
			return new ASTNode[0];
		} else {
			ASTNode[] nodes= ASTNodeSearchUtil.getDeclarationNodes(element, cuNode);
			// we have to delete the TypeDeclarationStatement
			nodes[0]= nodes[0].getParent();
			return nodes;
		}
	}
	return ASTNodeSearchUtil.getDeclarationNodes(element, cuNode);
}
 
Example #24
Source File: EnumDeclarationWriter.java    From juniversal with MIT License 5 votes vote down vote up
@Override public void write(EnumDeclaration enumDeclaration) {
    List modifiers = enumDeclaration.modifiers();

    writeAccessModifier(modifiers);

    // Skip the modifiers
    skipModifiers(modifiers);

    matchAndWrite("enum");

    copySpaceAndComments();
    writeNode(enumDeclaration.getName());

    copySpaceAndComments();
    matchAndWrite("{");

    // TODO: Support trailing comma
    writeCommaDelimitedNodes(enumDeclaration.enumConstants(), (EnumConstantDeclaration enumConstantDeclaration) -> {
        copySpaceAndComments();
        writeNode(enumConstantDeclaration.getName());

        if (enumConstantDeclaration.arguments().size() > 0)
            throw sourceNotSupported("Enum constants with arguments aren't currently supported; change the source to use a normal class, with public final members for the enum constants, instead");
    });

    if (enumDeclaration.bodyDeclarations().size() > 0)
        throw sourceNotSupported("Enums with methods/data members aren't currently supported; change the source to use a normal class instead, with public final members for the enum constants, instead");

    copySpaceAndComments();
    matchAndWrite("}");
}
 
Example #25
Source File: FlowAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void endVisit(EnumConstantDeclaration node) {
	if (skipNode(node)) {
		return;
	}
	GenericSequentialFlowInfo info = processSequential(node, node.arguments());
	process(info, node.getAnonymousClassDeclaration());
}
 
Example #26
Source File: ASTNodeDeleteUtil.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static ASTNode[] getNodesToDelete(IJavaElement element, CompilationUnit cuNode) throws JavaModelException {
	// fields are different because you don't delete the whole declaration but only a fragment of it
	if (element.getElementType() == IJavaElement.FIELD) {
		if (JdtFlags.isEnum((IField) element)) {
			return new ASTNode[] { ASTNodeSearchUtil.getEnumConstantDeclaration((IField) element, cuNode)};
		} else {
			return new ASTNode[] { ASTNodeSearchUtil.getFieldDeclarationFragmentNode((IField) element, cuNode)};
		}
	}
	if (element.getElementType() == IJavaElement.TYPE && ((IType) element).isLocal()) {
		IType type= (IType) element;
		if (type.isAnonymous()) {
			if (type.getParent().getElementType() == IJavaElement.FIELD) {
				EnumConstantDeclaration enumDecl= ASTNodeSearchUtil.getEnumConstantDeclaration((IField) element.getParent(), cuNode);
				if (enumDecl != null && enumDecl.getAnonymousClassDeclaration() != null)  {
					return new ASTNode[] { enumDecl.getAnonymousClassDeclaration() };
				}
			}
			ClassInstanceCreation creation= ASTNodeSearchUtil.getClassInstanceCreationNode(type, cuNode);
			if (creation != null) {
				if (creation.getLocationInParent() == ExpressionStatement.EXPRESSION_PROPERTY) {
					return new ASTNode[] { creation.getParent() };
				} else if (creation.getLocationInParent() == VariableDeclarationFragment.INITIALIZER_PROPERTY) {
					return new ASTNode[] { creation};
				}
				return new ASTNode[] { creation.getAnonymousClassDeclaration() };
			}
			return new ASTNode[0];
		} else {
			ASTNode[] nodes= ASTNodeSearchUtil.getDeclarationNodes(element, cuNode);
			// we have to delete the TypeDeclarationStatement
			nodes[0]= nodes[0].getParent();
			return nodes;
		}
	}
	return ASTNodeSearchUtil.getDeclarationNodes(element, cuNode);
}
 
Example #27
Source File: FlowAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void endVisit(EnumConstantDeclaration node) {
	if (skipNode(node))
		return;
	GenericSequentialFlowInfo info= processSequential(node, node.arguments());
	process(info, node.getAnonymousClassDeclaration());
}
 
Example #28
Source File: TypeVisitor.java    From repositoryminer with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public boolean visit(EnumConstantDeclaration node) {
	AbstractEnumConstant constant = new AbstractEnumConstant(node.getName().getIdentifier());

	List<String> expressions = new ArrayList<>();
	for (Expression exp : (List<Expression>) node.arguments()) {
		expressions.add(exp.toString());
	}
	constant.setArguments(expressions);

	enumConstants.add(constant);
	return true;
}
 
Example #29
Source File: ChangeSignatureProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean isRecursiveReference() {
	MethodDeclaration enclosingMethodDeclaration= (MethodDeclaration) ASTNodes.getParent(fNode, MethodDeclaration.class);
	if (enclosingMethodDeclaration == null)
		return false;

	IMethodBinding enclosingMethodBinding= enclosingMethodDeclaration.resolveBinding();
	if (enclosingMethodBinding == null)
		return false;

	if (fNode instanceof MethodInvocation)
		return enclosingMethodBinding == ((MethodInvocation)fNode).resolveMethodBinding();

	if (fNode instanceof SuperMethodInvocation) {
		IMethodBinding methodBinding= ((SuperMethodInvocation)fNode).resolveMethodBinding();
		return isSameMethod(methodBinding, enclosingMethodBinding);
	}

	if (fNode instanceof ClassInstanceCreation)
		return enclosingMethodBinding == ((ClassInstanceCreation)fNode).resolveConstructorBinding();

	if (fNode instanceof ConstructorInvocation)
		return enclosingMethodBinding == ((ConstructorInvocation)fNode).resolveConstructorBinding();

	if (fNode instanceof SuperConstructorInvocation) {
		return false; //Constructors don't override -> enclosing has not been changed -> no recursion
	}

	if (fNode instanceof EnumConstantDeclaration) {
		return false; //cannot define enum constant inside enum constructor
	}

	Assert.isTrue(false);
	return false;
}
 
Example #30
Source File: InvertBooleanUtility.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static Expression getBooleanExpression(ASTNode node) {
	if (!(node instanceof Expression)) {
		return null;
	}

	// check if the node is a location where it can be negated
	StructuralPropertyDescriptor locationInParent = node.getLocationInParent();
	if (locationInParent == QualifiedName.NAME_PROPERTY) {
		node = node.getParent();
		locationInParent = node.getLocationInParent();
	}
	while (locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) {
		node = node.getParent();
		locationInParent = node.getLocationInParent();
	}
	Expression expression = (Expression) node;
	if (!isBoolean(expression)) {
		return null;
	}
	if (expression.getParent() instanceof InfixExpression) {
		return expression;
	}
	if (locationInParent == Assignment.RIGHT_HAND_SIDE_PROPERTY || locationInParent == IfStatement.EXPRESSION_PROPERTY || locationInParent == WhileStatement.EXPRESSION_PROPERTY || locationInParent == DoStatement.EXPRESSION_PROPERTY
			|| locationInParent == ReturnStatement.EXPRESSION_PROPERTY || locationInParent == ForStatement.EXPRESSION_PROPERTY || locationInParent == AssertStatement.EXPRESSION_PROPERTY
			|| locationInParent == MethodInvocation.ARGUMENTS_PROPERTY || locationInParent == ConstructorInvocation.ARGUMENTS_PROPERTY || locationInParent == SuperMethodInvocation.ARGUMENTS_PROPERTY
			|| locationInParent == EnumConstantDeclaration.ARGUMENTS_PROPERTY || locationInParent == SuperConstructorInvocation.ARGUMENTS_PROPERTY || locationInParent == ClassInstanceCreation.ARGUMENTS_PROPERTY
			|| locationInParent == ConditionalExpression.EXPRESSION_PROPERTY || locationInParent == PrefixExpression.OPERAND_PROPERTY) {
		return expression;
	}
	return null;
}