Java Code Examples for org.eclipse.jdt.core.JavaModelException#printStackTrace()

The following examples show how to use org.eclipse.jdt.core.JavaModelException#printStackTrace() . 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: TypeCheckingEvolution.java    From JDeodorant with MIT License 6 votes vote down vote up
private List<TypeCheckElimination> generateTypeCheckEliminationsWithinJavaProject(IJavaProject javaProject, TypeCheckElimination elimination) {
	List<TypeCheckElimination> typeCheckEliminations = new ArrayList<TypeCheckElimination>();
	try {
		IPackageFragmentRoot[] iPackageFragmentRoots = javaProject.getPackageFragmentRoots();
		for(IPackageFragmentRoot iPackageFragmentRoot : iPackageFragmentRoots) {
			IJavaElement[] children = iPackageFragmentRoot.getChildren();
			for(IJavaElement child : children) {
				if(child.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
					IPackageFragment iPackageFragment = (IPackageFragment)child;
					ICompilationUnit[] iCompilationUnits = iPackageFragment.getCompilationUnits();
					for(ICompilationUnit iCompilationUnit : iCompilationUnits) {
						ASTParser parser = ASTParser.newParser(ASTReader.JLS);
				        parser.setKind(ASTParser.K_COMPILATION_UNIT);
				        parser.setSource(iCompilationUnit);
				        parser.setResolveBindings(true); // we need bindings later on
				        CompilationUnit compilationUnit = (CompilationUnit)parser.createAST(null);
						typeCheckEliminations.addAll(generateTypeCheckEliminationsWithinCompilationUnit(compilationUnit, elimination));
					}
				}
			}
		}
	} catch (JavaModelException e) {
		e.printStackTrace();
	}
	return typeCheckEliminations;
}
 
Example 2
Source File: MoveMethodRefactoring.java    From JDeodorant with MIT License 6 votes vote down vote up
private void removeAdditionalMethodsFromSourceClass() {
	Set<MethodDeclaration> methodsToBeMoved = new LinkedHashSet<MethodDeclaration>(additionalMethodsToBeMoved.values());
	for(MethodDeclaration methodDeclaration : methodsToBeMoved) {
		ASTRewrite sourceRewriter = ASTRewrite.create(sourceCompilationUnit.getAST());
		ListRewrite sourceClassBodyRewrite = sourceRewriter.getListRewrite(sourceTypeDeclaration, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
		sourceClassBodyRewrite.remove(methodDeclaration, null);
		try {
			TextEdit sourceEdit = sourceRewriter.rewriteAST();
			sourceMultiTextEdit.addChild(sourceEdit);
			sourceCompilationUnitChange.addTextEditGroup(new TextEditGroup("Remove additional moved method", new TextEdit[] {sourceEdit}));
		}
		catch(JavaModelException javaModelException) {
			javaModelException.printStackTrace();
		}
	}
}
 
Example 3
Source File: MoveMethodRefactoring.java    From JDeodorant with MIT License 6 votes vote down vote up
private void removeSourceMethod() {
	ASTRewrite sourceRewriter = ASTRewrite.create(sourceCompilationUnit.getAST());
	ListRewrite classBodyRewrite = sourceRewriter.getListRewrite(sourceTypeDeclaration, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
	classBodyRewrite.remove(sourceMethod, null);
	Set<MethodDeclaration> methodsToBeMoved = new LinkedHashSet<MethodDeclaration>(additionalMethodsToBeMoved.values());
	for(MethodDeclaration methodDeclaration : methodsToBeMoved) {
		classBodyRewrite.remove(methodDeclaration, null);
	}
	try {
		TextEdit sourceEdit = sourceRewriter.rewriteAST();
		sourceMultiTextEdit.addChild(sourceEdit);
		sourceCompilationUnitChange.addTextEditGroup(new TextEditGroup("Remove moved method", new TextEdit[] {sourceEdit}));
	}
	catch(JavaModelException javaModelException) {
		javaModelException.printStackTrace();
	}
}
 
Example 4
Source File: ReplaceTypeCodeWithStateStrategy.java    From JDeodorant with MIT License 6 votes vote down vote up
private void createStateField() {
	ASTRewrite sourceRewriter = ASTRewrite.create(sourceTypeDeclaration.getAST());
	AST contextAST = sourceTypeDeclaration.getAST();
	ListRewrite contextBodyRewrite = sourceRewriter.getListRewrite(sourceTypeDeclaration, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
	VariableDeclarationFragment typeFragment = createStateFieldVariableDeclarationFragment(sourceRewriter, contextAST);
	
	FieldDeclaration typeFieldDeclaration = contextAST.newFieldDeclaration(typeFragment);
	sourceRewriter.set(typeFieldDeclaration, FieldDeclaration.TYPE_PROPERTY, contextAST.newSimpleName(abstractClassName), null);
	ListRewrite typeFieldDeclarationModifiersRewrite = sourceRewriter.getListRewrite(typeFieldDeclaration, FieldDeclaration.MODIFIERS2_PROPERTY);
	typeFieldDeclarationModifiersRewrite.insertLast(contextAST.newModifier(Modifier.ModifierKeyword.PRIVATE_KEYWORD), null);
	contextBodyRewrite.insertBefore(typeFieldDeclaration, typeCheckElimination.getTypeField().getParent(), null);
	
	try {
		TextEdit sourceEdit = sourceRewriter.rewriteAST();
		ICompilationUnit sourceICompilationUnit = (ICompilationUnit)sourceCompilationUnit.getJavaElement();
		CompilationUnitChange change = compilationUnitChanges.get(sourceICompilationUnit);
		change.getEdit().addChild(sourceEdit);
		change.addTextEditGroup(new TextEditGroup("Create field holding the current state", new TextEdit[] {sourceEdit}));
	} catch (JavaModelException e) {
		e.printStackTrace();
	}
}
 
Example 5
Source File: CloneDetectorOutputParser.java    From JDeodorant with MIT License 6 votes vote down vote up
private CloneInstance getCloneInstance(int cloneInstanceIndex, ResourceInfo resourceInfo, CloneInstanceLocationInfo locationInfo) {
	CloneInstance cloneInstance = new CloneInstance(locationInfo, cloneInstanceIndex);
	cloneInstance.setSourceFolder(resourceInfo.getSourceFolder());
	cloneInstance.setPackageName(resourceInfo.getPackageName());
	cloneInstance.setClassName(resourceInfo.getClassName());
	IMethod iMethod = getIMethod(resourceInfo.getICompilationUnit(), resourceInfo.getCompilationUnit(), 
			locationInfo.getStartOffset(), locationInfo.getLength());
	if (iMethod != null) {
		cloneInstance.setMethodName(iMethod.getElementName());
		try {
			cloneInstance.setIMethodSignature(iMethod.getSignature());
		} catch (JavaModelException e) {
			e.printStackTrace();
		}
		cloneInstance.setMethodSignature(getMethodJavaSignature(iMethod));
		IJavaElement parent = iMethod.getParent();
		if (parent instanceof IType) {
			cloneInstance.setContainingClassFullyQualifiedName(((IType)parent).getFullyQualifiedName());
		}
	}
	return cloneInstance;
}
 
Example 6
Source File: GenericJavaLibraryDefaultValueProvider.java    From statecharts with Eclipse Public License 1.0 5 votes vote down vote up
private IStatus validateGeneratorClass(FeatureParameterValue parameterValue) {
	String value = parameterValue.getStringValue();
	IJavaProject ijp = JavaCore.create(this.getProject(parameterValue));
	try {
		if (ijp.findType(value) == null) {
			return error(String.format(CLASS_NOT_FOUND_ERROR, value));
		}
	} catch (JavaModelException e) {
		e.printStackTrace();
	}
	if (!value.matches(GENERATOR_CLASS_REGEX)) {
		return error(CLASS_FORMAT_ERROR);
	}
	return Status.OK_STATUS;
}
 
Example 7
Source File: SuperDevModeSrcArgumentProcessor.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Get a comma delimited list of the source directories relative to project.
 *
 * @param javaProject The java project.
 * @return csv of the source directories.
 */
private String getSrcDirectories(IJavaProject javaProject) {
  String src = "";

  IClasspathEntry[] cp = null;
  try {
    cp = javaProject.getRawClasspath();
  } catch (JavaModelException e) {
    e.printStackTrace();
  }

  // TODO maybe do a constant or some better error
  // Something had to go wrong with class path entries
  if (cp == null || cp.length == 0) {
    return "REPLACE_ME_WITH_src_dir";
  }

  for (int i = 0; i < cp.length; i++) {
    String path = getPathIfDir(javaProject, cp[i]);

    if (path != null && src.length() == 0) {
      src = path;
    } else if (path != null && src.length() > 0) {
      src = src + " -src " + path;
    }
  }

  // Something had to go wrong with the classpath entries, no source paths.
  if (src.length() == 0) {
    src = "REPLACE_ME_WITH_src_dir";
  }

  return src;
}
 
Example 8
Source File: TypeInfoViewer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private int getElementTypeCategory(TypeNameMatch type) {
	try {
		if (type.getPackageFragmentRoot().getKind() == IPackageFragmentRoot.K_SOURCE)
			return 0;
	} catch (JavaModelException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return 1;
}
 
Example 9
Source File: JavaSearchDocument.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public char[] getCharContents() {
	if (this.charContents != null) return this.charContents;
	try {
		return Util.getResourceContentsAsCharArray(getFile());
	} catch (JavaModelException e) {
		if (BasicSearchEngine.VERBOSE || JobManager.VERBOSE) { // used during search and during indexing
			e.printStackTrace();
		}
		return null;
	}
}
 
Example 10
Source File: SystemObject.java    From JDeodorant with MIT License 5 votes vote down vote up
public Set<ClassObject> getClassObjects(IPackageFragmentRoot packageFragmentRoot) {
  	Set<ClassObject> classObjectSet = new LinkedHashSet<ClassObject>();
  	try {
  		IJavaElement[] children = packageFragmentRoot.getChildren();
  		for(IJavaElement child : children) {
  			if(child.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
  				IPackageFragment packageFragment = (IPackageFragment)child;
  				classObjectSet.addAll(getClassObjects(packageFragment));
  			}
  		}
  	} catch(JavaModelException e) {
	e.printStackTrace();
}
  	return classObjectSet;
  }
 
Example 11
Source File: SystemObject.java    From JDeodorant with MIT License 5 votes vote down vote up
public Set<ClassObject> getClassObjects(IPackageFragment packageFragment) {
  	Set<ClassObject> classObjectSet = new LinkedHashSet<ClassObject>();
  	try {
  		ICompilationUnit[] packageCompilationUnits = packageFragment.getCompilationUnits();
	for(ICompilationUnit iCompilationUnit : packageCompilationUnits) {
		classObjectSet.addAll(getClassObjects(iCompilationUnit));
	}
  	} catch(JavaModelException e) {
	e.printStackTrace();
}
  	return classObjectSet;
  }
 
Example 12
Source File: SystemObject.java    From JDeodorant with MIT License 5 votes vote down vote up
public Set<ClassObject> getClassObjects(ICompilationUnit compilationUnit) {
  	Set<ClassObject> classObjectSet = new LinkedHashSet<ClassObject>();
try {
	IType[] topLevelTypes = compilationUnit.getTypes();
	for(IType type : topLevelTypes) {
		classObjectSet.addAll(getClassObjects(type));
	}
} catch(JavaModelException e) {
	e.printStackTrace();
}
  	return classObjectSet;
  }
 
Example 13
Source File: CompilationUnitCache.java    From JDeodorant with MIT License 5 votes vote down vote up
public void compilationUnitRemoved(ICompilationUnit compilationUnit) {
	try {
		if(compilationUnit.getCorrespondingResource() != null) {
			addedCompilationUnits.remove(compilationUnit);
			removedCompilationUnits.add(compilationUnit);
		}
	} catch (JavaModelException e) {
		e.printStackTrace();
	}
}
 
Example 14
Source File: CodeHighlightDecorator.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
private void decorateNamedMember(IDecoration decoration, NamedMember namedMember) {
	try {
		if( !namedMember.exists() )
			return;
		
		IResource resource = namedMember.getResource();
		
		if (resource == null )
			return;
		
		IMarker[] markers = getMarkersOfResource(resource, IResource.DEPTH_ZERO);
					
		ISourceRange range = namedMember.getSourceRange();
		
		int numberOfMarkersInRange = 0;
		
		for (IMarker marker : markers) {
			if (isMarkerInRange(range, marker)) {
				numberOfMarkersInRange++;
			}
		}
		
		if (numberOfMarkersInRange > 0) {
			applyDecorationOnContainer(decoration, numberOfMarkersInRange);
		}
	} catch (JavaModelException e) {
		e.printStackTrace();
	}
}
 
Example 15
Source File: ReplaceTypeCodeWithStateStrategy.java    From JDeodorant with MIT License 5 votes vote down vote up
private void removePrimitiveStateField() {
	ASTRewrite sourceRewriter = ASTRewrite.create(sourceTypeDeclaration.getAST());
	AST contextAST = sourceTypeDeclaration.getAST();
	ListRewrite contextBodyRewrite = sourceRewriter.getListRewrite(sourceTypeDeclaration, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
	FieldDeclaration[] fieldDeclarations = sourceTypeDeclaration.getFields();
	for(FieldDeclaration fieldDeclaration : fieldDeclarations) {
		List<VariableDeclarationFragment> fragments = fieldDeclaration.fragments();
		for(VariableDeclarationFragment fragment : fragments) {
			if(fragment.equals(typeCheckElimination.getTypeField())) {
				if(fragments.size() == 1) {
					contextBodyRewrite.remove(fragment.getParent(), null);
				}
				else {
					ListRewrite fragmentRewrite = sourceRewriter.getListRewrite(fragment.getParent(), FieldDeclaration.FRAGMENTS_PROPERTY);
					fragmentRewrite.remove(fragment, null);
				}
			}
		}
	}
	try {
		TextEdit sourceEdit = sourceRewriter.rewriteAST();
		ICompilationUnit sourceICompilationUnit = (ICompilationUnit)sourceCompilationUnit.getJavaElement();
		CompilationUnitChange change = compilationUnitChanges.get(sourceICompilationUnit);
		change.getEdit().addChild(sourceEdit);
		change.addTextEditGroup(new TextEditGroup("Remove primitive field holding the current state", new TextEdit[] {sourceEdit}));
	} catch (JavaModelException e) {
		e.printStackTrace();
	}
}
 
Example 16
Source File: ExtractClassInputPage.java    From JDeodorant with MIT License 4 votes vote down vote up
public ExtractClassInputPage(ExtractClassRefactoring refactoring) {
	super("Extracted Class Name");
	this.refactoring = refactoring;
	ICompilationUnit sourceCompilationUnit = (ICompilationUnit)refactoring.getSourceCompilationUnit().getJavaElement();
	this.parentPackage = (IPackageFragment)sourceCompilationUnit.getParent();
	this.parentPackageClassNames = new ArrayList<String>();
	try {
		for(ICompilationUnit compilationUnit : parentPackage.getCompilationUnits()) {
			String className = compilationUnit.getElementName();
			parentPackageClassNames.add(className.substring(0, className.indexOf(".java")));
		}
	} catch (JavaModelException e) {
		e.printStackTrace();
	}
	this.javaLangClassNames = new ArrayList<String>();
	this.javaLangClassNames.add("Boolean");
	this.javaLangClassNames.add("Byte");
	this.javaLangClassNames.add("Character");
	this.javaLangClassNames.add("Class");
	this.javaLangClassNames.add("Double");
	this.javaLangClassNames.add("Enum");
	this.javaLangClassNames.add("Error");
	this.javaLangClassNames.add("Exception");
	this.javaLangClassNames.add("Float");
	this.javaLangClassNames.add("Integer");
	this.javaLangClassNames.add("Long");
	this.javaLangClassNames.add("Math");
	this.javaLangClassNames.add("Number");
	this.javaLangClassNames.add("Object");
	this.javaLangClassNames.add("Package");
	this.javaLangClassNames.add("Process");
	this.javaLangClassNames.add("Runtime");
	this.javaLangClassNames.add("Short");
	this.javaLangClassNames.add("String");
	this.javaLangClassNames.add("StringBuffer");
	this.javaLangClassNames.add("StringBuilder");
	this.javaLangClassNames.add("System");
	this.javaLangClassNames.add("Thread");
	this.javaLangClassNames.add("Void");
	this.textMap = new LinkedHashMap<Text, String>();
	this.defaultNamingMap = new LinkedHashMap<Text, String>();
}
 
Example 17
Source File: ReplaceTypeCodeWithStateStrategyInputPage.java    From JDeodorant with MIT License 4 votes vote down vote up
public ReplaceTypeCodeWithStateStrategyInputPage(ReplaceTypeCodeWithStateStrategy refactoring) {
	super("State/Strategy Type Names");
	this.refactoring = refactoring;
	ICompilationUnit sourceCompilationUnit = (ICompilationUnit)refactoring.getSourceCompilationUnit().getJavaElement();
	this.parentPackage = (IPackageFragment)sourceCompilationUnit.getParent();
	this.parentPackageClassNames = new ArrayList<String>();
	try {
		for(ICompilationUnit compilationUnit : parentPackage.getCompilationUnits()) {
			String className = compilationUnit.getElementName();
			parentPackageClassNames.add(className.substring(0, className.indexOf(".java")));
		}
	} catch (JavaModelException e) {
		e.printStackTrace();
	}
	TypeVisitor typeVisitor = new TypeVisitor();
	refactoring.getSourceCompilationUnit().accept(typeVisitor);
	Set<ITypeBinding> typeBindings = typeVisitor.getTypeBindings();
	for(ITypeBinding typeBinding : typeBindings) {
		if(!parentPackageClassNames.contains(typeBinding.getName()) && !typeBinding.isNested()) {
			parentPackageClassNames.add(typeBinding.getName());
		}
	}
	this.javaLangClassNames = new ArrayList<String>();
	this.javaLangClassNames.add("Boolean");
	this.javaLangClassNames.add("Byte");
	this.javaLangClassNames.add("Character");
	this.javaLangClassNames.add("Class");
	this.javaLangClassNames.add("Double");
	this.javaLangClassNames.add("Enum");
	this.javaLangClassNames.add("Error");
	this.javaLangClassNames.add("Exception");
	this.javaLangClassNames.add("Float");
	this.javaLangClassNames.add("Integer");
	this.javaLangClassNames.add("Long");
	this.javaLangClassNames.add("Math");
	this.javaLangClassNames.add("Number");
	this.javaLangClassNames.add("Object");
	this.javaLangClassNames.add("Package");
	this.javaLangClassNames.add("Process");
	this.javaLangClassNames.add("Runtime");
	this.javaLangClassNames.add("Short");
	this.javaLangClassNames.add("String");
	this.javaLangClassNames.add("StringBuffer");
	this.javaLangClassNames.add("StringBuilder");
	this.javaLangClassNames.add("System");
	this.javaLangClassNames.add("Thread");
	this.javaLangClassNames.add("Void");
	this.textMap = new LinkedHashMap<Text, SimpleName>();
	this.defaultNamingMap = new LinkedHashMap<Text, String>();
}
 
Example 18
Source File: PolymorphismRefactoring.java    From JDeodorant with MIT License 4 votes vote down vote up
protected void generateGettersForAccessedFields() {
	AST contextAST = sourceTypeDeclaration.getAST();
	Set<VariableDeclarationFragment> accessedFields = new LinkedHashSet<VariableDeclarationFragment>();
	accessedFields.addAll(typeCheckElimination.getAccessedFields());
	accessedFields.addAll(typeCheckElimination.getSuperAccessedFields());
	for(VariableDeclarationFragment fragment : accessedFields) {
		if((fragment.resolveBinding().getModifiers() & Modifier.STATIC) == 0) {
			IMethodBinding getterMethodBinding = null;
			if(typeCheckElimination.getSuperAccessedFields().contains(fragment)) {
				for(IVariableBinding fieldBinding : typeCheckElimination.getSuperAccessedFieldBindings()) {
					if(fieldBinding.isEqualTo(fragment.resolveBinding())) {
						getterMethodBinding = typeCheckElimination.getGetterMethodBindingOfSuperAccessedField(fieldBinding);
						break;
					}
				}
			}
			else {
				getterMethodBinding = findGetterMethodInContext(fragment.resolveBinding());
			}
			if(getterMethodBinding == null) {
				FieldDeclaration fieldDeclaration = (FieldDeclaration)fragment.getParent();
				int modifiers = fieldDeclaration.getModifiers();
				if(!fragment.equals(typeCheckElimination.getTypeField()) &&
						!((modifiers & Modifier.PUBLIC) != 0 && (modifiers & Modifier.STATIC) != 0)) {
					ASTRewrite sourceRewriter = ASTRewrite.create(sourceTypeDeclaration.getAST());
					MethodDeclaration newMethodDeclaration = contextAST.newMethodDeclaration();
					sourceRewriter.set(newMethodDeclaration, MethodDeclaration.RETURN_TYPE2_PROPERTY, fieldDeclaration.getType(), null);
					ListRewrite methodDeclarationModifiersRewrite = sourceRewriter.getListRewrite(newMethodDeclaration, MethodDeclaration.MODIFIERS2_PROPERTY);
					methodDeclarationModifiersRewrite.insertLast(contextAST.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD), null);
					String methodName = fragment.getName().getIdentifier();
					methodName = "get" + methodName.substring(0,1).toUpperCase() + methodName.substring(1,methodName.length());
					sourceRewriter.set(newMethodDeclaration, MethodDeclaration.NAME_PROPERTY, contextAST.newSimpleName(methodName), null);
					Block methodDeclarationBody = contextAST.newBlock();
					ListRewrite methodDeclarationBodyStatementsRewrite = sourceRewriter.getListRewrite(methodDeclarationBody, Block.STATEMENTS_PROPERTY);
					ReturnStatement returnStatement = contextAST.newReturnStatement();
					sourceRewriter.set(returnStatement, ReturnStatement.EXPRESSION_PROPERTY, fragment.getName(), null);
					methodDeclarationBodyStatementsRewrite.insertLast(returnStatement, null);
					sourceRewriter.set(newMethodDeclaration, MethodDeclaration.BODY_PROPERTY, methodDeclarationBody, null);
					ListRewrite contextBodyRewrite = sourceRewriter.getListRewrite(sourceTypeDeclaration, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
					contextBodyRewrite.insertLast(newMethodDeclaration, null);
					try {
						TextEdit sourceEdit = sourceRewriter.rewriteAST();
						ICompilationUnit sourceICompilationUnit = (ICompilationUnit)sourceCompilationUnit.getJavaElement();
						CompilationUnitChange change = compilationUnitChanges.get(sourceICompilationUnit);
						change.getEdit().addChild(sourceEdit);
						change.addTextEditGroup(new TextEditGroup("Create getter method for accessed field", new TextEdit[] {sourceEdit}));
					} catch (JavaModelException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
}
 
Example 19
Source File: MultiOrganizeImportsHandler.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
private void collectRelevantFiles(Object element, Multimap<IProject, IFile> result) {
	try {
		if (element instanceof IJavaElement) {
			IJavaElement elem = (IJavaElement) element;
			if (elem.exists()) {
				switch (elem.getElementType()) {
					case IJavaElement.PACKAGE_FRAGMENT:
						collectRelevantFiles((IPackageFragment) elem, result);
						break;
					case IJavaElement.PACKAGE_FRAGMENT_ROOT:
						collectRelevantFiles((IPackageFragmentRoot) elem, result);
						break;
					case IJavaElement.JAVA_PROJECT:
						IPackageFragmentRoot[] roots;
						roots = ((IJavaProject) elem).getPackageFragmentRoots();
						for (int k = 0; k < roots.length; k++) {
							collectRelevantFiles(roots[k], result);
						}
						break;
				}
			}
		} else if (element instanceof LogicalPackage) {
			IPackageFragment[] packageFragments = ((LogicalPackage) element).getFragments();
			for (int k = 0; k < packageFragments.length; k++) {
				IPackageFragment pack = packageFragments[k];
				if (pack.exists()) {
					collectRelevantFiles(pack, result);
				}
			}
		} else if (element instanceof IWorkingSet) {
			IWorkingSet workingSet = (IWorkingSet) element;
			IAdaptable[] elements = workingSet.getElements();
			for (int j = 0; j < elements.length; j++) {
				collectRelevantFiles(elements[j], result);
			}
		} else if (element instanceof IFile) {
			collectIFiles(result, new Object[] { element });
		}
	} catch (JavaModelException e) {
		e.printStackTrace();
	}
}
 
Example 20
Source File: PolymorphismRefactoring.java    From JDeodorant with MIT License 4 votes vote down vote up
protected void generateSettersForAssignedFields() {
	AST contextAST = sourceTypeDeclaration.getAST();
	Set<VariableDeclarationFragment> assignedFields = new LinkedHashSet<VariableDeclarationFragment>();
	assignedFields.addAll(typeCheckElimination.getAssignedFields());
	assignedFields.addAll(typeCheckElimination.getSuperAssignedFields());
	for(VariableDeclarationFragment fragment : assignedFields) {
		IMethodBinding setterMethodBinding = null;
		if(typeCheckElimination.getSuperAssignedFields().contains(fragment)) {
			for(IVariableBinding fieldBinding : typeCheckElimination.getSuperAssignedFieldBindings()) {
				if(fieldBinding.isEqualTo(fragment.resolveBinding())) {
					setterMethodBinding = typeCheckElimination.getSetterMethodBindingOfSuperAssignedField(fieldBinding);
					break;
				}
			}
		}
		else {
			setterMethodBinding = findSetterMethodInContext(fragment.resolveBinding());
		}
		if(setterMethodBinding == null) {
			FieldDeclaration fieldDeclaration = (FieldDeclaration)fragment.getParent();
			if(!fragment.equals(typeCheckElimination.getTypeField())) {
				ASTRewrite sourceRewriter = ASTRewrite.create(sourceTypeDeclaration.getAST());
				MethodDeclaration newMethodDeclaration = contextAST.newMethodDeclaration();
				sourceRewriter.set(newMethodDeclaration, MethodDeclaration.RETURN_TYPE2_PROPERTY, contextAST.newPrimitiveType(PrimitiveType.VOID), null);
				ListRewrite methodDeclarationModifiersRewrite = sourceRewriter.getListRewrite(newMethodDeclaration, MethodDeclaration.MODIFIERS2_PROPERTY);
				methodDeclarationModifiersRewrite.insertLast(contextAST.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD), null);
				String methodName = fragment.getName().getIdentifier();
				methodName = "set" + methodName.substring(0,1).toUpperCase() + methodName.substring(1,methodName.length());
				sourceRewriter.set(newMethodDeclaration, MethodDeclaration.NAME_PROPERTY, contextAST.newSimpleName(methodName), null);
				ListRewrite methodDeclarationParametersRewrite = sourceRewriter.getListRewrite(newMethodDeclaration, MethodDeclaration.PARAMETERS_PROPERTY);
				SingleVariableDeclaration parameter = contextAST.newSingleVariableDeclaration();
				sourceRewriter.set(parameter, SingleVariableDeclaration.TYPE_PROPERTY, fieldDeclaration.getType(), null);
				sourceRewriter.set(parameter, SingleVariableDeclaration.NAME_PROPERTY, fragment.getName(), null);
				methodDeclarationParametersRewrite.insertLast(parameter, null);
				Block methodDeclarationBody = contextAST.newBlock();
				ListRewrite methodDeclarationBodyStatementsRewrite = sourceRewriter.getListRewrite(methodDeclarationBody, Block.STATEMENTS_PROPERTY);
				Assignment assignment = contextAST.newAssignment();
				sourceRewriter.set(assignment, Assignment.RIGHT_HAND_SIDE_PROPERTY, fragment.getName(), null);
				sourceRewriter.set(assignment, Assignment.OPERATOR_PROPERTY, Assignment.Operator.ASSIGN, null);
				FieldAccess fieldAccess = contextAST.newFieldAccess();
				sourceRewriter.set(fieldAccess, FieldAccess.EXPRESSION_PROPERTY, contextAST.newThisExpression(), null);
				sourceRewriter.set(fieldAccess, FieldAccess.NAME_PROPERTY, fragment.getName(), null);
				sourceRewriter.set(assignment, Assignment.LEFT_HAND_SIDE_PROPERTY, fieldAccess, null);
				ExpressionStatement expressionStatement = contextAST.newExpressionStatement(assignment);
				methodDeclarationBodyStatementsRewrite.insertLast(expressionStatement, null);
				sourceRewriter.set(newMethodDeclaration, MethodDeclaration.BODY_PROPERTY, methodDeclarationBody, null);
				ListRewrite contextBodyRewrite = sourceRewriter.getListRewrite(sourceTypeDeclaration, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
				contextBodyRewrite.insertLast(newMethodDeclaration, null);
				try {
					TextEdit sourceEdit = sourceRewriter.rewriteAST();
					ICompilationUnit sourceICompilationUnit = (ICompilationUnit)sourceCompilationUnit.getJavaElement();
					CompilationUnitChange change = compilationUnitChanges.get(sourceICompilationUnit);
					change.getEdit().addChild(sourceEdit);
					change.addTextEditGroup(new TextEditGroup("Create setter method for assigned field", new TextEdit[] {sourceEdit}));
				} catch (JavaModelException e) {
					e.printStackTrace();
				}
			}
		}
	}
}