use of org.eclipse.jdt.core.dom.FieldDeclaration in project eclipse-pmd by acanda.
the class SingularFieldQuickFix method updateFieldDeclaration.
/**
* Updates the field declaration. If the replaced field was the only fragment, the entire field declaration is
* removed. Otherwise the field declaration stays and only the respective fragment is removed.
*/
private void updateFieldDeclaration(final VariableDeclarationFragment node) {
final FieldDeclaration fieldDeclaration = (FieldDeclaration) node.getParent();
@SuppressWarnings("unchecked") final List<VariableDeclarationFragment> fragments = fieldDeclaration.fragments();
if (fragments.size() > 1) {
for (final VariableDeclarationFragment fragment : fragments) {
if (fragment.getName().getIdentifier().equals(node.getName().getIdentifier())) {
fragment.delete();
}
}
} else {
fieldDeclaration.delete();
}
}
use of org.eclipse.jdt.core.dom.FieldDeclaration in project AutoRefactor by JnRouvignac.
the class ASTNodeFactory method newFieldDeclaration.
/**
* Builds a new {@link FieldDeclaration} instance.
*
* @param type the declared variable type
* @param fragment the variable declaration fragment
* @return a new field declaration
*/
public FieldDeclaration newFieldDeclaration(final Type type, final VariableDeclarationFragment fragment) {
FieldDeclaration fd = ast.newFieldDeclaration(fragment);
fd.setType(type);
return fd;
}
use of org.eclipse.jdt.core.dom.FieldDeclaration in project xtext-xtend by eclipse.
the class JavaASTFlattener method visit.
@Override
public boolean visit(final Initializer it) {
Javadoc _javadoc = it.getJavadoc();
boolean _tripleNotEquals = (_javadoc != null);
if (_tripleNotEquals) {
it.getJavadoc().accept(this);
}
this.appendModifiers(it, it.modifiers());
boolean _isStatic = this._aSTFlattenerUtils.isStatic(it.modifiers());
if (_isStatic) {
if (((it.getParent() instanceof TypeDeclaration) && IterableExtensions.<FieldDeclaration>forall(IterableExtensions.<FieldDeclaration>filter(((Iterable<FieldDeclaration>) Conversions.doWrapArray(((TypeDeclaration) it.getParent()).getFields())), ((Function1<FieldDeclaration, Boolean>) (FieldDeclaration it_1) -> {
return Boolean.valueOf((this._aSTFlattenerUtils.isStatic(it_1.modifiers()) && this._aSTFlattenerUtils.isFinal(it_1.modifiers())));
})), ((Function1<FieldDeclaration, Boolean>) (FieldDeclaration f) -> {
final Function1<VariableDeclarationFragment, Boolean> _function = (VariableDeclarationFragment fragment) -> {
Boolean _isAssignedInBody = this._aSTFlattenerUtils.isAssignedInBody(it.getBody(), fragment);
return Boolean.valueOf((!(_isAssignedInBody).booleanValue()));
};
return Boolean.valueOf(IterableExtensions.<VariableDeclarationFragment>forall(f.fragments(), _function));
})))) {
this.appendToBuffer(" final Void static_initializer = {");
this.appendLineWrapToBuffer();
it.getBody().accept(this);
this.appendToBuffer("null }");
this.appendLineWrapToBuffer();
} else {
this.addProblem(it, "Static initializer is not fully supported");
this.appendToBuffer("{/*FIXME ");
it.getBody().accept(this);
this.appendToBuffer("*/}");
}
} else {
ASTNode _parent = it.getParent();
if ((_parent instanceof AnonymousClassDeclaration)) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("Initializer is not supported in ");
String _simpleName = ASTNode.nodeClassForType(it.getParent().getNodeType()).getSimpleName();
_builder.append(_simpleName);
this.addProblem(it, _builder.toString());
}
it.getBody().accept(this);
}
return false;
}
use of org.eclipse.jdt.core.dom.FieldDeclaration in project xtext-xtend by eclipse.
the class ASTFlattenerUtils method findDeclaredType.
private Type findDeclaredType(final ASTNode scope, final SimpleName simpleName) {
final ArrayList<Type> matchesFound = CollectionLiterals.<Type>newArrayList();
scope.accept(new ASTVisitor() {
@Override
public boolean visit(final VariableDeclarationFragment node) {
boolean _equals = node.getName().getIdentifier().equals(simpleName.getIdentifier());
if (_equals) {
final ASTNode parentNode = node.getParent();
boolean _matched = false;
if (parentNode instanceof VariableDeclarationStatement) {
_matched = true;
matchesFound.add(((VariableDeclarationStatement) parentNode).getType());
}
if (!_matched) {
if (parentNode instanceof FieldDeclaration) {
_matched = true;
matchesFound.add(((FieldDeclaration) parentNode).getType());
}
}
if (!_matched) {
if (parentNode instanceof VariableDeclarationExpression) {
_matched = true;
matchesFound.add(((VariableDeclarationExpression) parentNode).getType());
}
}
}
return false;
}
@Override
public boolean preVisit2(final ASTNode node) {
return matchesFound.isEmpty();
}
@Override
public boolean visit(final SingleVariableDeclaration node) {
boolean _equals = node.getName().getIdentifier().equals(simpleName.getIdentifier());
if (_equals) {
matchesFound.add(node.getType());
}
return false;
}
});
return IterableExtensions.<Type>head(matchesFound);
}
use of org.eclipse.jdt.core.dom.FieldDeclaration in project eap-additional-testsuite by jboss-set.
the class MethodInfo method parse.
public static void parse(String str) throws IOException {
types.clear();
fields.clear();
methods.clear();
classInstanceCreations.clear();
typesNotResolved.clear();
methodsNotResolved.clear();
methodInvocations.clear();
imports.clear();
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(readFileToString(str).toCharArray());
parser.setKind(ASTParser.K_COMPILATION_UNIT);
final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
HashMap<String, String> declarations = new HashMap();
cu.accept(new ASTVisitor() {
Set names = new HashSet();
int blockCount = 0;
public boolean visit(FieldDeclaration node) {
String name = node.fragments().get(0).toString().split("=")[0].trim();
String type = node.getType().toString();
declarations.put(name, type);
if (!node.modifiers().toString().contains("private")) {
fields.put(name, type);
}
ArrayList<String> types0 = new ArrayList<>();
String type2 = null;
do {
if (type.contains("[")) {
type = type.replaceAll("\\[\\]", "");
if (type.contains("[")) {
type = type.substring(0, type.indexOf("["));
}
}
if (type.contains("<")) {
String type3 = type;
type = type.substring(0, type.indexOf("<"));
if (type3.substring(type3.indexOf("<") + 1).startsWith("<>") || type3.substring(type.indexOf("<") + 1).startsWith("<T>")) {
type2 = null;
} else {
type2 = type3.substring(type3.indexOf("<") + 1, type3.lastIndexOf(">"));
if (type2.contains(",")) {
if (type2.substring(0, type2.indexOf(",")).contains("<")) {
types0.add(type2);
} else {
types0.add(type2.substring(0, type2.indexOf(",")));
types0.add(type2.substring(type2.indexOf(",") + 1));
}
} else {
types0.add(type2);
}
}
}
types.addAll(Arrays.asList(type.split(" extends ")));
if (types0.size() != 0) {
type = types0.remove(0);
} else {
type = null;
}
} while (type != null);
return false;
}
public boolean visit(EnumConstantDeclaration node) {
String type = node.getName().toString();
// System.out.println("ccccc : " + type);
List<String> constants = node.arguments();
for (String f : constants) {
// System.out.println("ccccc : " + type + "." + f);
declarations.put(type + "." + f, type);
if (!node.modifiers().toString().contains("private")) {
fields.put(type + "." + f, type);
}
}
return false;
}
public boolean visit(MethodDeclaration node) {
if (node.getName().getIdentifier() != null) {
String returnType = null;
if (node.getReturnType2() == null) {
returnType = "";
} else {
returnType = node.getReturnType2().toString();
}
HashMap<String, String> bDeclarations = new HashMap();
bDeclarations.putAll(importedClassFields);
bDeclarations.putAll(declarations);
methods.put(node.getName().toString() + "_Return_Type", new String[] { returnType });
String[] methodParams = new String[node.parameters().size()];
List params = node.parameters();
int i = 0;
for (Object s : params) {
String type = ((SingleVariableDeclaration) s).getType().toString();
bDeclarations.put(((SingleVariableDeclaration) s).getName().toString(), type);
ArrayList<String> types0 = new ArrayList<>();
String type2 = null;
String typeF = null;
do {
if (type.contains("[")) {
type = type.replaceAll("\\[\\]", "");
if (type.contains("[")) {
type = type.substring(0, type.indexOf("["));
}
} else if (type.contains("<")) {
String type3 = type;
type = type.substring(0, type.indexOf("<"));
if (type3.substring(type3.indexOf("<") + 1).startsWith("<>") || type3.substring(type.indexOf("<") + 1).startsWith("<T>")) {
type2 = null;
} else {
type2 = type3.substring(type3.indexOf("<") + 1, type3.lastIndexOf(">"));
if (type2.contains(",")) {
if (type2.substring(0, type2.indexOf(",")).contains("<")) {
types0.add(type2);
} else {
types0.add(type2.substring(0, type2.indexOf(",")));
types0.add(type2.substring(type2.indexOf(",") + 1));
}
} else {
types0.add(type2);
}
}
}
types.addAll(Arrays.asList(type.split(" extends ")));
if (types0.size() != 0) {
type = types0.remove(0);
typeF = type;
} else {
type = null;
}
} while (type != null);
methodParams[i++] = typeF;
}
methods.put(node.getName().toString() + "_Return_Type", methodParams);
Block block = node.getBody();
blockIterate(block, cu, bDeclarations);
}
return false;
}
public boolean visit(ImportDeclaration node) {
imports.add(node.getName().toString());
if (DependencyTreeMethods.jarClassPaths.containsKey(node.getName().toString())) {
importedClassFields = DependencyTreeMethods.listFieldsOfJarClass(DependencyTreeMethods.jarClassPaths.get(node.getName().toString()), node.getName().toString());
fields.putAll(importedClassFields);
}
return false;
}
public boolean visit(PackageDeclaration node) {
packageName = node.getName().toString();
return true;
}
});
}
Aggregations