Java Code Examples for org.eclipse.jdt.core.compiler.CharOperation#indexOf()

The following examples show how to use org.eclipse.jdt.core.compiler.CharOperation#indexOf() . 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: Scribe.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private boolean hasNLSTag(int sourceStart) {
	// search the last comment where commentEnd < current lineEnd
	if (this.lineEnds == null) return false;
	int index = Arrays.binarySearch(this.lineEnds, sourceStart);
	int currentLineEnd = getLineEnd(-index);
	if (currentLineEnd != -1) {
		int commentIndex = getCommentIndex(currentLineEnd);
		if (commentIndex < 0) {
			commentIndex = -commentIndex - 2;
		}
		if (commentIndex >= 0 && commentIndex < this.commentPositions.length) {
			int start = this.commentPositions[commentIndex][0];
			if (start < 0) {
				start = -start;
				// check that we are on the same line
				int lineIndexForComment = Arrays.binarySearch(this.lineEnds, start);
				if (lineIndexForComment == index) {
					return CharOperation.indexOf(Scanner.TAG_PREFIX, this.scanner.source, true, start, currentLineEnd) != -1;
				}
			}
		}
	}
	return false;
}
 
Example 2
Source File: ClassFileReader.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public char[] getSourceName() {
	if (this.sourceName != null)
		return this.sourceName;

	char[] name = getInnerSourceName(); // member or local scenario
	if (name == null) {
		name = getName(); // extract from full name
		int start;
		if (isAnonymous()) {
			start = CharOperation.indexOf('$', name, CharOperation.lastIndexOf('/', name) + 1) + 1;
		} else {
			start = CharOperation.lastIndexOf('/', name) + 1;
		}
		if (start > 0) {
			char[] newName = new char[name.length - start];
			System.arraycopy(name, start, newName, 0, newName.length);
			name = newName;
		}
	}
	return this.sourceName = name;
}
 
Example 3
Source File: TypeSystem.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public final TypeBinding getUnannotatedType(TypeBinding type) {
	if (type.isUnresolvedType() && CharOperation.indexOf('$', type.sourceName()) > 0)
		type = BinaryTypeBinding.resolveType(type, this.environment, true); // to ensure unique id assignment (when enclosing type is parameterized, inner type is also) 
	if (type.id == TypeIds.NoId) {
		if (type.hasTypeAnnotations())
			throw new IllegalStateException();
		int typesLength = this.types.length;
		if (this.typeid == typesLength)
			System.arraycopy(this.types, 0, this.types = new TypeBinding[typesLength * 2][], 0, typesLength);
		this.types[type.id = this.typeid++] = new TypeBinding[4];
	} else {
		TypeBinding nakedType = this.types[type.id] == null ? null : this.types[type.id][0];
		if (type.hasTypeAnnotations() && nakedType == null)
			throw new IllegalStateException();
		if (nakedType != null)
			return nakedType;
		this.types[type.id] = new TypeBinding[4];  // well known type, assigned id elsewhere.
	}

	return this.types[type.id][0] = type;
}
 
Example 4
Source File: JavadocContents.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private int[] computeChildRange(int anchorEndStart, char[] anchor, int indexOfBottom) {
	int[] range = null;
			
	// try to find the bottom of the section
	if (indexOfBottom != -1) {
		// try to find the end of the anchor
		int indexOfEndLink = CharOperation.indexOf(JavadocConstants.ANCHOR_SUFFIX, this.content, false, anchorEndStart + anchor.length);
		if (indexOfEndLink != -1) {
			// try to find the next anchor
			int indexOfNextElement = CharOperation.indexOf(JavadocConstants.ANCHOR_PREFIX_START, this.content, false, indexOfEndLink);
			
			int javadocStart = indexOfEndLink + JavadocConstants.ANCHOR_SUFFIX_LENGTH;
			int javadocEnd = indexOfNextElement == -1 ? indexOfBottom : Math.min(indexOfNextElement, indexOfBottom);
			range = new int[]{javadocStart, javadocEnd};
		} else {
			// the anchor has no suffix
			range = UNKNOWN_FORMAT;
		}
	} else {
		// the detail section has no bottom
		range = UNKNOWN_FORMAT;
	}
	
	return range;
}
 
Example 5
Source File: JavadocContents.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public String getPackageDoc() throws JavaModelException {
	if (this.content == null) return null;
	int[] range = null;
	int index = CharOperation.indexOf(JavadocConstants.PACKAGE_DESCRIPTION_START, this.content, false, 0);
	if (index == -1) return null;
	index = CharOperation.indexOf(JavadocConstants.ANCHOR_SUFFIX, this.content, false, index);
	if (index == -1) return null;
	
	int start = CharOperation.indexOf(JavadocConstants.H2_PREFIX, this.content, false, index);
	if (start != -1) {
		start = CharOperation.indexOf(JavadocConstants.H2_SUFFIX, this.content, false, start);
		if (start != -1) index = start + JavadocConstants.H2_SUFFIX_LENGTH;
	}
	if (index != -1) {
		int end = CharOperation.indexOf(JavadocConstants.BOTTOM_NAVBAR, this.content, false, index);
		if (end == -1) end = this.content.length -1;
		range = new int[]{index, end};
		return String.valueOf(CharOperation.subarray(this.content, range[0], range[1]));
	}
	return null;
}
 
Example 6
Source File: SourceMapper.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private boolean hasToRetrieveSourceRangesForLocalClass(char[] eltName) {
	/*
	 * A$1$B$2 : true
	 * A$B$B$2 : true
	 * A$C$B$D : false
	 * A$F$B$D$1$F : true
	 * A$F$B$D$1F : true
	 * A$1 : true
	 * A$B : false
	 */
	if (eltName == null) return false;
	int length = eltName.length;
	int dollarIndex = CharOperation.indexOf('$', eltName, 0);
	while (dollarIndex != -1) {
		int nameStart = dollarIndex+1;
		if (nameStart == length) return false;
		if (Character.isDigit(eltName[nameStart]))
			return true;
		dollarIndex = CharOperation.indexOf('$', eltName, nameStart);
	}
	return false;
}
 
Example 7
Source File: BasicCompilationUnit.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public char[] getMainTypeName() {
	if (this.mainTypeName == null) {
		int start = CharOperation.lastIndexOf('/', this.fileName) + 1;
		if (start == 0 || start < CharOperation.lastIndexOf('\\', this.fileName))
			start = CharOperation.lastIndexOf('\\', this.fileName) + 1;
		int separator = CharOperation.indexOf('|', this.fileName) + 1;
		if (separator > start) // case of a .class file in a default package in a jar
			start = separator;

		int end = CharOperation.lastIndexOf('$', this.fileName);
		if (end == -1 || !Util.isClassFileName(this.fileName)) {
			end = CharOperation.lastIndexOf('.', this.fileName);
			if (end == -1)
				end = this.fileName.length;
		}

		this.mainTypeName = CharOperation.subarray(this.fileName, start, end);
	}
	return this.mainTypeName;
}
 
Example 8
Source File: HierarchyResolver.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public ReferenceBinding setFocusType(char[][] compoundName) {
	if (compoundName == null || this.lookupEnvironment == null) return null;
	this.focusType = this.lookupEnvironment.getCachedType(compoundName);
	if (this.focusType == null) {
		this.focusType = this.lookupEnvironment.askForType(compoundName);
		if (this.focusType == null) {
			int length = compoundName.length;
			char[] typeName = compoundName[length-1];
			int firstDollar = CharOperation.indexOf('$', typeName);
			if (firstDollar != -1) {
				compoundName[length-1] = CharOperation.subarray(typeName, 0, firstDollar);
				this.focusType = this.lookupEnvironment.askForType(compoundName);
				if (this.focusType != null) {
					char[][] memberTypeNames = CharOperation.splitOn('$', typeName, firstDollar+1, typeName.length);
					for (int i = 0; i < memberTypeNames.length; i++) {
						this.focusType = this.focusType.getMemberType(memberTypeNames[i]);
						if (this.focusType == null)
							return null;
					}
				}
			}
		}
	}
	return this.focusType;
}
 
Example 9
Source File: Util.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static IClassFile getClassFile(char[] fileName) {
	int jarSeparator = CharOperation.indexOf(IDependent.JAR_FILE_ENTRY_SEPARATOR, fileName);
	int pkgEnd = CharOperation.lastIndexOf('/', fileName); // pkgEnd is exclusive
	if (pkgEnd == -1)
		pkgEnd = CharOperation.lastIndexOf(File.separatorChar, fileName);
	if (jarSeparator != -1 && pkgEnd < jarSeparator) // if in a jar and no slash, it is a default package -> pkgEnd should be equal to jarSeparator
		pkgEnd = jarSeparator;
	if (pkgEnd == -1)
		return null;
	IPackageFragment pkg = getPackageFragment(fileName, pkgEnd, jarSeparator);
	if (pkg == null) return null;
	int start;
	return pkg.getClassFile(new String(fileName, start = pkgEnd + 1, fileName.length - start));
}
 
Example 10
Source File: SignatureWrapper.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public int computeEnd() {
	int index = this.start;
	while (this.signature[index] == '[')
		index++;
	switch (this.signature[index]) {
		case 'L' :
		case 'T' :
			this.end = CharOperation.indexOf(';', this.signature, this.start);
			if (this.bracket <= this.start) // already know it if its > start
				this.bracket = CharOperation.indexOf('<', this.signature, this.start);

			if (this.bracket > this.start && this.bracket < this.end)
				this.end = this.bracket;
			else if (this.end == -1)
				this.end = this.signature.length + 1;
			break;
		default :
			this.end = this.start;
	}

	if (this.use15specifics || this.end != this.bracket) {
		this.start = this.end + 1; // skip ';'
	} else {
		this.start = skipAngleContents(this.end) + 1;  // skip <<>*>;
		this.bracket = -1;
	}
	return this.end;
}
 
Example 11
Source File: TypeDeclarationPattern.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public void decodeIndexKey(char[] key) {
	int slash = CharOperation.indexOf(SEPARATOR, key, 0);
	this.simpleName = CharOperation.subarray(key, 0, slash);

	int start = ++slash;
	if (key[start] == SEPARATOR) {
		this.pkg = CharOperation.NO_CHAR;
	} else {
		slash = CharOperation.indexOf(SEPARATOR, key, start);
		this.pkg = internedPackageNames.add(CharOperation.subarray(key, start, slash));
	}

	// Continue key read by the end to decode modifiers
	int last = key.length-1;
	this.secondary = key[last] == 'S';
	if (this.secondary) {
		last -= 2;
	}
	this.modifiers = key[last-1] + (key[last]<<16);
	decodeModifiers();

	// Retrieve enclosing type names
	start = slash + 1;
	last -= 2; // position of ending slash
	if (start == last) {
		this.enclosingTypeNames = CharOperation.NO_CHAR_CHAR;
	} else {
		if (last == (start+1) && key[start] == ZERO_CHAR) {
			this.enclosingTypeNames = ONE_ZERO_CHAR;
		} else {
			this.enclosingTypeNames = CharOperation.splitOn('.', key, start, last);
		}
	}
}
 
Example 12
Source File: Signature.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Extracts the class and interface bounds from the given formal type
 * parameter signature. The class bound, if present, is listed before
 * the interface bounds. The signature is expected to be dot-based.
 *
 * @param formalTypeParameterSignature the formal type parameter signature
 * @return the (possibly empty) list of type signatures for the bounds
 * @exception IllegalArgumentException if the signature is syntactically
 *   incorrect
 * @since 3.0
 */
public static char[][] getTypeParameterBounds(char[] formalTypeParameterSignature) throws IllegalArgumentException {
	int p1 = CharOperation.indexOf(C_COLON, formalTypeParameterSignature);
	if (p1 < 0) {
		// no ":" means can't be a formal type parameter signature
		throw new IllegalArgumentException();
	}
	if (p1 == formalTypeParameterSignature.length - 1) {
		// no class or interface bounds
		return CharOperation.NO_CHAR_CHAR;
	}
	int p2 = CharOperation.indexOf(C_COLON, formalTypeParameterSignature, p1 + 1);
	char[] classBound;
	if (p2 < 0) {
		// no interface bounds
		classBound = CharOperation.subarray(formalTypeParameterSignature, p1 + 1, formalTypeParameterSignature.length);
		return new char[][] {classBound};
	}
	if (p2 == p1 + 1) {
		// no class bound, but 1 or more interface bounds
		classBound = null;
	} else {
		classBound = CharOperation.subarray(formalTypeParameterSignature, p1 + 1, p2);
	}
	char[][] interfaceBounds = CharOperation.splitOn(C_COLON, formalTypeParameterSignature, p2 + 1, formalTypeParameterSignature.length);
	if (classBound == null) {
		return interfaceBounds;
	}
	int resultLength = interfaceBounds.length + 1;
	char[][] result = new char[resultLength][];
	result[0] = classBound;
	System.arraycopy(interfaceBounds, 0, result, 1, interfaceBounds.length);
	return result;
}
 
Example 13
Source File: ConstructorDeclarationPattern.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public void decodeIndexKey(char[] key) {
	int last = key.length - 1;
	int slash = CharOperation.indexOf(SEPARATOR, key, 0);
	this.declaringSimpleName = CharOperation.subarray(key, 0, slash);
	
	int start = slash + 1;
	slash = CharOperation.indexOf(SEPARATOR, key, start);
	last = slash - 1;
	
	boolean isDefaultConstructor = key[last] == '#';
	if (isDefaultConstructor) {
		this.parameterCount = -1;
	} else {
		this.parameterCount = 0;
		int power = 1;
		for (int i = last; i >= start; i--) {
			if (i == last) {
				this.parameterCount = key[i] - '0';
			} else {
				power *= 10;
				this.parameterCount += power * (key[i] - '0');
			}
		}
	}
	
	slash = slash + 3;
	last = slash - 1;
	
	int typeModifiersWithExtraFlags = key[last-1] + (key[last]<<16);
	this.declaringTypeModifiers = decodeModifers(typeModifiersWithExtraFlags);
	this.extraFlags = decodeExtraFlags(typeModifiersWithExtraFlags);
	
	// initialize optional fields
	this.declaringPackageName = null;
	this.modifiers = 0;
	this.signature = null;
	this.parameterTypes = null;
	this.parameterNames = null;
	
	boolean isMemberType = (this.extraFlags & ExtraFlags.IsMemberType) != 0;
	
	if (!isMemberType) {
		start = slash + 1;
		if (this.parameterCount == -1) {
			slash = key.length;
			last = slash - 1;
		} else {
			slash = CharOperation.indexOf(SEPARATOR, key, start);
		}
		last = slash - 1;
		
		this.declaringPackageName = CharOperation.subarray(key, start, slash);
		
		start = slash + 1;
		if (this.parameterCount == 0) {
			slash = slash + 3;
			last = slash - 1;
			
			this.modifiers = key[last-1] + (key[last]<<16);
		} else if (this.parameterCount > 0){
			slash = CharOperation.indexOf(SEPARATOR, key, start);
			last = slash - 1;
			
			boolean hasParameterStoredAsSignature = (this.extraFlags & ExtraFlags.ParameterTypesStoredAsSignature) != 0;
			if (hasParameterStoredAsSignature) {
				this.signature  = CharOperation.subarray(key, start, slash);
				CharOperation.replace(this.signature , '\\', SEPARATOR);
			} else {
				this.parameterTypes = CharOperation.splitOn(PARAMETER_SEPARATOR, key, start, slash);
			}
			start = slash + 1;
			slash = CharOperation.indexOf(SEPARATOR, key, start);
			last = slash - 1;
			
			if (slash != start) {
				this.parameterNames = CharOperation.splitOn(PARAMETER_SEPARATOR, key, start, slash);
			}
			
			slash = slash + 3;
			last = slash - 1;
			
			this.modifiers = key[last-1] + (key[last]<<16);
		} else {
			this.modifiers = ClassFileConstants.AccPublic;
		}
	}
	
	removeInternalFlags(); // remove internal flags
}
 
Example 14
Source File: DoubleLiteral.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public void computeConstant() {
	Double computedValue;
	boolean containsUnderscores = CharOperation.indexOf('_', this.source) > 0;
	if (containsUnderscores) {
		// remove all underscores from source
		this.source = CharOperation.remove(this.source, '_');
	}
	try {
		computedValue = Double.valueOf(String.valueOf(this.source));
	} catch (NumberFormatException e) {
		// hex floating point literal
		// being rejected by 1.4 libraries where Double.valueOf(...) doesn't handle hex decimal floats
		try {
			double v = FloatUtil.valueOfHexDoubleLiteral(this.source);
			if (v == Double.POSITIVE_INFINITY) {
				// error: the number is too large to represent
				return;
			}
			if (Double.isNaN(v)) {
				// error: the number is too small to represent
				return;
			}
			this.value = v;
			this.constant = DoubleConstant.fromValue(v);
		} catch (NumberFormatException e1) {
			// if the computation of the constant fails
		}
		return;
	}
	final double doubleValue = computedValue.doubleValue();
	if (doubleValue > Double.MAX_VALUE) {
		// error: the number is too large to represent
		return;
	}
	if (doubleValue < Double.MIN_VALUE) {
		// see 1F6IGUU
		// a true 0 only has '0' and '.' in mantissa
		// 1.0e-5000d is non-zero, but underflows to 0
		boolean isHexaDecimal = false;
		label : for (int i = 0; i < this.source.length; i++) { //it is welled formated so just test against '0' and potential . D d
			switch (this.source[i]) {
				case '0' :
				case '.' :
					break;
				case 'x' :
				case 'X' :
					isHexaDecimal = true;
					break;
				case 'e' :
				case 'E' :
				case 'f' :
				case 'F' :
				case 'd' :
				case 'D' :
					if (isHexaDecimal) {
						return;
					}
					// starting the exponent - mantissa is all zero
					// no exponent - mantissa is all zero
					break label;
				case 'p' :
				case 'P' :
					break label;
				default :
					// error: the number is too small to represent
					return;
			}
		}
	}
	this.value = doubleValue;
	this.constant = DoubleConstant.fromValue(this.value);
}
 
Example 15
Source File: BinaryTypeBinding.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private void initializeTypeVariable(TypeVariableBinding variable, TypeVariableBinding[] existingVariables, SignatureWrapper wrapper, char[][][] missingTypeNames, TypeAnnotationWalker walker) {
	if (!isPrototype()) throw new IllegalStateException();
	// ParameterSignature = Identifier ':' TypeSignature
	//   or Identifier ':' TypeSignature(optional) InterfaceBound(s)
	// InterfaceBound = ':' TypeSignature
	int colon = CharOperation.indexOf(Util.C_COLON, wrapper.signature, wrapper.start);
	wrapper.start = colon + 1; // skip name + ':'
	ReferenceBinding type, firstBound = null;
	short rank = 0;
	if (wrapper.signature[wrapper.start] == Util.C_COLON) {
		type = this.environment.getResolvedType(TypeConstants.JAVA_LANG_OBJECT, null);
	} else {
		TypeBinding typeFromTypeSignature = this.environment.getTypeFromTypeSignature(wrapper, existingVariables, this, missingTypeNames, walker.toTypeBound(rank++));
		if (typeFromTypeSignature instanceof ReferenceBinding) {
			type = (ReferenceBinding) typeFromTypeSignature;
		} else {
			// this should only happen if the signature is corrupted (332423)
			type = this.environment.getResolvedType(TypeConstants.JAVA_LANG_OBJECT, null);
		}
		firstBound = type;
	}

	// variable is visible to its bounds
	variable.modifiers |= ExtraCompilerModifiers.AccUnresolved;
	variable.setSuperClass(type);

	ReferenceBinding[] bounds = null;
	if (wrapper.signature[wrapper.start] == Util.C_COLON) {
		java.util.ArrayList types = new java.util.ArrayList(2);
		do {
			wrapper.start++; // skip ':'
			types.add(this.environment.getTypeFromTypeSignature(wrapper, existingVariables, this, missingTypeNames, walker.toTypeBound(rank++)));
		} while (wrapper.signature[wrapper.start] == Util.C_COLON);
		bounds = new ReferenceBinding[types.size()];
		types.toArray(bounds);
	}

	variable.setSuperInterfaces(bounds == null ? Binding.NO_SUPERINTERFACES : bounds);
	if (firstBound == null) {
		firstBound = variable.superInterfaces.length == 0 ? null : variable.superInterfaces[0];
	}
	variable.setFirstBound(firstBound);
}
 
Example 16
Source File: BinaryIndexer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private int extractArgCount(char[] signature, char[] className) throws ClassFormatException {
	int indexOfClosingParen = CharOperation.lastIndexOf(')', signature);
	if (indexOfClosingParen == 1) {
		// there is no parameter
		return 0;
	}
	if (indexOfClosingParen == -1) {
		throw new ClassFormatException(ClassFormatException.ErrInvalidMethodSignature);
	}
	int parameterTypesCounter = 0;
	for (int i = 1; i < indexOfClosingParen; i++) {
		switch(signature[i]) {
			case 'B':
			case 'C':
			case 'D':
			case 'F':
			case 'I':
			case 'J':
			case 'S':
			case 'Z':
				parameterTypesCounter++;
				break;
			case 'L':
				int indexOfSemiColon = CharOperation.indexOf(';', signature, i+1);
				if (indexOfSemiColon == -1) throw new ClassFormatException(ClassFormatException.ErrInvalidMethodSignature);
				// verify if first parameter is synthetic
				if (className != null && parameterTypesCounter == 0) {
					char[] classSignature = Signature.createCharArrayTypeSignature(className, true);
					int length = indexOfSemiColon-i+1;
					if (classSignature.length > (length+1)) {
						// synthetic means that parameter type has same signature than given class
						for (int j=i, k=0; j<indexOfSemiColon; j++, k++) {
							if (!(signature[j] == classSignature[k] || (signature[j] == '/' && classSignature[k] == '.' ))) {
								parameterTypesCounter++;
								break;
							}
						}
					} else {
						parameterTypesCounter++;
					}
					className = null; // do not verify following parameters
				} else {
					parameterTypesCounter++;
				}
				i = indexOfSemiColon;
				break;
			case '[':
				break;
			default:
				throw new ClassFormatException(ClassFormatException.ErrInvalidMethodSignature);
		}
	}
	return parameterTypesCounter;
}
 
Example 17
Source File: Signature.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Converts the given method signature to a readable form. The method signature is expected to
 * be dot-based.
 * <p>
 * For example:
 * <pre>
 * <code>
 * toString("([Ljava.lang.String;)V", "main", new String[] {"args"}, false, true) -> "void main(String[] args)"
 * </code>
 * </pre>
 * </p>
 *
 * @param methodSignature the method signature to convert
 * @param methodName the name of the method to insert in the result, or
 *   <code>null</code> if no method name is to be included
 * @param parameterNames the parameter names to insert in the result, or
 *   <code>null</code> if no parameter names are to be included; if supplied,
 *   the number of parameter names must match that of the method signature
 * @param fullyQualifyTypeNames <code>true</code> if type names should be fully
 *   qualified, and <code>false</code> to use only simple names
 * @param includeReturnType <code>true</code> if the return type is to be
 *   included
 * @param isVargArgs <code>true</code> if the last argument should be displayed as a
 * variable argument,  <code>false</code> otherwise.
 * @return the char array representation of the method signature
 * @throws IllegalArgumentException if the method signature is syntactically incorrect
 *
 * @since 3.1
 */
public static char[] toCharArray(char[] methodSignature, char[] methodName, char[][] parameterNames, boolean fullyQualifyTypeNames, boolean includeReturnType, boolean isVargArgs) {
	int firstParen = CharOperation.indexOf(C_PARAM_START, methodSignature);
	if (firstParen == -1) {
		throw new IllegalArgumentException();
	}

	StringBuffer buffer = new StringBuffer(methodSignature.length + 10);

	// return type
	if (includeReturnType) {
		char[] rts = getReturnType(methodSignature);
		appendTypeSignature(rts, 0 , fullyQualifyTypeNames, buffer);
		buffer.append(' ');
	}

	// selector
	if (methodName != null) {
		buffer.append(methodName);
	}

	// parameters
	buffer.append('(');
	char[][] pts = getParameterTypes(methodSignature);
	// search for the last array in the signature
	int max = pts.length;
	int index = max - 1;
	loop: for (int i = index; i >= 0; i--) {
		if (pts[i][0] == Signature.C_ARRAY) {
			break loop;
		}
		index--;
	}
	for (int i = 0; i < max; i++) {
		if (i == index) {
			appendTypeSignature(pts[i], 0 , fullyQualifyTypeNames, buffer, isVargArgs);
		} else {
			appendTypeSignature(pts[i], 0 , fullyQualifyTypeNames, buffer);
		}
		if (parameterNames != null) {
			buffer.append(' ');
			buffer.append(parameterNames[i]);
		}
		if (i != pts.length - 1) {
			buffer.append(',');
			buffer.append(' ');
		}
	}
	buffer.append(')');
	char[] result = new char[buffer.length()];
	buffer.getChars(0, buffer.length(), result, 0);
	return result;
}
 
Example 18
Source File: Util.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public static String toString(char[] declaringClass, char[] methodName, char[] methodSignature, boolean includeReturnType, boolean compact) {
	final boolean isConstructor = CharOperation.equals(methodName, INIT);
	int firstParen = CharOperation.indexOf(Signature.C_PARAM_START, methodSignature);
	if (firstParen == -1) {
		return ""; //$NON-NLS-1$
	}

	StringBuffer buffer = new StringBuffer(methodSignature.length + 10);

	// decode declaring class name
	// it can be either an array signature or a type signature
	if (declaringClass != null && declaringClass.length > 0) {
		char[] declaringClassSignature = null;
		if (declaringClass[0] == Signature.C_ARRAY) {
			CharOperation.replace(declaringClass, '/', '.');
			declaringClassSignature = Signature.toCharArray(declaringClass);
		} else {
			CharOperation.replace(declaringClass, '/', '.');
			declaringClassSignature = declaringClass;
		}
		int lastIndexOfSlash = CharOperation.lastIndexOf('.', declaringClassSignature);
		if (compact && lastIndexOfSlash != -1) {
			buffer.append(declaringClassSignature, lastIndexOfSlash + 1, declaringClassSignature.length - lastIndexOfSlash - 1);
		} else {
			buffer.append(declaringClassSignature);
		}
		if (!isConstructor) {
			buffer.append('.');
		}
	}

	// selector
	if (!isConstructor && methodName != null) {
		buffer.append(methodName);
	}

	// parameters
	buffer.append('(');
	char[][] pts = Signature.getParameterTypes(methodSignature);
	for (int i = 0, max = pts.length; i < max; i++) {
		appendTypeSignature(pts[i], 0 , buffer, compact);
		if (i != pts.length - 1) {
			buffer.append(',');
			buffer.append(' ');
		}
	}
	buffer.append(')');

	if (!isConstructor) {
		buffer.append(" : "); //$NON-NLS-1$
		// return type
		if (includeReturnType) {
			char[] rts = Signature.getReturnType(methodSignature);
			appendTypeSignature(rts, 0 , buffer, compact);
		}
	}
	return String.valueOf(buffer);
}
 
Example 19
Source File: JDTCompilerAdapter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * check the compiler arguments.
 * Extract from files specified using @, lines marked with ADAPTER_PREFIX
 * These lines specify information that needs to be interpreted by us.
 * @param args compiler arguments to process
 */
private void checkCompilerArgs(String[] args) {
	for (int i = 0; i < args.length; i++) {
		if (args[i].charAt(0) == '@') {
			try {
				char[] content = Util.getFileCharContent(new File(args[i].substring(1)), null);
				int offset = 0;
				int prefixLength = ADAPTER_PREFIX.length;
				while ((offset = CharOperation.indexOf(ADAPTER_PREFIX, content, true, offset)) > -1) {
					int start = offset + prefixLength;
					int end = CharOperation.indexOf('\n', content, start);
					if (end == -1)
						end = content.length;
					while (CharOperation.isWhitespace(content[end])) {
						end--;
					}

					// end is inclusive, but in the API end is exclusive
					if (CharOperation.equals(ADAPTER_ENCODING, content, start, start + ADAPTER_ENCODING.length)) {
						CharOperation.replace(content, SEPARATOR_CHARS, File.separatorChar, start, end + 1);
						// file or folder level custom encoding
						start += ADAPTER_ENCODING.length;
						int encodeStart = CharOperation.lastIndexOf('[', content, start, end);
						if (start < encodeStart && encodeStart < end) {
							boolean isFile = CharOperation.equals(SuffixConstants.SUFFIX_java, content, encodeStart - 5, encodeStart, false);

							String str = String.valueOf(content, start, encodeStart - start);
							String enc = String.valueOf(content, encodeStart, end - encodeStart + 1);
							if (isFile) {
								if (this.fileEncodings == null)
									this.fileEncodings = new HashMap();
								//use File to translate the string into a path with the correct File.seperator
								this.fileEncodings.put(str, enc);
							} else {
								if (this.dirEncodings == null)
									this.dirEncodings = new HashMap();
								this.dirEncodings.put(str, enc);
							}
						}
					} else if (CharOperation.equals(ADAPTER_ACCESS, content, start, start + ADAPTER_ACCESS.length)) {
						// access rules for the classpath
						start += ADAPTER_ACCESS.length;
						int accessStart = CharOperation.indexOf('[', content, start, end);
						CharOperation.replace(content, SEPARATOR_CHARS, File.separatorChar, start, accessStart);
						if (start < accessStart && accessStart < end) {
							String path = String.valueOf(content, start, accessStart - start);
							String access = String.valueOf(content, accessStart, end - accessStart + 1);
							if (this.accessRules == null)
								this.accessRules = new ArrayList();
							this.accessRules.add(path);
							this.accessRules.add(access);
						}
					}
					offset = end;
				}
			} catch (IOException e) {
				//ignore
			}
		}
	}

}
 
Example 20
Source File: Scribe.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private int getTextLength(FormatJavadocBlock block, FormatJavadocText text) {

		// Special case for immutable tags
		if (text.isImmutable()) {
			this.scanner.resetTo(text.sourceStart , text.sourceEnd);
			int textLength = 0;
			while (!this.scanner.atEnd()) {
				try {
	                int token = this.scanner.getNextToken();
	    			if (token == TerminalTokens.TokenNameWHITESPACE) {
						if (CharOperation.indexOf('\n', this.scanner.source, this.scanner.startPosition, this.scanner.currentPosition) >= 0) {
							textLength = 0;
							this.scanner.getNextChar();
							if (this.scanner.currentCharacter == '*') {
								this.scanner.getNextChar();
								if (this.scanner.currentCharacter != ' ') {
									textLength++;
								}
							} else {
								textLength++;
							}
							continue;
						}
	    			}
	    			textLength += (this.scanner.atEnd() ? this.scanner.eofPosition : this.scanner.currentPosition) - this.scanner.startPosition;
                } catch (InvalidInputException e) {
   					// maybe an unterminated string or comment
	    			textLength += (this.scanner.atEnd() ? this.scanner.eofPosition : this.scanner.currentPosition) - this.scanner.startPosition;
                }
			}
			return textLength;
		}

		// Simple for one line tags
	    if (block.isOneLineTag()) {
	    	return text.sourceEnd - text.sourceStart + 1;
	    }

	    // Find last line
    	int startLine = Util.getLineNumber(text.sourceStart, this.lineEnds, 0, this.maxLines);
    	int endLine = startLine;
    	int previousEnd = -1;
    	for (int i=0; i<=text.separatorsPtr; i++) {
    		int end = (int) (text.separators[i] >>> 32);
    		endLine = Util.getLineNumber(end, this.lineEnds, endLine-1, this.maxLines);
    		if (endLine > startLine) {
    			return previousEnd - text.sourceStart + 1;
    		}
    		previousEnd = end;
    	}

    	// This was a one line text
		return text.sourceEnd - text.sourceStart + 1;
    }