Java Code Examples for org.eclipse.jdt.core.dom.TypeDeclaration#bodyDeclarations()

The following examples show how to use org.eclipse.jdt.core.dom.TypeDeclaration#bodyDeclarations() . 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: NewMethodCorrectionProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private int getInterfaceMethodModifiers(ASTNode targetTypeDecl, boolean createAbstractMethod) {
	// for interface and annotation members copy the modifiers from an existing member
	if (targetTypeDecl instanceof TypeDeclaration) {
		TypeDeclaration type= (TypeDeclaration) targetTypeDecl;
		MethodDeclaration[] methodDecls= type.getMethods();
		if (methodDecls.length > 0) {
			if (createAbstractMethod) {
				for (MethodDeclaration methodDeclaration : methodDecls) {
					IMethodBinding methodBinding= methodDeclaration.resolveBinding();
					if (methodBinding != null && JdtFlags.isAbstract(methodBinding)) {
						return methodDeclaration.getModifiers();
					}
				}
			}
			return methodDecls[0].getModifiers() & Modifier.PUBLIC;
		}
		List<BodyDeclaration> bodyDecls= type.bodyDeclarations();
		if (bodyDecls.size() > 0) {
			return bodyDecls.get(0).getModifiers() & Modifier.PUBLIC;
		}
	}
	return 0;
}
 
Example 2
Source File: CreateTypeMemberOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Generates an <code>ASTNode</code> based on the source of this operation
 * when there is likely a syntax error in the source.
 * Returns the source used to generate this node.
 */
protected String generateSyntaxIncorrectAST() {
	//create some dummy source to generate an ast node
	StringBuffer buff = new StringBuffer();
	IType type = getType();
	String lineSeparator = org.eclipse.jdt.internal.core.util.Util.getLineSeparator(this.source, type == null ? null : type.getJavaProject());
	buff.append(lineSeparator + " public class A {" + lineSeparator); //$NON-NLS-1$
	buff.append(this.source);
	buff.append(lineSeparator).append('}');
	ASTParser parser = ASTParser.newParser(AST.JLS8);
	parser.setSource(buff.toString().toCharArray());
	CompilationUnit compilationUnit = (CompilationUnit) parser.createAST(null);
	TypeDeclaration typeDeclaration = (TypeDeclaration) compilationUnit.types().iterator().next();
	List bodyDeclarations = typeDeclaration.bodyDeclarations();
	if (bodyDeclarations.size() != 0)
		this.createdNode = (ASTNode) bodyDeclarations.iterator().next();
	return buff.toString();
}
 
Example 3
Source File: TestQ20.java    From compiler with Apache License 2.0 5 votes vote down vote up
@Override
public boolean visit(TypeDeclaration node) {
//	classes++;
	for (Object d : node.bodyDeclarations()) {
		if (d instanceof FieldDeclaration)
			((FieldDeclaration)d).accept(this);
		if (d instanceof MethodDeclaration)
			((MethodDeclaration)d).accept(this);
	}
	return false;
}
 
Example 4
Source File: TestQ21.java    From compiler with Apache License 2.0 5 votes vote down vote up
@Override
public boolean visit(TypeDeclaration node) {
//	classes++;
	for (Object d : node.bodyDeclarations()) {
		if (d instanceof FieldDeclaration)
			((FieldDeclaration)d).accept(this);
		if (d instanceof MethodDeclaration)
			((MethodDeclaration)d).accept(this);
	}
	return false;
}
 
Example 5
Source File: JavaASTFlattener.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public boolean visit(final TypeDeclaration it) {
  boolean _isDummyType = this._aSTFlattenerUtils.isDummyType(it);
  if (_isDummyType) {
    this.visitAll(it.bodyDeclarations(), this.nl());
    return false;
  }
  boolean _isNotSupportedInnerType = this._aSTFlattenerUtils.isNotSupportedInnerType(it);
  if (_isNotSupportedInnerType) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("/* FIXME Non-static inner classes are not supported.*/");
    this.appendToBuffer(_builder.toString());
    this.addProblem(it, "Non-static inner classes are not supported.");
  }
  Javadoc _javadoc = it.getJavadoc();
  boolean _tripleNotEquals = (_javadoc != null);
  if (_tripleNotEquals) {
    it.getJavadoc().accept(this);
  }
  this.appendModifiers(it, it.modifiers());
  boolean _isInterface = it.isInterface();
  if (_isInterface) {
    this.appendToBuffer("interface ");
  } else {
    boolean _isPackageVisibility = this._aSTFlattenerUtils.isPackageVisibility(Iterables.<Modifier>filter(it.modifiers(), Modifier.class));
    if (_isPackageVisibility) {
      this.appendToBuffer("package ");
    }
    this.appendToBuffer("class ");
  }
  it.getName().accept(this);
  boolean _isEmpty = it.typeParameters().isEmpty();
  boolean _not = (!_isEmpty);
  if (_not) {
    this.appendTypeParameters(it.typeParameters());
  }
  this.appendSpaceToBuffer();
  Type _superclassType = it.getSuperclassType();
  boolean _tripleNotEquals_1 = (_superclassType != null);
  if (_tripleNotEquals_1) {
    this.appendToBuffer("extends ");
    it.getSuperclassType().accept(this);
    this.appendSpaceToBuffer();
  }
  boolean _isEmpty_1 = it.superInterfaceTypes().isEmpty();
  boolean _not_1 = (!_isEmpty_1);
  if (_not_1) {
    boolean _isInterface_1 = it.isInterface();
    if (_isInterface_1) {
      this.appendToBuffer("extends ");
    } else {
      this.appendToBuffer("implements ");
    }
    this.visitAllSeparatedByComma(it.superInterfaceTypes());
  }
  this.appendToBuffer("{");
  this.increaseIndent();
  BodyDeclaration prev = null;
  List _bodyDeclarations = it.bodyDeclarations();
  for (final BodyDeclaration body : ((Iterable<BodyDeclaration>) _bodyDeclarations)) {
    {
      if ((prev instanceof EnumConstantDeclaration)) {
        if ((body instanceof EnumConstantDeclaration)) {
          this.appendToBuffer(", ");
        } else {
          this.appendToBuffer("; ");
        }
      }
      this.appendLineWrapToBuffer();
      body.accept(this);
      prev = body;
    }
  }
  ASTNode _root = it.getRoot();
  if ((_root instanceof CompilationUnit)) {
    ASTNode _root_1 = it.getRoot();
    final CompilationUnit cu = ((CompilationUnit) _root_1);
    final Consumer<Comment> _function = (Comment it_1) -> {
      it_1.accept(this);
      this.assignedComments.add(it_1);
    };
    this.unAssignedComments(cu).forEach(_function);
  }
  this.decreaseIndent();
  this.appendLineWrapToBuffer();
  this.appendToBuffer("}");
  return false;
}
 
Example 6
Source File: VisitorBase.java    From txtUML with Eclipse Public License 1.0 4 votes vote down vote up
protected void acceptChildren(TypeDeclaration elem, VisitorBase visitor) {
	for (Object decl : elem.bodyDeclarations()) {
		((BodyDeclaration) decl).accept(visitor);
	}
	visitor.check();
}
 
Example 7
Source File: ConvertAnonymousToNestedRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private AbstractTypeDeclaration createNewNestedClass(CompilationUnitRewrite rewrite, ITypeBinding[] typeParameters) throws CoreException {
	final AST ast= fAnonymousInnerClassNode.getAST();

	final TypeDeclaration newDeclaration= ast.newTypeDeclaration();
	newDeclaration.setInterface(false);
	newDeclaration.setJavadoc(null);
	newDeclaration.modifiers().addAll(ASTNodeFactory.newModifiers(ast, createModifiersForNestedClass()));
	newDeclaration.setName(ast.newSimpleName(fClassName));

	TypeParameter parameter= null;
	for (int index= 0; index < typeParameters.length; index++) {
		parameter= ast.newTypeParameter();
		parameter.setName(ast.newSimpleName(typeParameters[index].getName()));
		newDeclaration.typeParameters().add(parameter);
	}
	setSuperType(newDeclaration);

	IJavaProject project= fCu.getJavaProject();

	IVariableBinding[] bindings= getUsedLocalVariables();
	ArrayList<String> fieldNames= new ArrayList<String>();
	for (int i= 0; i < bindings.length; i++) {
		String name= StubUtility.getBaseName(bindings[i], project);
		String[] fieldNameProposals= StubUtility.getVariableNameSuggestions(NamingConventions.VK_INSTANCE_FIELD, project, name, 0, fieldNames, true);
		fieldNames.add(fieldNameProposals[0]);


		if (fLinkedProposalModel != null) {
			LinkedProposalPositionGroup positionGroup= fLinkedProposalModel.getPositionGroup(KEY_FIELD_NAME_EXT + i, true);
			for (int k= 0; k < fieldNameProposals.length; k++) {
				positionGroup.addProposal(fieldNameProposals[k], null, fieldNameProposals.length - k);
			}
		}
	}
	String[] allFieldNames= fieldNames.toArray(new String[fieldNames.size()]);

	List<BodyDeclaration> newBodyDeclarations= newDeclaration.bodyDeclarations();

	createFieldsForAccessedLocals(rewrite, bindings, allFieldNames, newBodyDeclarations);

	MethodDeclaration newConstructorDecl= createNewConstructor(rewrite, bindings, allFieldNames);
	if (newConstructorDecl != null) {
		newBodyDeclarations.add(newConstructorDecl);
	}

	updateAndMoveBodyDeclarations(rewrite, bindings, allFieldNames, newBodyDeclarations, newConstructorDecl);

	if (doAddComments()) {
		String[] parameterNames= new String[typeParameters.length];
		for (int index= 0; index < parameterNames.length; index++) {
			parameterNames[index]= typeParameters[index].getName();
		}
		String string= CodeGeneration.getTypeComment(rewrite.getCu(), fClassName, parameterNames, StubUtility.getLineDelimiterUsed(fCu));
		if (string != null) {
			Javadoc javadoc= (Javadoc) rewrite.getASTRewrite().createStringPlaceholder(string, ASTNode.JAVADOC);
			newDeclaration.setJavadoc(javadoc);
		}
	}
	if (fLinkedProposalModel != null) {
		addLinkedPosition(KEY_TYPE_NAME, newDeclaration.getName(), rewrite.getASTRewrite(), false);
		ModifierCorrectionSubProcessor.installLinkedVisibilityProposals(fLinkedProposalModel, rewrite.getASTRewrite(), newDeclaration.modifiers(), false);
	}

	return newDeclaration;
}
 
Example 8
Source File: CreateTypeMemberOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
protected ASTNode generateElementAST(ASTRewrite rewriter, ICompilationUnit cu) throws JavaModelException {
	if (this.createdNode == null) {
		this.source = removeIndentAndNewLines(this.source, cu);
		ASTParser parser = ASTParser.newParser(AST.JLS8);
		parser.setSource(this.source.toCharArray());
		parser.setProject(getCompilationUnit().getJavaProject());
		parser.setKind(ASTParser.K_CLASS_BODY_DECLARATIONS);
		ASTNode node = parser.createAST(this.progressMonitor);
		String createdNodeSource;
		if (node.getNodeType() != ASTNode.TYPE_DECLARATION) {
			createdNodeSource = generateSyntaxIncorrectAST();
			if (this.createdNode == null)
				throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_CONTENTS));
		} else {
			TypeDeclaration typeDeclaration = (TypeDeclaration) node;
			if ((typeDeclaration.getFlags() & ASTNode.MALFORMED) != 0) {
				createdNodeSource = generateSyntaxIncorrectAST();
				if (this.createdNode == null)
					throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_CONTENTS));
			} else {
				List bodyDeclarations = typeDeclaration.bodyDeclarations();
				if (bodyDeclarations.size() == 0) {
					throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_CONTENTS));
				}
				this.createdNode = (ASTNode) bodyDeclarations.iterator().next();
				createdNodeSource = this.source;
			}
		}
		if (this.alteredName != null) {
			SimpleName newName = this.createdNode.getAST().newSimpleName(this.alteredName);
			SimpleName oldName = rename(this.createdNode, newName);
			int nameStart = oldName.getStartPosition();
			int nameEnd = nameStart + oldName.getLength();
			StringBuffer newSource = new StringBuffer();
			if (this.source.equals(createdNodeSource)) {
				newSource.append(createdNodeSource.substring(0, nameStart));
				newSource.append(this.alteredName);
				newSource.append(createdNodeSource.substring(nameEnd));
			} else {
				// syntactically incorrect source
				int createdNodeStart = this.createdNode.getStartPosition();
				int createdNodeEnd = createdNodeStart + this.createdNode.getLength();
				newSource.append(createdNodeSource.substring(createdNodeStart, nameStart));
				newSource.append(this.alteredName);
				newSource.append(createdNodeSource.substring(nameEnd, createdNodeEnd));

			}
			this.source = newSource.toString();
		}
	}
	if (rewriter == null) return this.createdNode;
	// return a string place holder (instead of the created node) so has to not lose comments and formatting
	return rewriter.createStringPlaceholder(this.source, this.createdNode.getNodeType());
}