List of usage examples for org.eclipse.jdt.core IMethod getResource
IResource getResource();
From source file:com.feup.contribution.druid.builder.DruidBuilder.java
License:Open Source License
private void processPostBuildAnnotations() throws CoreException { DruidProject project = DruidPlugin.getPlugin().getProject(getJavaProject()); for (BuilderAnnotation bAnnotation : postBuildAnnotations) { IAnnotation annotation = bAnnotation.getAnnotation(); IMethod method = bAnnotation.getMethod(); String annotationType = annotation.getElementName(); String unitName = bAnnotation.getUnitName(); String value = annotation.getMemberValuePairs()[0].getValue().toString(); String feature = extractFeature(value); String unit = extractUnit(value, unitName); int offset = annotation.getSourceRange().getOffset(); int length = annotation.getSourceRange().getLength(); ArrayList<String> featureNames = project.getFeatureNames(unitName, method); if (annotationType.equals("Depends")) if (!project.addDepends(unitName, featureNames, unit, feature, method.getResource(), offset, length))//from w w w .j av a2 s .c o m DruidMarker.addUndefinedFeatureMarker(annotation, feature); if (annotationType.equals("Tests")) if (!project.addTest(method, unit, feature)) DruidMarker.addUndefinedFeatureMarker(annotation, feature); if (annotationType.equals("Deprecates")) if (!project.addDeprecate(unitName, featureNames, unit, feature, method.getResource(), offset, length)) DruidMarker.addUndefinedFeatureMarker(annotation, feature); } postBuildAnnotations.clear(); }
From source file:com.feup.contribution.druid.data.DruidProject.java
License:Open Source License
public void detectInteractionsBuild(IProgressMonitor monitor) { ArrayList<DruidComponent> components = DruidComponent.getOrderedComponents(units); ArrayList<DruidComponent> toCompile = new ArrayList<DruidComponent>(); DruidTester tester = new DruidTester(); String cp = getClasspath();/* ww w .j a v a 2s . c om*/ removeDeprecatedBy(); removeOldMarkers(); monitor.beginTask("Detect Interactions", components.size() + 1); monitor.worked(1); int currentComponent = 1; for (DruidComponent component : components) { if (monitor.isCanceled()) return; monitor.subTask(component.toString()); toCompile.add(component); if (testDeprecatedFeatures(component)) return; for (DruidUnit druidUnit : component.getUnits()) druidUnit.addDeprecatedBy(); tester.setUpTest(toCompile); tester.compile(cp); for (DruidComponent druidComponent : toCompile) { for (DruidUnit druidUnit : druidComponent.getUnits()) { for (DruidFeature druidFeature : druidUnit.getFeatures()) { for (DruidTest druidTest : druidFeature.getTests()) { boolean result = tester.test(druidTest.getMethod(), cp); try { if (!result && component.getUnits().contains(druidUnit)) { String message = "Unit " + component.getUnits().get(0).getName() + ": test failed for feature " + druidFeature; IMethod method = druidTest.getMethod(); addMarker(FAILED_TEST, message, method.getResource(), method.getNameRange().getOffset(), method.getNameRange().getLength()); monitor.done(); showErrorDialog("Failed Test", message, tester.getDetails()); return; } else if (!druidFeature.isDeprecated() && !result) { String message = "Unit " + component.getUnits().get(0).getName() + " breaks feature " + druidFeature; IMethod method = druidTest.getMethod(); addMarker(FEATURE_BROKEN, message, method.getResource(), method.getNameRange().getOffset(), method.getNameRange().getLength()); monitor.done(); showErrorDialog("Feature Broken", message, ""); return; } else if (druidFeature.isDeprecated() && result) { String message = "Unit " + component.getUnits().get(0).getName() + " doesn't break feature " + druidFeature; DruidDeprecate deprecate = druidFeature.getDeprecatedBy().get(0); addMarker(FEATURE_NOT_BROKEN, message, deprecate.getResource(), deprecate.getOffset(), deprecate.getLength()); monitor.done(); showErrorDialog("Feature Not Broken", message, ""); return; } } catch (JavaModelException e) { DruidPlugin.getPlugin().logException(e); } } } } tester.tearDown(); } monitor.worked(++currentComponent); } monitor.done(); }
From source file:net.rim.ejde.internal.builders.CompilerToAppDescriptorManager.java
License:Open Source License
/** * Check for the error caused by having a libMain and not setting AutoStartup * * @param cu/*from ww w. jav a2 s.c om*/ * the cu * * @throws JavaModelException * the java model exception * @throws CoreException * the core exception */ private void findLibMainInCU(ICompilationUnit cu) throws JavaModelException, CoreException { IType primaryType; if (cu != null) { primaryType = cu.findPrimaryType(); if (primaryType == null) { if ((cu.getAllTypes() != null) && (cu.getAllTypes().length > 0)) { primaryType = cu.getAllTypes()[0]; } else { return; } } final IMethod libMainMethod = primaryType.getMethod("libMain", new String[] { "[QString;" }); //$NON-NLS-1$ //$NON-NLS-2$ if (libMainMethod.exists() && (libMainMethod.getExceptionTypes().length == 0) && Flags.isStatic(libMainMethod.getFlags()) && Flags.isPublic(libMainMethod.getFlags())) { final IJavaProject javaProject = cu.getJavaProject(); IProject project = javaProject.getProject(); if (NatureUtils.hasBBNature(project)) { BlackBerryProperties properties = ContextManager.PLUGIN.getBBProperties(project.getName(), false); if (properties._application.getType().equals(BlackBerryProject.LIBRARY) && !properties._application.isAutostartup().booleanValue()) { Display.getDefault().asyncExec(new Runnable() { @Override public void run() { IMarker problem; try { problem = libMainMethod.getResource() .createMarker(IRIMMarker.BLACKBERRY_PROBLEM); int startPos = libMainMethod.getSourceRange().getOffset(), endPos = startPos + libMainMethod.getSourceRange().getLength() - 1; problem.setAttributes( new String[] { IMarker.MESSAGE, IMarker.CHAR_START, IMarker.CHAR_END, IMarker.SEVERITY, IRIMMarker.ID }, new Object[] { Messages.CompilerToProjectEditorManager_libWarnMsg, Integer.valueOf(startPos), Integer.valueOf(endPos), Integer.valueOf(IMarker.SEVERITY_WARNING), Integer.valueOf(IRIMMarker.LIBMAIN_PROBLEM_ID) }); addManagedMarker(javaProject, problem); } catch (CoreException e) { _log.error("LibMainMethodManager.add: ", e); } } }); } } } } }
From source file:org.eclim.plugin.jdt.command.hierarchy.CallHierarchyCommand.java
License:Open Source License
@Override public Object execute(CommandLine commandLine) throws Exception { HashMap<String, Object> result = new HashMap<String, Object>(); String project = commandLine.getValue(Options.PROJECT_OPTION); String file = commandLine.getValue(Options.FILE_OPTION); boolean callees = commandLine.hasOption(CALLEES_OPTION); String scope = commandLine.getValue(Options.SCOPE_OPTION); int length = commandLine.getIntValue(Options.LENGTH_OPTION); int offset = getOffset(commandLine); ICompilationUnit src = JavaUtils.getCompilationUnit(project, file); IJavaElement[] elements = src.codeSelect(offset, length); if (elements == null || elements.length == 0) { return result; }//from ww w. j av a 2 s.c o m IJavaElement element = elements[0]; if (element instanceof IMethod) { IMethod method = (IMethod) element; IMember[] members = new IMember[] { method }; MethodWrapper[] roots; CallHierarchy callHierarchy = CallHierarchy.getDefault(); callHierarchy.setSearchScope(getScope(scope, src.getJavaProject())); Comparator<MethodWrapper> comparator = null; if (callees) { roots = callHierarchy.getCalleeRoots(members); } else { roots = callHierarchy.getCallerRoots(members); // Following Eclipse's GUI, callers are ordered // alphabetically, callees by position in function. comparator = new Comparator<MethodWrapper>() { public int compare(MethodWrapper o1, MethodWrapper o2) { return o1.getName().compareToIgnoreCase(o2.getName()); } }; } if (roots.length > 0) { // Is it possible to have multiple roots? If so we'll need // to change this. result = formatRoot(roots[0], comparator, callees); IResource resource = method.getResource(); ISourceRange sourceRange = method.getSourceRange(); // The root element doesn't get his location like all the others // (this happens with the GUI too). So add it ourselves. result.put("position", Position.fromOffset(resource.getLocation().toOSString(), null, sourceRange.getOffset(), sourceRange.getLength())); } } return result; }
From source file:org.eclipse.objectteams.otdt.internal.ui.compare.CompareBoundMethodsEditorInput.java
License:Open Source License
private String getEncoding(IMethod method) { IResource resource = method.getResource(); if (resource instanceof IFile) { IFile file = (IFile) resource;/*from w w w . j a v a2 s. co m*/ try { return file.getCharset(); } catch (CoreException e) { OTDTUIPlugin.log(e); } } return null; }
From source file:org.incha.core.jswingripples.MethodOverrideTester.java
License:Open Source License
/** * Finds all overrides/* w ww . jav a 2 s .c om*/ */ public IMethod findAllDeclaringMethods(final IMethod overriding, final boolean testVisibility, final HashSet<IMethod> overrides) throws JavaModelException { IMethod result = null; IMethod overridden = findOverriddenMethod(overriding, testVisibility); while (overridden != null) { if (overridden.getResource() == null) return null; overrides.add(overridden); result = overridden; overridden = findOverriddenMethod(result, testVisibility); } return result; }
From source file:org.jboss.tools.cdi.internal.core.validation.CDICoreValidator.java
License:Open Source License
/** * If the method is not a static method and is not a business method of the * session bean and is observer or disposer then mark it as incorrect. * //from w ww.ja v a 2s. co m * @param bean * @param method * @param annotatedParams * @param errorKey */ private void validateSessionBeanMethod(IClassBean bean, IBeanMethod method, Collection<ITextSourceReference> annotatedParams, String errorMessage, String preferencesKey, int id) { if (bean instanceof ISessionBean && annotatedParams != null) { IMethod iMethod = CDIUtil.getBusinessMethodDeclaration((SessionBean) bean, method); if (iMethod == null) { saveAllSuperTypesAsLinkedResources(bean); for (ITextSourceReference declaration : annotatedParams) { String bindedErrorMessage = NLS.bind(errorMessage, new String[] { method.getMethod().getElementName(), bean.getBeanClass().getElementName() }); addProblem(bindedErrorMessage, preferencesKey, declaration, bean.getResource(), id); } } else if (!isAsYouTypeValidation() && iMethod != method.getMethod() && !iMethod.isBinary()) { getValidationContext().addLinkedCoreResource(SHORT_ID, bean.getSourcePath().toOSString(), iMethod.getResource().getFullPath(), false); } } }
From source file:org.jboss.tools.cdi.internal.core.validation.CDICoreValidator.java
License:Open Source License
private void validateProducer(CDIValidationContext context, IProducer producer) { try {/*from w w w. j a v a 2s . c o m*/ Collection<ITypeDeclaration> typeDeclarations = producer.getAllTypeDeclarations(); String[] typeVariables = producer.getBeanClass().getTypeParameterSignatures(); ITypeDeclaration typeDeclaration = null; ITextSourceReference typeDeclarationReference = null; if (!typeDeclarations.isEmpty()) { /* * 3.3. Producer methods * - producer method return type contains a wildcard type parameter * * 2.2.1 Legal bean types * - a parameterized type that contains a wildcard type parameter is not a legal bean type. * * 3.4. Producer fields * - producer field type contains a wildcard type parameter */ typeDeclaration = typeDeclarations.iterator().next(); typeDeclarationReference = CDIUtil.convertToJavaSourceReference(typeDeclaration, producer.getSourceMember()); String[] paramTypes = Signature.getTypeArguments(typeDeclaration.getSignature()); boolean variable = false; for (String paramType : paramTypes) { if (Signature.getTypeSignatureKind(paramType) == Signature.WILDCARD_TYPE_SIGNATURE) { if (producer instanceof IProducerField) { addProblem(CDIValidationMessages.PRODUCER_FIELD_TYPE_HAS_WILDCARD, CDIPreferences.PRODUCER_METHOD_RETURN_TYPE_HAS_WILDCARD_OR_VARIABLE, typeDeclarationReference, producer.getResource()); } else { addProblem(CDIValidationMessages.PRODUCER_METHOD_RETURN_TYPE_HAS_WILDCARD, CDIPreferences.PRODUCER_METHOD_RETURN_TYPE_HAS_WILDCARD_OR_VARIABLE, typeDeclarationReference, producer.getResource()); } } else if (!variable && isTypeVariable(producer, Signature.toString(paramType), typeVariables)) { /* * 3.3. Producer methods * - producer method with a parameterized return type with a type variable declares any scope other than @Dependent * * 3.4. Producer fields * - producer field with a parameterized type with a type variable declares any scope other than @Dependent */ variable = true; IAnnotationDeclaration scopeOrStereotypeDeclaration = CDIUtil .getDifferentScopeDeclarationThanDepentend(producer); if (scopeOrStereotypeDeclaration != null) { boolean field = producer instanceof IProducerField; addProblem( field ? CDIValidationMessages.ILLEGAL_SCOPE_FOR_PRODUCER_FIELD : CDIValidationMessages.ILLEGAL_SCOPE_FOR_PRODUCER_METHOD, field ? CDIPreferences.ILLEGAL_SCOPE_FOR_BEAN : CDIPreferences.ILLEGAL_SCOPE_FOR_BEAN, scopeOrStereotypeDeclaration, producer.getResource()); } break; } } } /* * 3.3.2. Declaring a producer method * - producer method is annotated @Inject */ IAnnotationDeclaration inject = producer.getAnnotation(CDIConstants.INJECT_ANNOTATION_TYPE_NAME); if (inject != null) { addProblem(CDIValidationMessages.PRODUCER_ANNOTATED_INJECT, CDIPreferences.PRODUCER_ANNOTATED_INJECT, inject, inject.getResource() != null ? inject.getResource() : producer.getResource(), PRODUCER_ANNOTATED_INJECT_ID); } if (producer instanceof IProducerField) { /* * 3.5.1. Declaring a resource * - producer field declaration specifies an EL name (together with one of @Resource, @PersistenceContext, @PersistenceUnit, @EJB, @WebServiceRef) */ IProducerField producerField = (IProducerField) producer; if (producerField.getName() != null) { IAnnotationDeclaration declaration; for (String annotationType : RESOURCE_ANNOTATIONS) { declaration = producerField.getAnnotation(annotationType); if (declaration != null) { IAnnotationDeclaration nameDeclaration = producerField .getAnnotation(CDIConstants.NAMED_QUALIFIER_TYPE_NAME); if (nameDeclaration != null) { declaration = nameDeclaration; } addProblem(CDIValidationMessages.RESOURCE_PRODUCER_FIELD_SETS_EL_NAME, CDIPreferences.RESOURCE_PRODUCER_FIELD_SETS_EL_NAME, declaration, producer.getResource()); } } } /* * 3.4. Producer fields * - producer field type is a type variable */ if (typeVariables.length > 0) { String typeSign = producerField.getField().getTypeSignature(); String typeString = Signature.toString(typeSign); for (String variableSig : typeVariables) { String variableName = Signature.getTypeVariable(variableSig); if (typeString.equals(variableName)) { addProblem(CDIValidationMessages.PRODUCER_FIELD_TYPE_IS_VARIABLE, CDIPreferences.PRODUCER_METHOD_RETURN_TYPE_HAS_WILDCARD_OR_VARIABLE, typeDeclaration != null ? typeDeclarationReference : producer, producer.getResource()); } } } /* * 3.4.2. Declaring a producer field * - non-static field of a session bean class is annotated @Produces */ if (producer.getClassBean() instanceof ISessionBean && !Flags.isStatic(producerField.getField().getFlags())) { addProblem(CDIValidationMessages.ILLEGAL_PRODUCER_FIELD_IN_SESSION_BEAN, CDIPreferences.ILLEGAL_PRODUCER_METHOD_IN_SESSION_BEAN, producer.getProducesAnnotation(), producer.getResource(), ILLEGAL_PRODUCER_FIELD_IN_SESSION_BEAN_ID); } } else { IProducerMethod producerMethod = (IProducerMethod) producer; List<IParameter> params = producerMethod.getParameters(); Collection<ITextSourceReference> observesDeclarations = new ArrayList<ITextSourceReference>(); Collection<ITextSourceReference> disposalDeclarations = new ArrayList<ITextSourceReference>(); IAnnotationDeclaration producesDeclaration = producerMethod .getAnnotation(CDIConstants.PRODUCES_ANNOTATION_TYPE_NAME); if (producesDeclaration != null) { observesDeclarations.add(producesDeclaration); disposalDeclarations.add(producesDeclaration); } for (IParameter param : params) { /* * 3.3.6. Declaring a disposer method * - a disposer method is annotated @Produces. * * 3.3.2. Declaring a producer method * - a has a parameter annotated @Disposes */ ITextSourceReference declaration = param .getAnnotationPosition(CDIConstants.DISPOSES_ANNOTATION_TYPE_NAME); if (declaration != null) { disposalDeclarations.add(declaration); } /* * 3.3.2. Declaring a producer method * - a has a parameter annotated @Observers * * 10.4.2. Declaring an observer method * - an observer method is annotated @Produces */ declaration = param.getAnnotationPosition(CDIConstants.OBSERVERS_ANNOTATION_TYPE_NAME); if (declaration != null) { observesDeclarations.add(declaration); } } if (observesDeclarations.size() > 1) { for (ITextSourceReference declaration : observesDeclarations) { addProblem(CDIValidationMessages.PRODUCER_PARAMETER_ILLEGALLY_ANNOTATED_OBSERVES, CDIPreferences.PRODUCER_PARAMETER_ILLEGALLY_ANNOTATED, declaration, producer.getResource(), PRODUCER_PARAMETER_ILLEGALLY_ANNOTATED_OBSERVES_ID); } } if (disposalDeclarations.size() > 1) { for (ITextSourceReference declaration : disposalDeclarations) { addProblem(CDIValidationMessages.PRODUCER_PARAMETER_ILLEGALLY_ANNOTATED_DISPOSES, CDIPreferences.PRODUCER_PARAMETER_ILLEGALLY_ANNOTATED, declaration, producer.getResource(), PRODUCER_PARAMETER_ILLEGALLY_ANNOTATED_DISPOSES_ID); } } /* * 3.3. Producer methods * - producer method return type is a type variable * * 2.2.1 - Legal bean types * - a type variable is not a legal bean type */ String typeSign = producerMethod.getMethod().getReturnType(); String typeString = Signature.toString(typeSign); if (isTypeVariable(producerMethod, typeString, typeVariables)) { addProblem(CDIValidationMessages.PRODUCER_METHOD_RETURN_TYPE_IS_VARIABLE, CDIPreferences.PRODUCER_METHOD_RETURN_TYPE_HAS_WILDCARD_OR_VARIABLE, typeDeclaration != null ? typeDeclarationReference : producer, producer.getResource()); } /* * 3.3.2. Declaring a producer method * - non-static method of a session bean class is annotated @Produces, and the method is not a business method of the session bean */ IClassBean classBean = producer.getClassBean(); if (classBean instanceof ISessionBean) { IMethod method = CDIUtil.getBusinessMethodDeclaration((SessionBean) classBean, producerMethod); if (method == null) { String bindedErrorMessage = NLS.bind( CDIValidationMessages.ILLEGAL_PRODUCER_METHOD_IN_SESSION_BEAN, new String[] { producerMethod.getMethod().getElementName(), producer.getBeanClass().getElementName() }); addProblem(bindedErrorMessage, CDIPreferences.ILLEGAL_PRODUCER_METHOD_IN_SESSION_BEAN, producer.getProducesAnnotation(), producer.getResource(), ILLEGAL_PRODUCER_METHOD_IN_SESSION_BEAN_ID); saveAllSuperTypesAsLinkedResources(classBean); } else if (!isAsYouTypeValidation() && method != producerMethod.getMethod() && method.exists() && !method.isReadOnly()) { getValidationContext().addLinkedCoreResource(SHORT_ID, classBean.getSourcePath().toOSString(), method.getResource().getFullPath(), false); } } IAnnotationDeclaration sDeclaration = producerMethod.getSpecializesAnnotationDeclaration(); if (sDeclaration != null) { if (Flags.isStatic(producerMethod.getMethod().getFlags())) { /* * 3.3.3. Specializing a producer method * - method annotated @Specializes is static */ addProblem(CDIValidationMessages.ILLEGAL_SPECIALIZING_PRODUCER_STATIC, CDIPreferences.ILLEGAL_SPECIALIZING_BEAN, sDeclaration, producer.getResource()); } else { /* * 3.3.3. Specializing a producer method * - method annotated @Specializes does not directly override another producer method */ IMethod superMethod = CDIUtil.getDirectOverridingMethodDeclaration(producerMethod); boolean overrides = false; if (superMethod != null) { IType superType = superMethod.getDeclaringType(); if (superType.isBinary()) { IAnnotation[] ants = superMethod.getAnnotations(); for (IAnnotation an : ants) { if (CDIConstants.PRODUCES_ANNOTATION_TYPE_NAME.equals(an.getElementName())) { overrides = true; } } } else { Collection<IBean> beans = context.getCdiProject() .getBeans(superType.getResource().getFullPath()); for (IBean iBean : beans) { if (iBean instanceof IProducerMethod) { IProducerMethod prMethod = (IProducerMethod) iBean; if (prMethod.getMethod().isSimilar(superMethod)) { overrides = true; } } } } } if (!overrides) { addProblem(CDIValidationMessages.ILLEGAL_SPECIALIZING_PRODUCER_OVERRIDE, CDIPreferences.ILLEGAL_SPECIALIZING_BEAN, sDeclaration, producer.getResource()); } saveAllSuperTypesAsLinkedResources(producer.getClassBean()); } } } } catch (JavaModelException e) { CDICorePlugin.getDefault().logError(e); } }
From source file:org.jboss.tools.jsf.ui.el.refactoring.RenameMethodParticipant.java
License:Open Source License
@Override protected boolean initialize(Object element) { if (!getArguments().getUpdateReferences()) return false; if (element instanceof IMethod) { IMethod method = (IMethod) element; status = new RefactoringStatus(); rootChange = new CompositeChange(JsfUIMessages.RENAME_METHOD_PARTICIPANT_UPDATE_METHOD_REFERENCES); this.element = method; oldName = method.getElementName(); newName = RefactorSearcher.getPropertyName(method, getArguments().getNewName()); searcher = new RenameMethodSearcher((IFile) method.getResource(), oldName); return true; }/*from w w w .ja v a 2 s .com*/ return false; }
From source file:org.jboss.tools.jsf.ui.el.refactoring.RenameMethodParticipant.java
License:Open Source License
public boolean initialize(Object element, String newName) { if (element instanceof IMethod) { IMethod method = (IMethod) element; status = new RefactoringStatus(); rootChange = new CompositeChange(JsfUIMessages.RENAME_METHOD_PARTICIPANT_UPDATE_METHOD_REFERENCES); this.element = method; oldName = method.getElementName(); this.newName = newName; searcher = new RenameMethodSearcher((IFile) method.getResource(), oldName); return true; }/*from w w w. jav a 2 s. c o m*/ return false; }