use of org.eclipse.jdt.core.dom.FieldDeclaration in project eclipse.jdt.ls by eclipse.
the class MoveHandler method moveStaticMember.
private static RefactorWorkspaceEdit moveStaticMember(CodeActionParams params, String destinationTypeName, IProgressMonitor monitor) {
final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(params.getTextDocument().getUri());
if (unit == null) {
return new RefactorWorkspaceEdit("Failed to move static member because cannot find the compilation unit associated with " + params.getTextDocument().getUri());
}
BodyDeclaration bodyDeclaration = getSelectedMemberDeclaration(unit, params);
List<IJavaElement> elements = new ArrayList<>();
if (bodyDeclaration instanceof MethodDeclaration) {
elements.add(((MethodDeclaration) bodyDeclaration).resolveBinding().getJavaElement());
} else if (bodyDeclaration instanceof FieldDeclaration) {
for (Object fragment : ((FieldDeclaration) bodyDeclaration).fragments()) {
elements.add(((VariableDeclarationFragment) fragment).resolveBinding().getJavaElement());
}
} else if (bodyDeclaration instanceof AbstractTypeDeclaration) {
elements.add(((AbstractTypeDeclaration) bodyDeclaration).resolveBinding().getJavaElement());
}
IMember[] members = elements.stream().filter(element -> element instanceof IMember).map(element -> (IMember) element).toArray(IMember[]::new);
return moveStaticMember(members, destinationTypeName, monitor);
}
use of org.eclipse.jdt.core.dom.FieldDeclaration in project eclipse.jdt.ls by eclipse.
the class MoveHandler method getSelectedMemberDeclaration.
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;
}
use of org.eclipse.jdt.core.dom.FieldDeclaration in project eclipse.jdt.ls by eclipse.
the class RefactorProposalUtility method getDisplayName.
private static String getDisplayName(ASTNode declaration) {
if (declaration instanceof MethodDeclaration) {
IMethodBinding method = ((MethodDeclaration) declaration).resolveBinding();
if (method != null) {
String name = method.getName();
String[] parameters = Stream.of(method.getParameterTypes()).map(type -> type.getName()).toArray(String[]::new);
return name + "(" + String.join(",", parameters) + ")";
}
} else if (declaration instanceof FieldDeclaration) {
List<String> fieldNames = new ArrayList<>();
for (Object fragment : ((FieldDeclaration) declaration).fragments()) {
IVariableBinding variable = ((VariableDeclarationFragment) fragment).resolveBinding();
if (variable != null) {
fieldNames.add(variable.getName());
}
}
return String.join(",", fieldNames);
} else if (declaration instanceof AbstractTypeDeclaration) {
ITypeBinding type = ((AbstractTypeDeclaration) declaration).resolveBinding();
if (type != null) {
return type.getName();
}
}
return null;
}
use of org.eclipse.jdt.core.dom.FieldDeclaration in project eap-additional-testsuite by jboss-set.
the class SourceParser method parse.
public static void parse(String str) throws IOException {
fields.clear();
methods.clear();
imports.clear();
types.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);
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();
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 if (type3.indexOf("<") >= 0 && type3.indexOf(">") >= 0 && type3.indexOf("<") < type3.indexOf(">")) {
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 true;
}
public boolean visit(MethodDeclaration node) {
if (node.getName().getIdentifier() != null) {
MethodInfo2 mf = new MethodInfo2();
mf.name = node.getName().toString();
if (node.getReturnType2() != null) {
mf.returnType = node.getReturnType2().toString();
} else {
mf.returnType = null;
}
List params = node.parameters();
ArrayList<String> types = new ArrayList<>();
// System.out.println("params : " + params.toString());
for (Object s : params) {
String type = ((SingleVariableDeclaration) s).getType().toString();
if (type.startsWith("class "))
type = type.replaceFirst("class ", "");
types.add(type);
}
// System.out.println("sourceTypes : " + types.toString());
mf.paramTypes = types;
methods.put(mf.name, mf);
}
return true;
}
public boolean visit(ImportDeclaration node) {
imports.add(node.getName().toString());
return true;
}
public boolean visit(PackageDeclaration node) {
packageName = node.getName().toString();
return true;
}
});
}
use of org.eclipse.jdt.core.dom.FieldDeclaration in project che by eclipse.
the class VariableDeclarationFix method createAddFinalOperation.
private static ModifierChangeOperation createAddFinalOperation(SimpleName name, ASTNode decl) {
if (decl == null)
return null;
IBinding binding = name.resolveBinding();
if (!canAddFinal(binding, decl))
return null;
if (decl instanceof SingleVariableDeclaration) {
return new ModifierChangeOperation(decl, new ArrayList<VariableDeclarationFragment>(), Modifier.FINAL, Modifier.NONE);
} else if (decl instanceof VariableDeclarationExpression) {
return new ModifierChangeOperation(decl, new ArrayList<VariableDeclarationFragment>(), Modifier.FINAL, Modifier.NONE);
} else if (decl instanceof VariableDeclarationFragment) {
VariableDeclarationFragment frag = (VariableDeclarationFragment) decl;
decl = decl.getParent();
if (decl instanceof FieldDeclaration || decl instanceof VariableDeclarationStatement) {
List<VariableDeclarationFragment> list = new ArrayList<VariableDeclarationFragment>();
list.add(frag);
return new ModifierChangeOperation(decl, list, Modifier.FINAL, Modifier.NONE);
} else if (decl instanceof VariableDeclarationExpression) {
return new ModifierChangeOperation(decl, new ArrayList<VariableDeclarationFragment>(), Modifier.FINAL, Modifier.NONE);
}
}
return null;
}
Aggregations