Java Code Examples for org.eclipse.jdt.core.IField#getElementName()

The following examples show how to use org.eclipse.jdt.core.IField#getElementName() . 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: DeltaConverter.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * We don't include nested types because structural changes of nested types should not affect Xtend classes which
 * use top level types.
 * 
 * @deprecated This method is not used anymore.
 */
@Deprecated
protected void traverseType(IType type, NameBasedEObjectDescriptionBuilder acceptor) {
	try {
		if (type.exists()) {
			for (IField field : type.getFields()) {
				if (!Flags.isSynthetic(field.getFlags())) {
					String fieldName = field.getElementName();
					acceptor.accept(fieldName);
				}
			}
			for (IMethod method : type.getMethods()) {
				if (!Flags.isSynthetic(method.getFlags())) {
					String methodName = method.getElementName();
					acceptor.accept(methodName);
				}
			}

		}
	} catch (JavaModelException e) {
		if (LOGGER.isDebugEnabled())
			LOGGER.debug(e, e);
	}
}
 
Example 2
Source File: NLSSearchQuery.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private boolean isNLSField(IField field) throws JavaModelException {
	int flags= field.getFlags();
	if (!Flags.isPublic(flags))
		return false;

	if (!Flags.isStatic(flags))
		return false;

	String fieldName= field.getElementName();
	if (NLSRefactoring.BUNDLE_NAME_FIELD.equals(fieldName))
		return false;

	if ("RESOURCE_BUNDLE".equals(fieldName)) //$NON-NLS-1$
		return false;

	return true;
}
 
Example 3
Source File: MethodContentGenerations.java    From jenerate with Eclipse Public License 1.0 5 votes vote down vote up
private static String generateGetter(final IField field) throws JavaModelException {
    String elementName = field.getElementName();
    if (isFieldABoolean(field)) {
        return "is" + upperCaseFirst(elementName + "()");
    }
    return "get" + upperCaseFirst(elementName + "()");

}
 
Example 4
Source File: JDTMethodHelper.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
private static IMethod createSetterMethod(final IType type, IField field) {
    try {
        return new GroovyFieldAccessorMethod(type, GetterSetterUtil.getSetterName(field, null), "V",
                new String[] { field.getElementName() }, new String[] { field.getTypeSignature() });
    } catch (JavaModelException e) {
        BonitaStudioLog.error(e);
        return null;
    }
}
 
Example 5
Source File: ExtractClassRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private FieldInfo(ParameterInfo parameterInfo, IField ifield) {
	super();
	this.pi= parameterInfo;
	this.ifield= ifield;
	this.name= ifield.getElementName();
}
 
Example 6
Source File: NLSSearchQuery.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public IStatus run(IProgressMonitor monitor) {
	monitor.beginTask("", 5 * fWrapperClass.length); //$NON-NLS-1$

	try {
		final AbstractTextSearchResult textResult= (AbstractTextSearchResult) getSearchResult();
		textResult.removeAll();

		for (int i= 0; i < fWrapperClass.length; i++) {
			IJavaElement wrapperClass= fWrapperClass[i];
			IFile propertieFile= fPropertiesFile[i];
			if (! wrapperClass.exists())
				return JavaUIStatus.createError(0, Messages.format(NLSSearchMessages.NLSSearchQuery_wrapperNotExists, JavaElementLabels.getElementLabel(wrapperClass, JavaElementLabels.ALL_DEFAULT)), null);
			if (! propertieFile.exists())
				return JavaUIStatus.createError(0, Messages.format(NLSSearchMessages.NLSSearchQuery_propertiesNotExists, BasicElementLabels.getResourceName(propertieFile)), null);

			SearchPattern pattern= SearchPattern.createPattern(wrapperClass, IJavaSearchConstants.REFERENCES, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
			SearchParticipant[] participants= new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()};

			NLSSearchResultRequestor requestor= new NLSSearchResultRequestor(propertieFile, fResult);
			try {
				SearchEngine engine= new SearchEngine();
				engine.search(pattern, participants, fScope, requestor, new SubProgressMonitor(monitor, 4));
				requestor.reportUnusedPropertyNames(new SubProgressMonitor(monitor, 1));

				ICompilationUnit compilationUnit= ((IType)wrapperClass).getCompilationUnit();
				CompilationUnitEntry groupElement= new CompilationUnitEntry(NLSSearchMessages.NLSSearchResultCollector_unusedKeys, compilationUnit);

				boolean hasUnusedPropertie= false;
				IField[] fields= ((IType)wrapperClass).getFields();
				for (int j= 0; j < fields.length; j++) {
					IField field= fields[j];
					if (isNLSField(field)) {
						ISourceRange sourceRange= field.getSourceRange();
						if (sourceRange != null) {
							String fieldName= field.getElementName();
							if (!requestor.hasPropertyKey(fieldName)) {
								fResult.addMatch(new Match(compilationUnit, sourceRange.getOffset(), sourceRange.getLength()));
							}
							if (!requestor.isUsedPropertyKey(fieldName)) {
								hasUnusedPropertie= true;
								fResult.addMatch(new Match(groupElement, sourceRange.getOffset(), sourceRange.getLength()));
							}
						}
					}
				}
				if (hasUnusedPropertie)
					fResult.addCompilationUnitGroup(groupElement);

			} catch (CoreException e) {
				return new Status(e.getStatus().getSeverity(), JavaPlugin.getPluginId(), IStatus.OK, NLSSearchMessages.NLSSearchQuery_error, e);
			}
		}
	} finally {
		monitor.done();
	}
	return 	Status.OK_STATUS;
}
 
Example 7
Source File: AddJavaDocStubOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private String createFieldComment(IField field, String lineDelimiter) throws JavaModelException, CoreException {
	String typeName= Signature.toString(field.getTypeSignature());
	String fieldName= field.getElementName();
	return CodeGeneration.getFieldComment(field.getCompilationUnit(), typeName, fieldName, lineDelimiter);
}
 
Example 8
Source File: GetterSetterUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Create a stub for a getter of the given field using getter/setter templates. The resulting code
 * has to be formatted and indented.
 * @param field The field to create a getter for
 * @param setterName The chosen name for the setter
 * @param addComments If <code>true</code>, comments will be added.
 * @param flags The flags signaling visibility, if static, synchronized or final
 * @return Returns the generated stub.
 * @throws CoreException when stub creation failed
 */
public static String getSetterStub(IField field, String setterName, boolean addComments, int flags) throws CoreException {

	String fieldName= field.getElementName();
	IType parentType= field.getDeclaringType();

	String returnSig= field.getTypeSignature();
	String typeName= Signature.toString(returnSig);

	IJavaProject project= field.getJavaProject();

	String accessorName= StubUtility.getBaseName(field);
	String argname= StubUtility.suggestArgumentName(project, accessorName, EMPTY);

	boolean isStatic= Flags.isStatic(flags);
	boolean isSync= Flags.isSynchronized(flags);
	boolean isFinal= Flags.isFinal(flags);

	String lineDelim= "\n"; // Use default line delimiter, as generated stub has to be formatted anyway //$NON-NLS-1$
	StringBuffer buf= new StringBuffer();
	if (addComments) {
		String comment= CodeGeneration.getSetterComment(field.getCompilationUnit(), parentType.getTypeQualifiedName('.'), setterName, field.getElementName(), typeName, argname, accessorName, lineDelim);
		if (comment != null) {
			buf.append(comment);
			buf.append(lineDelim);
		}
	}
	buf.append(JdtFlags.getVisibilityString(flags));
	buf.append(' ');
	if (isStatic)
		buf.append("static "); //$NON-NLS-1$
	if (isSync)
		buf.append("synchronized "); //$NON-NLS-1$
	if (isFinal)
		buf.append("final "); //$NON-NLS-1$

	buf.append("void "); //$NON-NLS-1$
	buf.append(setterName);
	buf.append('(');
	buf.append(typeName);
	buf.append(' ');
	buf.append(argname);
	buf.append(") {"); //$NON-NLS-1$
	buf.append(lineDelim);

	boolean useThis= StubUtility.useThisForFieldAccess(project);
	if (argname.equals(fieldName) || (useThis && !isStatic)) {
		if (isStatic)
			fieldName= parentType.getElementName() + '.' + fieldName;
		else
			fieldName= "this." + fieldName; //$NON-NLS-1$
	}
	String body= CodeGeneration.getSetterMethodBodyContent(field.getCompilationUnit(), parentType.getTypeQualifiedName('.'), setterName, fieldName, argname, lineDelim);
	if (body != null) {
		buf.append(body);
	}
	buf.append("}"); //$NON-NLS-1$
	buf.append(lineDelim);
	return buf.toString();
}
 
Example 9
Source File: GetterSetterUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Create a stub for a getter of the given field using getter/setter templates. The resulting code
 * has to be formatted and indented.
 * @param field The field to create a getter for
 * @param getterName The chosen name for the getter
 * @param addComments If <code>true</code>, comments will be added.
 * @param flags The flags signaling visibility, if static, synchronized or final
 * @return Returns the generated stub.
 * @throws CoreException when stub creation failed
 */
public static String getGetterStub(IField field, String getterName, boolean addComments, int flags) throws CoreException {
	String fieldName= field.getElementName();
	IType parentType= field.getDeclaringType();

	boolean isStatic= Flags.isStatic(flags);
	boolean isSync= Flags.isSynchronized(flags);
	boolean isFinal= Flags.isFinal(flags);

	String typeName= Signature.toString(field.getTypeSignature());
	String accessorName= StubUtility.getBaseName(field);

	String lineDelim= "\n"; // Use default line delimiter, as generated stub has to be formatted anyway //$NON-NLS-1$
	StringBuffer buf= new StringBuffer();
	if (addComments) {
		String comment= CodeGeneration.getGetterComment(field.getCompilationUnit(), parentType.getTypeQualifiedName('.'), getterName, field.getElementName(), typeName, accessorName, lineDelim);
		if (comment != null) {
			buf.append(comment);
			buf.append(lineDelim);
		}
	}

	buf.append(JdtFlags.getVisibilityString(flags));
	buf.append(' ');
	if (isStatic)
		buf.append("static "); //$NON-NLS-1$
	if (isSync)
		buf.append("synchronized "); //$NON-NLS-1$
	if (isFinal)
		buf.append("final "); //$NON-NLS-1$

	buf.append(typeName);
	buf.append(' ');
	buf.append(getterName);
	buf.append("() {"); //$NON-NLS-1$
	buf.append(lineDelim);

	boolean useThis= StubUtility.useThisForFieldAccess(field.getJavaProject());
	if (useThis && !isStatic) {
		fieldName= "this." + fieldName; //$NON-NLS-1$
	}

	String body= CodeGeneration.getGetterMethodBodyContent(field.getCompilationUnit(), parentType.getTypeQualifiedName('.'), getterName, fieldName, lineDelim);
	if (body != null) {
		buf.append(body);
	}
	buf.append("}"); //$NON-NLS-1$
	buf.append(lineDelim);
	return buf.toString();
}
 
Example 10
Source File: JavadocContents.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private int[] computeFieldRange(IField field) throws JavaModelException {
	if (!this.hasComputedChildrenSections) {
		computeChildrenSections();
	}
	
	StringBuffer buffer = new StringBuffer(field.getElementName());
	buffer.append(JavadocConstants.ANCHOR_PREFIX_END);
	char[] anchor = String.valueOf(buffer).toCharArray();
	
	int[] range = null;
	
	if (this.indexOfFieldDetails == -1 || this.indexOfFieldsBottom == -1) {
		// the detail section has no top or bottom, so the doc has an unknown format
		if (this.unknownFormatAnchorIndexes == null) {
			this.unknownFormatAnchorIndexes = new int[this.type.getChildren().length];
			this.unknownFormatAnchorIndexesCount = 0;
			this.unknownFormatLastAnchorFoundIndex = this.childrenStart;
		}
		
		this.tempAnchorIndexes = this.unknownFormatAnchorIndexes;
		this.tempAnchorIndexesCount = this.unknownFormatAnchorIndexesCount;
		this.tempLastAnchorFoundIndex = this.unknownFormatLastAnchorFoundIndex;
		
		range = computeChildRange(anchor, this.indexOfFieldsBottom);
		
		this.unknownFormatLastAnchorFoundIndex = this.tempLastAnchorFoundIndex;
		this.unknownFormatAnchorIndexesCount = this.tempAnchorIndexesCount;
		this.unknownFormatAnchorIndexes = this.tempAnchorIndexes;
	} else {
		if (this.fieldAnchorIndexes == null) {
			this.fieldAnchorIndexes = new int[this.type.getFields().length];
			this.fieldAnchorIndexesCount = 0;
			this.fieldLastAnchorFoundIndex = this.indexOfFieldDetails;
		}
		
		this.tempAnchorIndexes = this.fieldAnchorIndexes;
		this.tempAnchorIndexesCount = this.fieldAnchorIndexesCount;
		this.tempLastAnchorFoundIndex = this.fieldLastAnchorFoundIndex;
		
		range = computeChildRange(anchor, this.indexOfFieldsBottom);
		
		this.fieldLastAnchorFoundIndex = this.tempLastAnchorFoundIndex;
		this.fieldAnchorIndexesCount = this.tempAnchorIndexesCount;
		this.fieldAnchorIndexes = this.tempAnchorIndexes;
	}
	
	return range;
}
 
Example 11
Source File: MethodContentGenerations.java    From jenerate with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Returns the field accessor string if the useGettersInsteadOfFields parameter is {@code true}, otherwise returns
 * the field name directly. This method assumes that the accessor has a standard name e.g 'getField()' in case the
 * field name is 'field'.
 * 
 * @param field the field to extract information from
 * @param useGettersInsteadOfFields {@code true} if the field accessor should be returned, {@code false} if the
 *            field name should be returned
 * @return the field accessor if the useGettersInsteadOfFields parameter is {@code true}, otherwise the field name
 *         directly
 * @throws JavaModelException if an problem occurs while retrieving field information
 */
public static String getFieldAccessorString(final IField field, final boolean useGettersInsteadOfFields)
        throws JavaModelException {
    if (useGettersInsteadOfFields) {
        return generateGetter(field);
    }
    return field.getElementName();

}