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

The following examples show how to use org.eclipse.jdt.core.IField#getTypeSignature() . 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: RefactoringAvailabilityTester.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static boolean isGeneralizeTypeAvailable(final IJavaElement element) throws JavaModelException {
	if (element != null && element.exists()) {
		String type= null;
		if (element instanceof IMethod)
			type= ((IMethod) element).getReturnType();
		else if (element instanceof IField) {
			final IField field= (IField) element;
			if (JdtFlags.isEnum(field))
				return false;
			type= field.getTypeSignature();
		} else if (element instanceof ILocalVariable)
			return true;
		else if (element instanceof IType) {
			final IType clazz= (IType) element;
			if (JdtFlags.isEnum(clazz))
				return false;
			return true;
		}
		if (type == null || PrimitiveType.toCode(Signature.toString(type)) != null)
			return false;
		return true;
	}
	return false;
}
 
Example 2
Source File: RefactoringAvailabilityTester.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static boolean isGeneralizeTypeAvailable(final IJavaElement element) throws JavaModelException {
	if (element != null && element.exists()) {
		String type = null;
		if (element instanceof IMethod) {
			type = ((IMethod) element).getReturnType();
		} else if (element instanceof IField) {
			final IField field = (IField) element;
			if (JdtFlags.isEnum(field)) {
				return false;
			}
			type = field.getTypeSignature();
		} else if (element instanceof ILocalVariable) {
			return true;
		} else if (element instanceof IType) {
			final IType clazz = (IType) element;
			if (JdtFlags.isEnum(clazz)) {
				return false;
			}
			return true;
		}
		if (type == null || PrimitiveType.toCode(Signature.toString(type)) != null) {
			return false;
		}
		return true;
	}
	return false;
}
 
Example 3
Source File: WizardUtils.java    From txtUML with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @return an empty optional if the given type does not have any field,
 *         otherwise the name of the model package and its java project
 *         respectively, which contains the types of the given diagramType
 */
public static Optional<Pair<String, String>> getModelByFields(IType diagramType) {
	try {
		List<String> referencedProjects = new ArrayList<>(
				Arrays.asList(diagramType.getJavaProject().getRequiredProjectNames()));
		referencedProjects.add(diagramType.getJavaProject().getElementName());

		for (IField field : diagramType.getFields()) {
			String typeSignature = field.getTypeSignature();
			
			// generic type parameters
			int typeParamStartIdx = typeSignature.indexOf("<Q");
			if (typeParamStartIdx != -1) {
				typeSignature = typeSignature.substring(typeParamStartIdx + 1, typeSignature.indexOf(';') + 1);
			}
			
			String[][] resolvedTypes = resolveType(diagramType,
					typeSignature.substring(1, typeSignature.length() - 1));
			List<String[]> resolvedTypeList = new ArrayList<>(Arrays.asList(resolvedTypes));

			for (String[] type : resolvedTypeList) {
				Optional<Pair<String, String>> model = ModelUtils.getModelOf(type[0], referencedProjects);
				if (model.isPresent()) {
					return model;
				}
			}
		}

		IType[] superTypes = diagramType.newSupertypeHierarchy(null).getAllSupertypes(diagramType);
		if (superTypes.length != 0) {
			IType superType = superTypes[0];
			return getModelByFields(superType);
		}
	} catch (JavaModelException | NoSuchElementException e) {
	}

	return Optional.empty();
}
 
Example 4
Source File: StubCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected void appendFieldDeclaration(final IField field) throws JavaModelException {
	appendFlags(field);
	fBuffer.append(" "); //$NON-NLS-1$
	final String signature= field.getTypeSignature();
	fBuffer.append(Signature.toString(signature));
	fBuffer.append(" "); //$NON-NLS-1$
	fBuffer.append(field.getElementName());
	if (Flags.isFinal(field.getFlags())) {
		fBuffer.append("="); //$NON-NLS-1$
		appendExpression(signature);
	}
	fBuffer.append(";"); //$NON-NLS-1$
}
 
Example 5
Source File: AddGetterSetterAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Creates a key used in hash maps for a method signature (gettersettername+arguments(fqn)).
 * 
 * @param methodName the method name
 * @param field the filed
 * @return the signature
 * @throws JavaModelException if getting the field's type signature fails
 */
private static String createSignatureKey(String methodName, IField field) throws JavaModelException {
	StringBuffer buffer= new StringBuffer();
	buffer.append(methodName);
	String fieldType= field.getTypeSignature();
	String signature= Signature.getSimpleName(Signature.toString(fieldType));
	buffer.append("#"); //$NON-NLS-1$
	buffer.append(signature);

	return buffer.toString();
}
 
Example 6
Source File: SearchUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean isPrimitiveOrString(IField field) {
	String fieldType;
	try {
		fieldType= field.getTypeSignature();
	} catch (JavaModelException ex) {
		return false;
	}
	char first= fieldType.charAt(0);
	return (first != Signature.C_RESOLVED && first != Signature.C_UNRESOLVED && first != Signature.C_ARRAY)
		|| ((first == Signature.C_RESOLVED || first == Signature.C_UNRESOLVED) && fieldType.substring(1, fieldType.length() - 1).equals(String.class.getName())
		|| (first == Signature.C_UNRESOLVED && fieldType.substring(1, fieldType.length() - 1).equals("String"))); //$NON-NLS-1$
}
 
Example 7
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 8
Source File: JDTMethodHelper.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
private static IMethod createGetterMethod(final IType type, IField field) {
    try {
        return new GroovyFieldAccessorMethod(type, GetterSetterUtil.getGetterName(field, null),
                field.getTypeSignature(), new String[] {}, new String[] {});
    } catch (JavaModelException e) {
        BonitaStudioLog.error(e);
        return null;
    }
}
 
Example 9
Source File: ParameterGuesser.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public Variable createVariable(IJavaElement element, IType enclosingType, String expectedType, int positionScore) throws JavaModelException {
	int variableType;
	int elementType= element.getElementType();
	String elementName= element.getElementName();

	String typeSignature;
	switch (elementType) {
		case IJavaElement.FIELD: {
			IField field= (IField) element;
			if (field.getDeclaringType().equals(enclosingType)) {
				variableType= Variable.FIELD;
			} else {
				variableType= Variable.INHERITED_FIELD;
			}
			if (field.isResolved()) {
				typeSignature= new BindingKey(field.getKey()).toSignature();
			} else {
				typeSignature= field.getTypeSignature();
			}
			break;
		}
		case IJavaElement.LOCAL_VARIABLE: {
			ILocalVariable locVar= (ILocalVariable) element;
			variableType= Variable.LOCAL;
			typeSignature= locVar.getTypeSignature();
			break;
		}
		case IJavaElement.METHOD: {
			IMethod method= (IMethod) element;
			if (isMethodToSuggest(method)) {
				if (method.getDeclaringType().equals(enclosingType)) {
					variableType= Variable.METHOD;
				} else {
					variableType= Variable.INHERITED_METHOD;
				}
				if (method.isResolved()) {
					typeSignature= Signature.getReturnType(new BindingKey(method.getKey()).toSignature());
				} else {
					typeSignature= method.getReturnType();
				}
				elementName= elementName + "()";  //$NON-NLS-1$
			} else {
				return null;
			}
			break;
		}
		default:
			return null;
	}
	String type= Signature.toString(typeSignature);

	boolean isAutoboxMatch= isPrimitiveType(expectedType) != isPrimitiveType(type);
	return new Variable(type, elementName, variableType, isAutoboxMatch, positionScore);
}
 
Example 10
Source File: GenerateDelegateMethodsHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private static boolean hasPrimitiveType(IField field) throws JavaModelException {
	String signature = field.getTypeSignature();
	char first = Signature.getElementType(signature).charAt(0);
	return (first != Signature.C_RESOLVED && first != Signature.C_UNRESOLVED);
}
 
Example 11
Source File: GetterSetterUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public static IMethod getSetter(IField field) throws JavaModelException{
	String[] args= new String[] { field.getTypeSignature() };
	return JavaModelUtil.findMethod(getSetterName(field, EMPTY), args, false, field.getDeclaringType());
}
 
Example 12
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 13
Source File: AddDelegateMethodsAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static boolean hasPrimitiveType(IField field) throws JavaModelException {
	String signature= field.getTypeSignature();
	char first= Signature.getElementType(signature).charAt(0);
	return (first != Signature.C_RESOLVED && first != Signature.C_UNRESOLVED);
}
 
Example 14
Source File: ParameterGuesser.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public Variable createVariable(IJavaElement element, IType enclosingType, String expectedType, int positionScore) throws JavaModelException {
	int variableType;
	int elementType= element.getElementType();
	String elementName= element.getElementName();

	String typeSignature;
	switch (elementType) {
		case IJavaElement.FIELD: {
			IField field= (IField) element;
			if (field.getDeclaringType().equals(enclosingType)) {
				variableType= Variable.FIELD;
			} else {
				variableType= Variable.INHERITED_FIELD;
			}
			if (field.isResolved()) {
				typeSignature= new BindingKey(field.getKey()).toSignature();
			} else {
				typeSignature= field.getTypeSignature();
			}
			break;
		}
		case IJavaElement.LOCAL_VARIABLE: {
			ILocalVariable locVar= (ILocalVariable) element;
			variableType= Variable.LOCAL;
			typeSignature= locVar.getTypeSignature();
			break;
		}
		case IJavaElement.METHOD: {
			IMethod method= (IMethod) element;
			if (isMethodToSuggest(method)) {
				if (method.getDeclaringType().equals(enclosingType)) {
					variableType= Variable.METHOD;
				} else {
					variableType= Variable.INHERITED_METHOD;
				}
				if (method.isResolved()) {
					typeSignature= Signature.getReturnType(new BindingKey(method.getKey()).toSignature());
				} else {
					typeSignature= method.getReturnType();
				}
				elementName= elementName + "()";  //$NON-NLS-1$
			} else {
				return null;
			}
			break;
		}
		default:
			return null;
	}
	String type= Signature.toString(typeSignature);

	boolean isAutoboxMatch= isPrimitiveType(expectedType) != isPrimitiveType(type);
	return new Variable(type, elementName, variableType, isAutoboxMatch, positionScore, NO_TRIGGERS, getImageDescriptor(element));
}