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

The following examples show how to use org.eclipse.jdt.core.compiler.CharOperation#replace() . 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: Factory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private AnnotationMirrorImpl createAnnotationMirror(String annoTypeName, AnnotationBinding annoInstance) {
	ReferenceBinding binding = annoInstance.getAnnotationType();
	if (binding != null && binding.isAnnotationType() ) {
		char[] qName;
		if (binding.isMemberType()) {
			annoTypeName = annoTypeName.replace('$', '.');
			qName = CharOperation.concatWith(binding.enclosingType().compoundName, binding.sourceName, '.');
			CharOperation.replace(qName, '$', '.');
		} else {
			qName = CharOperation.concatWith(binding.compoundName, '.');
		}
		if(annoTypeName.equals(new String(qName)) ){
			return (AnnotationMirrorImpl)_env.getFactory().newAnnotationMirror(annoInstance);
		}
	}
	return null;
}
 
Example 2
Source File: HierarchyBinaryType.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public char[] getGenericSignature() {
	if (this.typeParameterSignatures != null && this.genericSignature == null) {
		StringBuffer buffer = new StringBuffer();
		buffer.append('<');
		for (int i = 0, length = this.typeParameterSignatures.length; i < length; i++) {
			buffer.append(this.typeParameterSignatures[i]);
		}
		buffer.append('>');
		if (this.superclass == null)
			buffer.append(Signature.createTypeSignature("java.lang.Object", true/*resolved*/)); //$NON-NLS-1$
		else
			buffer.append(Signature.createTypeSignature(this.superclass, true/*resolved*/));
		if (this.superInterfaces != null)
			for (int i = 0, length = this.superInterfaces.length; i < length; i++)
				buffer.append(Signature.createTypeSignature(this.superInterfaces[i], true/*resolved*/));
		this.genericSignature = buffer.toString().toCharArray();
		CharOperation.replace(this.genericSignature, '.', '/');
	}
	return this.genericSignature;
}
 
Example 3
Source File: KeyToSignature.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public void consumeMethod(char[] selector, char[] methodSignature) {
	this.arguments = new ArrayList();
	this.typeArguments = new ArrayList();
	CharOperation.replace(methodSignature, '/', '.');
	switch(this.kind) {
		case SIGNATURE:
			this.signature = new StringBuffer();
			this.signature.append(methodSignature);
			break;
		case THROWN_EXCEPTIONS:
			if (CharOperation.indexOf('^', methodSignature) > 0) {
				char[][] types = Signature.getThrownExceptionTypes(methodSignature);
				int length = types.length;
				for (int i=0; i<length; i++) {
					this.thrownExceptions.add(new String(types[i]));
				}
			}
			break;
	}
}
 
Example 4
Source File: TypeElementImpl.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public String toString() {
	ReferenceBinding binding = (ReferenceBinding) this._binding;
	char[] concatWith = CharOperation.concatWith(binding.compoundName, '.');
	if (binding.isNestedType()) {
		CharOperation.replace(concatWith, '$', '.');
		return new String(concatWith);
	}
	return new String(concatWith);

}
 
Example 5
Source File: ErrorTypeElement.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Name getQualifiedName() {
	ReferenceBinding binding = (ReferenceBinding)_binding;
	char[] qName;
	if (binding.isMemberType()) {
		qName = CharOperation.concatWith(binding.enclosingType().compoundName, binding.sourceName, '.');
		CharOperation.replace(qName, '$', '.');
	} else {
		qName = CharOperation.concatWith(binding.compoundName, '.');
	}
	return new NameImpl(qName);
}
 
Example 6
Source File: ClasspathJar.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public char[] normalizedPath() {
	if (this.normalizedPath == null) {
		String path2 = this.getPath();
		char[] rawName = path2.toCharArray();
		if (File.separatorChar == '\\') {
			CharOperation.replace(rawName, '\\', '/');
		}
		this.normalizedPath = CharOperation.subarray(rawName, 0, CharOperation.lastIndexOf('.', rawName));
	}
	return this.normalizedPath;
}
 
Example 7
Source File: Disassembler.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private char[] getSignatureForField(char[] fieldDescriptor) {
	char[] newFieldDescriptor = CharOperation.replaceOnCopy(fieldDescriptor, '/', '.');
	newFieldDescriptor = CharOperation.replaceOnCopy(newFieldDescriptor, '$', '%');
	char[] fieldDescriptorSignature = Signature.toCharArray(newFieldDescriptor);
	CharOperation.replace(fieldDescriptorSignature, '%', '$');
	return fieldDescriptorSignature;
}
 
Example 8
Source File: HierarchyBinaryType.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public HierarchyBinaryType(int modifiers, char[] qualification, char[] sourceName, char[] enclosingTypeName, char[][] typeParameterSignatures, char typeSuffix){

	this.modifiers = modifiers;
	this.sourceName = sourceName;
	if (enclosingTypeName == null){
		this.name = CharOperation.concat(qualification, sourceName, '/');
	} else {
		this.name = CharOperation.concat(qualification, '/', enclosingTypeName, '$', sourceName); //rebuild A$B name
		this.enclosingTypeName = CharOperation.concat(qualification, enclosingTypeName,'/');
		CharOperation.replace(this.enclosingTypeName, '.', '/');
	}
	this.typeParameterSignatures = typeParameterSignatures;
	CharOperation.replace(this.name, '.', '/');
}
 
Example 9
Source File: ClasspathDirectory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public char[] normalizedPath() {
	if (this.normalizedPath == null) {
		this.normalizedPath = this.path.toCharArray();
		if (File.separatorChar == '\\') {
			CharOperation.replace(this.normalizedPath, '\\', '/');
		}
	}
	return this.normalizedPath;
}
 
Example 10
Source File: CompilationUnit.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public CompilationUnit(char[] contents, String fileName, String encoding,
		String destinationPath, boolean ignoreOptionalProblems) {
	this.contents = contents;
	char[] fileNameCharArray = fileName.toCharArray();
	switch(File.separatorChar) {
		case '/' :
			if (CharOperation.indexOf('\\', fileNameCharArray) != -1) {
				CharOperation.replace(fileNameCharArray, '\\', '/');
			}
			break;
		case '\\' :
			if (CharOperation.indexOf('/', fileNameCharArray) != -1) {
				CharOperation.replace(fileNameCharArray, '/', '\\');
			}
	}
	this.fileName = fileNameCharArray;
	int start = CharOperation.lastIndexOf(File.separatorChar, fileNameCharArray) + 1;

	int end = CharOperation.lastIndexOf('.', fileNameCharArray);
	if (end == -1) {
		end = fileNameCharArray.length;
	}

	this.mainTypeName = CharOperation.subarray(fileNameCharArray, start, end);
	this.encoding = encoding;
	this.destinationPath = destinationPath;
	this.ignoreOptionalProblems = ignoreOptionalProblems;
}
 
Example 11
Source File: DefaultBytecodeVisitor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private String returnClassName(char[] classInfoName) {
	if (classInfoName.length == 0) {
		return EMPTY_CLASS_NAME;
	} else if (isCompact()) {
		int lastIndexOfSlash = CharOperation.lastIndexOf('/', classInfoName);
		if (lastIndexOfSlash != -1) {
			return new String(classInfoName, lastIndexOfSlash + 1, classInfoName.length - lastIndexOfSlash - 1);
		}
	}
	CharOperation.replace(classInfoName, '/', '.');
	return new String(classInfoName);
}
 
Example 12
Source File: DefaultBytecodeVisitor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private String returnMethodSignature(IConstantPoolEntry constantMethodref) {
	final char[] methodDescriptor = constantMethodref.getMethodDescriptor();
	CharOperation.replace(methodDescriptor, '$', '#');
	final char[] signature = Util.toString(
			constantMethodref.getClassName(),
			constantMethodref.getMethodName(),
			methodDescriptor,
			true,
			isCompact()).toCharArray();
	CharOperation.replace(signature, '#', '$');
	return String.valueOf(signature);
}
 
Example 13
Source File: KeyToSignature.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public void consumeLocalType(char[] uniqueKey) {
	this.signature = new StringBuffer();
	// remove trailing semi-colon as it is added later in comsumeType()
	uniqueKey = CharOperation.subarray(uniqueKey, 0, uniqueKey.length-1);
	CharOperation.replace(uniqueKey, '/', '.');
	this.signature.append(uniqueKey);
}
 
Example 14
Source File: Disassembler.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void disassembleGenericSignature(int mode, StringBuffer buffer, final char[] signature) {
	CharOperation.replace(signature, '/', '.');
	final char[][] typeParameters = Signature.getTypeParameters(signature);
	final int typeParametersLength = typeParameters.length;
	if (typeParametersLength != 0) {
		buffer.append('<');
		for (int i = 0; i < typeParametersLength; i++) {
			if (i != 0) {
				buffer.append(Messages.disassembler_comma);
			}
			// extract the name
			buffer.append(typeParameters[i], 0, CharOperation.indexOf(':', typeParameters[i]));
			final char[][] bounds = Signature.getTypeParameterBounds(typeParameters[i]);
			final int boundsLength = bounds.length;
			if (boundsLength != 0) {
				if (boundsLength == 1) {
					final char[] bound = bounds[0];
					// check if this is java.lang.Object
					if (!isJavaLangObject(Signature.toCharArray(bound))) {
						buffer.append(" extends "); //$NON-NLS-1$
						buffer.append(returnClassName(Signature.toCharArray(bound), '.', mode));
					}
				} else {
					buffer.append(" extends "); //$NON-NLS-1$
					for (int j= 0; j < boundsLength; j++) {
						if (j != 0) {
							buffer.append(" & "); //$NON-NLS-1$
						}
						buffer.append(returnClassName(Signature.toCharArray(bounds[j]), '.', mode));
					}
				}
			}
		}
		buffer.append('>');
	}
}
 
Example 15
Source File: FileSystem.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private void initializeKnownFileNames(String[] initialFileNames) {
	if (initialFileNames == null) {
		this.knownFileNames = new HashSet(0);
		return;
	}
	this.knownFileNames = new HashSet(initialFileNames.length * 2);
	for (int i = initialFileNames.length; --i >= 0;) {
		File compilationUnitFile = new File(initialFileNames[i]);
		char[] fileName = null;
		try {
			fileName = compilationUnitFile.getCanonicalPath().toCharArray();
		} catch (IOException e) {
			// this should not happen as the file exists
			continue;
		}
		char[] matchingPathName = null;
		final int lastIndexOf = CharOperation.lastIndexOf('.', fileName);
		if (lastIndexOf != -1) {
			fileName = CharOperation.subarray(fileName, 0, lastIndexOf);
		}
		CharOperation.replace(fileName, '\\', '/');
		boolean globalPathMatches = false;
		// the most nested path should be the selected one
		for (int j = 0, max = this.classpaths.length; j < max; j++) {
			char[] matchCandidate = this.classpaths[j].normalizedPath();
			boolean currentPathMatch = false;
			if (this.classpaths[j] instanceof ClasspathDirectory
					&& CharOperation.prefixEquals(matchCandidate, fileName)) {
				currentPathMatch = true;
				if (matchingPathName == null) {
					matchingPathName = matchCandidate;
				} else {
					if (currentPathMatch) {
						// we have a second source folder that matches the path of the source file
						if (matchCandidate.length > matchingPathName.length) {
							// we want to preserve the shortest possible path
							matchingPathName = matchCandidate;
						}
					} else {
						// we want to preserve the shortest possible path
						if (!globalPathMatches && matchCandidate.length < matchingPathName.length) {
							matchingPathName = matchCandidate;
						}
					}
				}
				if (currentPathMatch) {
					globalPathMatches = true;
				}
			}
		}
		if (matchingPathName == null) {
			this.knownFileNames.add(new String(fileName)); // leave as is...
		} else {
			this.knownFileNames.add(new String(CharOperation.subarray(fileName, matchingPathName.length, fileName.length)));
		}
		matchingPathName = null;
	}
}
 
Example 16
Source File: Util.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
	 * Split signatures of all levels  from a type unique key.
	 *
	 * Example:
	 * 	For following type X<Y<Z>,V<W>,U>.A<B>, unique key is:
	 * 	"LX<LY<LZ;>;LV<LW;>;LU;>.LA<LB;>;"
	 *
	 * 	The return splitted signatures array is:
	 * 	[
	 * 		['L','X','<','L','Y','<','L','Z',';'>',';','L','V','<','L','W',';'>',';','L','U','>',';'],
	 * 		['L','A','<','L','B',';','>',';']
	 *
	 * @param typeSignature ParameterizedSourceType type signature
	 * @return char[][] Array of signatures for each level of given unique key
	 */
	public final static char[][] splitTypeLevelsSignature(String typeSignature) {
		// In case of IJavaElement signature, replace '$' by '.'
		char[] source = Signature.removeCapture(typeSignature.toCharArray());
		CharOperation.replace(source, '$', '.');

		// Init counters and arrays
		char[][] signatures = new char[10][];
		int signaturesCount = 0;
//		int[] lengthes = new int [10];
		int paramOpening = 0;

		// Scan each signature character
		for (int idx=0, ln = source.length; idx < ln; idx++) {
			switch (source[idx]) {
				case '>':
					paramOpening--;
					if (paramOpening == 0)  {
						if (signaturesCount == signatures.length) {
							System.arraycopy(signatures, 0, signatures = new char[signaturesCount+10][], 0, signaturesCount);
						}
					}
					break;
				case '<':
					paramOpening++;
					break;
				case '.':
					if (paramOpening == 0)  {
						if (signaturesCount == signatures.length) {
							System.arraycopy(signatures, 0, signatures = new char[signaturesCount+10][], 0, signaturesCount);
						}
						signatures[signaturesCount] = new char[idx+1];
						System.arraycopy(source, 0, signatures[signaturesCount], 0, idx);
						signatures[signaturesCount][idx] = Signature.C_SEMICOLON;
						signaturesCount++;
					}
					break;
				case '/':
					source[idx] = '.';
					break;
			}
		}

		// Resize signatures array
		char[][] typeSignatures = new char[signaturesCount+1][];
		typeSignatures[0] = source;
		for (int i=1, j=signaturesCount-1; i<=signaturesCount; i++, j--){
			typeSignatures[i] = signatures[j];
		}
		return typeSignatures;
	}
 
Example 17
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 18
Source File: NamedMember.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
protected String getKey(IMethod method, boolean forceOpen) throws JavaModelException {
	StringBuffer key = new StringBuffer();

	// declaring class
	String declaringKey = getKey((IType) method.getParent(), forceOpen);
	key.append(declaringKey);

	// selector
	key.append('.');
	String selector = method.getElementName();
	key.append(selector);

	// type parameters
	if (forceOpen) {
		ITypeParameter[] typeParameters = method.getTypeParameters();
		int length = typeParameters.length;
		if (length > 0) {
			key.append('<');
			for (int i = 0; i < length; i++) {
				ITypeParameter typeParameter = typeParameters[i];
				String[] bounds = typeParameter.getBounds();
				int boundsLength = bounds.length;
				char[][] boundSignatures = new char[boundsLength][];
				for (int j = 0; j < boundsLength; j++) {
					boundSignatures[j] = Signature.createCharArrayTypeSignature(bounds[j].toCharArray(), method.isBinary());
					CharOperation.replace(boundSignatures[j], '.', '/');
				}
				char[] sig = Signature.createTypeParameterSignature(typeParameter.getElementName().toCharArray(), boundSignatures);
				key.append(sig);
			}
			key.append('>');
		}
	}

	// parameters
	key.append('(');
	String[] parameters = method.getParameterTypes();
	for (int i = 0, length = parameters.length; i < length; i++)
		key.append(parameters[i].replace('.', '/'));
	key.append(')');

	// return type
	if (forceOpen)
		key.append(method.getReturnType().replace('.', '/'));
	else
		key.append('V');

	return key.toString();
}
 
Example 19
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 20
Source File: ClasspathLocation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Return the first access rule which is violated when accessing a given
 * type, or null if no 'non accessible' access rule applies.
 *
 * @param qualifiedBinaryFileName
 *            tested type specification, formed as:
 *            "org/eclipse/jdt/core/JavaCore.class"; on systems that
 *            use \ as File.separator, the
 *            "org\eclipse\jdt\core\JavaCore.class" is accepted as well
 * @return the first access rule which is violated when accessing a given
 *         type, or null if none applies
 */
protected AccessRestriction fetchAccessRestriction(String qualifiedBinaryFileName) {
	if (this.accessRuleSet == null)
		return null;
	char [] qualifiedTypeName = qualifiedBinaryFileName.
		substring(0, qualifiedBinaryFileName.length() - SUFFIX_CLASS.length)
		.toCharArray();
	if (File.separatorChar == '\\') {
		CharOperation.replace(qualifiedTypeName, File.separatorChar, '/');
	}
	return this.accessRuleSet.getViolatedRestriction(qualifiedTypeName);
}