List of usage examples for org.eclipse.jdt.core.dom FieldDeclaration getStartPosition
public final int getStartPosition()
From source file:br.uff.ic.gems.resources.ast.Visitor.java
@Override public boolean visit(FieldDeclaration node) { List<VariableDeclarationFragment> fragments = node.fragments(); for (VariableDeclarationFragment fragment : fragments) { int beginLine = cu.getLineNumber(fragment.getStartPosition()); int endLine = cu.getLineNumber(fragment.getStartPosition() + fragment.getLength()); int beginColumn = beginColunm(node); int endColumn = cu.getColumnNumber(node.getStartPosition() + node.getLength()); SimpleName name = fragment.getName(); if (name != null) { endLine = cu.getLineNumber(name.getStartPosition() + name.getLength()); endColumn = cu.getColumnNumber(name.getStartPosition() + name.getLength()); }/* www . j av a2 s . c o m*/ languageConstructs.add(new LanguageConstruct(node.getClass().getSimpleName(), beginLine, endLine, beginColumn, endColumn, fragment.getName().getIdentifier())); } return true; }
From source file:gr.uop.intermittent.faults.intermittentfaultscodeparser.ParsingWithEclipseJDT.java
public static void parseForMethods(String str, final ClassStructure cs, final FileStructure fStr) { ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource(str.toCharArray()); parser.setKind(ASTParser.K_COMPILATION_UNIT); final CompilationUnit cu = (CompilationUnit) parser.createAST(null); cu.accept(new ASTVisitor() { public boolean visit(FieldDeclaration node) { String name = node.fragments().get(0).toString().split("=")[0]; String type = node.getType().toString(); FieldStructure fs = new FieldStructure(); fs.setFieldName(name);/* w w w. j ava2s . co m*/ fs.setFieldType(type); Info info = new Info(); info.setClassName(cs.getClassName()); info.setFile(cs.getParent().getFileName()); info.setLine(cu.getLineNumber(node.getStartPosition())); fStr.addInfoInLineDictionary(cu.getLineNumber(node.getStartPosition()), info); fs.addFieldInfo(info); cs.addFieldStructure(fs); System.out.println("Field declaration '" + name + "' with type " + type + " at line " + cu.getLineNumber(node.getStartPosition())); return true; } public boolean visit(MethodDeclaration node) { if (node.getName().getIdentifier() != null) { try { System.out.println("Declaration of method : " + node.getName() + " with paremeters " + Arrays.toString(node.parameters().toString().split(",")) + " at line " + cu.getLineNumber(node.getStartPosition()) + " is constructor " + node.isConstructor()); MethodStructure ms = new MethodStructure(); ms.setMethodName(node.getName().getIdentifier()); if (!node.isConstructor()) ms.setMethodReturnType(node.getReturnType2().toString()); Info info = new Info(); info.setClassName(cs.getClassName()); info.setFile(cs.getParent().getFileName()); firstMethodLine = cu.getLineNumber(node.getStartPosition()); int methodLines = ParsingWithEclipseJDT.countMethodLines(cs.getParent().getFilePath()); info.setLine(firstMethodLine); info.setStructureLength(methodLines); fStr.addInfoInLineDictionary(cu.getLineNumber(node.getStartPosition()), info); ms.addMethodInfo(info); Object[] params = node.parameters().toArray(); Set<String> parameterNames = new HashSet(); for (Object s : params) { String param = s.toString(); ParameterStructure ps = new ParameterStructure(); String[] parts = param.split(" "); ps.setParameterName(parts[parts.length - 1]); ps.setParameterType(parts[parts.length - 2]); Info info2 = new Info(); info2.setClassName(cs.getClassName()); info2.setFile(cs.getParent().getFileName()); info2.setLine(cu.getLineNumber(node.getStartPosition())); fStr.addInfoInLineDictionary(cu.getLineNumber(node.getStartPosition()), info2); ps.addParameterInfo(info2); ms.addParameterStructure(ps); parameterNames.add(ps.getParameterName()); } BlockStructure bs = new BlockStructure(); bs.setBlockNum(0); bs.setMethodParent(ms); Info info3 = new Info(); info3.setClassName(cs.getClassName()); info3.setFile(cs.getParent().getFileName()); info3.setLine(cu.getLineNumber(node.getStartPosition())); bs.addBlockInfo(info3); bs.setVariables(new ArrayList<VariableStructure>()); ms.addBlockStructure(bs); cs.addMethodStructure(ms); } catch (IOException ex) { Logger.getLogger(ParsingWithEclipseJDT.class.getName()).log(Level.SEVERE, null, ex); } } return true; } }); }
From source file:org.cruxframework.cruxdevtools.loginstrumentation.LogInstrumentation.java
License:Apache License
private static void parse(String str) { ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource(str.toCharArray()); parser.setKind(ASTParser.K_COMPILATION_UNIT); final CompilationUnit cu = (CompilationUnit) parser.createAST(null); cu.accept(new ASTVisitor() { private boolean logInserted = false; private String fileName; @Override/*from w w w . j a v a 2s.com*/ public boolean visit(FieldDeclaration node) { if (!logInserted) { logInserted = true; int lineNumber = cu.getLineNumber(node.getStartPosition()); int charNumber = node.getStartPosition(); mapVariableLocation.put(fileName, new VariableLocation(lineNumber - 1, charNumber, node.toString(), fileName.substring(0, fileName.length() - ".java".length()))); } return super.visit(node); } @Override public boolean visit(TypeDeclaration node) { fileName = node.getName().getFullyQualifiedName() + ".java"; return super.visit(node); } @Override public boolean visit(MethodDeclaration node) { List<MethodLocation> methodLocations = mapMethodLocation.get(fileName); if (methodLocations == null) { methodLocations = new ArrayList<LogInstrumentation.MethodLocation>(); } if (node != null && node.getBody() != null) { int indexOfSuper = node.getBody().toString().indexOf("super("); if (indexOfSuper < 0 || indexOfSuper > 5) { int lineNumber = cu.getLineNumber(node.getBody().getStartPosition()); int charNumber = node.getStartPosition(); methodLocations.add( new MethodLocation(lineNumber, charNumber, node.getName().getFullyQualifiedName())); mapMethodLocation.put(fileName, methodLocations); } } return super.visit(node); } }); }
From source file:org.eclipse.swt.tools.internal.ASTField.java
License:Open Source License
public ASTField(ASTClass declaringClass, String source, FieldDeclaration field, VariableDeclarationFragment fragment) { this.declaringClass = declaringClass; name = fragment.getName().getIdentifier(); modifiers = field.getModifiers();//from w w w .j a va 2 s . c o m start = field.getStartPosition(); Javadoc doc = field.getJavadoc(); List<TagElement> tags = null; if (doc != null) { tags = doc.tags(); for (Iterator<TagElement> iterator = tags.iterator(); iterator.hasNext();) { TagElement tag = iterator.next(); if ("@field".equals(tag.getTagName())) { String data = tag.fragments().get(0).toString(); setMetaData(data); break; } } } type = new ASTType(declaringClass.resolver, field.getType(), fragment.getExtraDimensions()); type64 = this.type; if (GEN64) { String s = source.substring(field.getStartPosition(), field.getStartPosition() + field.getLength()); if (type.isType("int") && s.indexOf("int /*long*/") != -1) type64 = new ASTType("J"); else if (type.isType("float") && s.indexOf("float /*double*/") != -1) type64 = new ASTType("D"); else if (type.isType("[I") && (s.indexOf("int /*long*/") != -1 || s.indexOf("int[] /*long[]*/") != -1)) type64 = new ASTType("[J"); else if (type.isType("[F") && (s.indexOf("float /*double*/") != -1 || s.indexOf("float[] /*double[]*/") != -1)) type64 = new ASTType("[D"); else if (type.isType("long") && s.indexOf("long /*int*/") != -1) type = new ASTType("I"); else if (type.isType("double") && s.indexOf("double /*float*/") != -1) type = new ASTType("F"); else if (type.isType("[J") && (s.indexOf("long /*int*/") != -1 || s.indexOf("long[] /*int[]*/") != -1)) type = new ASTType("[I"); else if (type.isType("[D") && (s.indexOf("double /*float*/") != -1 || s.indexOf("double[] /*float[]*/") != -1)) type = new ASTType("[F"); } }
From source file:org.eclipse.swt.tools.internal.ReflectField.java
License:Open Source License
public ReflectField(ReflectClass declaringClass, Field field, String source, CompilationUnit unit) { this.declaringClass = declaringClass; this.field = field; Class<?> clazz = field.getType(); type = new ReflectType(clazz); type64 = type;//from w w w. ja va 2 s . c o m boolean changes = canChange64(clazz); if (changes && new File(declaringClass.sourcePath).exists()) { TypeDeclaration type1 = (TypeDeclaration) unit.types().get(0); Class<?> result = null; FieldDeclaration[] fields = type1.getFields(); for (int i = 0; i < fields.length && result == null; i++) { FieldDeclaration node = fields[i]; for (Iterator<?> iterator = node.fragments().iterator(); iterator.hasNext();) { VariableDeclarationFragment decl = (VariableDeclarationFragment) iterator.next(); if (decl.getName().getIdentifier().equals(field.getName())) { String s = source.substring(node.getStartPosition(), node.getStartPosition() + node.getLength()); if (clazz == int.class && s.indexOf("int /*long*/") != -1) type64 = new ReflectType(long.class); else if (clazz == float.class && s.indexOf("float /*double*/") != -1) type64 = new ReflectType(double.class); else if (clazz == int[].class && (s.indexOf("int /*long*/") != -1 || s.indexOf("int[] /*long[]*/") != -1)) type64 = new ReflectType(long[].class); else if (clazz == float[].class && (s.indexOf("float /*double*/") != -1 || s.indexOf("float[] /*double[]*/") != -1)) type = new ReflectType(double[].class); else if (clazz == long.class && s.indexOf("long /*int*/") != -1) type = new ReflectType(int.class); else if (clazz == double.class && s.indexOf("double /*float*/") != -1) type = new ReflectType(float.class); else if (clazz == long[].class && (s.indexOf("long /*int*/") != -1 || s.indexOf("long[] /*int[]*/") != -1)) type = new ReflectType(int[].class); else if (clazz == double[].class && (s.indexOf("double /*float*/") != -1 || s.indexOf("double[] /*float[]*/") != -1)) type = new ReflectType(float[].class); break; } } } } }
From source file:org.eclipse.wb.internal.core.model.variable.EmptyVariableSupport.java
License:Open Source License
/** * Converts this "empty" variable into {@link LocalUniqueVariableSupport}. * <p>//ww w .ja v a2 s . c o m * Case when initializer {@link Expression} is part of other {@link FieldDeclaration}. */ private void materialize_newField() throws Exception { AstEditor editor = m_javaInfo.getEditor(); FieldDeclaration oldField = getEnclosingField(); // prepare type ITypeBinding typeBinding; String typeName; { typeBinding = AstNodeUtils.getTypeBinding(m_initializer); typeName = editor.getTypeBindingSource(typeBinding); } // initialize position and source int position = oldField.getStartPosition(); String source = ""; // modifier { source += "private "; } // add type Type newType; { newType = editor.getParser().parseQualifiedType(position + source.length(), typeName); source += typeName + " "; } // add variable String variableName; SimpleName newDeclarationVariable; { variableName = editor.getUniqueVariableName(m_initializer.getStartPosition(), NamesManager.getName(m_javaInfo), null); newDeclarationVariable = editor.getParser().parseVariable(position + source.length(), variableName, null, typeBinding, false, Modifier.NONE); source += variableName + " = "; } // move initializer { String initializerSource = editor.getSource(m_initializer); int originalInitializerPosition = m_initializer.getStartPosition(); int originalInitializerLength = m_initializer.getLength(); // replace initializer with variable { SimpleName newUseVariable = editor.getParser().parseVariable(originalInitializerPosition, variableName, null, typeBinding, false, Modifier.NONE); // AstEditor.replaceNode(m_initializer, newUseVariable); m_javaInfo.addRelatedNode(newUseVariable); editor.replaceSubstring(originalInitializerPosition, originalInitializerLength, newUseVariable.getIdentifier()); AstNodeUtils.moveNode(newUseVariable, originalInitializerPosition); editor.inlineParenthesizedExpression(newUseVariable); } // use initializer for declaration AstNodeUtils.moveNode(m_initializer, position + source.length()); source += initializerSource; } // add fragment VariableDeclarationFragment newFragment; { newFragment = m_initializer.getAST().newVariableDeclarationFragment(); newFragment.setName(newDeclarationVariable); newFragment.setInitializer(m_initializer); AstNodeUtils.setSourceRange(newFragment, newDeclarationVariable, m_initializer); } // add statement FieldDeclaration newField; { newField = m_initializer.getAST().newFieldDeclaration(newFragment); newField.setType(newType); AstNodeUtils.setSourceRange(newField, newType, newFragment, 1); source += ";"; } // add EOL and prefix source += editor.getGeneration().getEndOfLine() + editor.getWhitespaceToLeft(position, false); // update source editor.replaceSubstring(position, 0, source); // add new statement to AST { TypeDeclaration typeDeclaration = (TypeDeclaration) oldField.getParent(); List<BodyDeclaration> bodyDeclarations = DomGenerics.bodyDeclarations(typeDeclaration); int index = bodyDeclarations.indexOf(oldField); bodyDeclarations.add(index, newField); editor.resolveImports(newField); } // use local variable support m_javaInfo.setVariableSupport(new FieldInitializerVariableSupport(m_javaInfo, newDeclarationVariable)); }
From source file:org.eclipse.wb.tests.designer.core.util.ast.AstEditorTest.java
License:Open Source License
public void test_getWhitespaceToLeft() throws Exception { TypeDeclaration typeDeclaration;//from w ww. j ava 2 s . co m { String code = "\t\t\r\t\t\n\t\tprivate int m_value = 12345;"; typeDeclaration = createTypeDeclaration_TestC(code); } // prepare field and its position FieldDeclaration fieldDeclaration = typeDeclaration.getFields()[0]; int end = fieldDeclaration.getStartPosition(); // do checks assertEquals("", m_lastEditor.getWhitespaceToLeft(end + 1, false)); assertEquals("\t\t", m_lastEditor.getWhitespaceToLeft(end, false)); assertEquals("\t\t\r\t\t\n\t\t", m_lastEditor.getWhitespaceToLeft(end, true)); }
From source file:org.eclipse.wb.tests.designer.core.util.ast.AstEditorTest.java
License:Open Source License
public void test_getLineNumber() throws Exception { TypeDeclaration typeDeclaration = createTypeDeclaration_Test("// filler filler filler", "// filler filler filler", "class Test {", " int a;", "}"); FieldDeclaration fieldDeclaration = typeDeclaration.getFields()[0]; assertEquals(4, m_lastEditor.getLineNumber(fieldDeclaration.getStartPosition())); }
From source file:org.eclipse.wb.tests.designer.core.util.ast.AstEditorTest.java
License:Open Source License
public void test_skipSingleEOLToLeft_1() throws Exception { TypeDeclaration typeDeclaration = createTypeDeclaration_Test("// filler filler filler", "// filler filler filler", "class Test {", "\r\n\r\nint b;", "}"); FieldDeclaration fieldDeclaration = typeDeclaration.getFields()[0]; int index = fieldDeclaration.getStartPosition(); // no EOL to left assertEquals(index + 1, m_lastEditor.skipSingleEOLToLeft(index + 1)); // skip only single EOL, but this is \r\n, so 2 character assertEquals(index - 2, m_lastEditor.skipSingleEOLToLeft(index)); }
From source file:org.eclipse.wb.tests.designer.core.util.ast.AstEditorTest.java
License:Open Source License
public void test_skipSingleEOLToLeft_2() throws Exception { TypeDeclaration typeDeclaration = createTypeDeclaration_Test("// filler filler filler", "// filler filler filler", "class Test {", "\n\nint b;", "}"); FieldDeclaration fieldDeclaration = typeDeclaration.getFields()[0]; int index = fieldDeclaration.getStartPosition(); // skip only single EOL - \n, so 1 character assertEquals(index - 1, m_lastEditor.skipSingleEOLToLeft(index)); }