public void testCustomPreferences() throws Exception {
   DiaGenSource s = createLibraryGen(false);
   final GenDiagram gd = s.getGenDiagram();
   GenCustomPreferencePage pp = GMFGenFactory.eINSTANCE.createGenCustomPreferencePage();
   if (gd.getPreferencePages().isEmpty()) {
     gd.getPreferencePages().add(pp);
   } else {
     gd.getPreferencePages().get(0).getChildren().add(pp);
   }
   pp.setGenerateBoilerplate(true);
   pp.setName("Page Name");
   pp.setQualifiedClassName(
       gd.getEditorGen().getEditor().getPackageName() + ".CustomPreferencePage");
   GenPreference p1 = GMFGenFactory.eINSTANCE.createGenPreference();
   p1.setName("PREF_XXX_ONE");
   p1.setDefaultValue("\"XXX_ONE_DEFAULT\"");
   GenPreference p2 = GMFGenFactory.eINSTANCE.createGenPreference();
   p2.setName("NO_PREFIX_XXX_TWO");
   p2.setKey("KEY.XXX.TWO");
   pp.getPreferences().add(p1);
   pp.getPreferences().add(p2);
   //
   generateAndCompile(s);
   //
   IProject generatedProject =
       ResourcesPlugin.getWorkspace().getRoot().getProject(gd.getEditorGen().getPlugin().getID());
   IFile file_pp =
       generatedProject.getFile("/src/" + pp.getQualifiedClassName().replace('.', '/') + ".java");
   assertTrue(file_pp.exists());
   ICompilationUnit cuPage = (ICompilationUnit) JavaCore.create(file_pp);
   assertNotNull(cuPage);
   IType mainClass = cuPage.getTypes()[0];
   assertNotNull(mainClass);
   assertEquals(2, mainClass.getFields().length);
   final IField p1field = mainClass.getField(p1.getName());
   final IField p2field = mainClass.getField(p2.getName());
   assertTrue(Flags.isPublic(p1field.getFlags()));
   assertTrue(Flags.isStatic(p1field.getFlags()));
   assertTrue(Flags.isPublic(p2field.getFlags()));
   assertTrue(Flags.isStatic(p2field.getFlags()));
   assertEquals('"' + p1.getKey() + '"', p1field.getConstant());
   assertEquals('"' + p2.getKey() + '"', p2field.getConstant());
   IMethod initMethod =
       mainClass.getMethod(
           "initDefaults", new String[] {"Q" + IPreferenceStore.class.getSimpleName() + ";"});
   assertNotNull(initMethod);
   String methodText = initMethod.getSource();
   assertTrue(methodText.indexOf(p1.getName()) != -1);
   assertTrue(methodText.indexOf(p1.getDefaultValue()) != -1);
   assertTrue(methodText.indexOf(p2.getName()) == -1);
 }
Exemple #2
0
 private static void populateClasses(
     final Shell shell, final IParent parent, final List<IType> types, final Filter filter) {
   try {
     for (final IJavaElement element : parent.getChildren()) {
       if (element instanceof IType) {
         final IType type = (IType) element;
         if (type.isClass()
             && type.isStructureKnown()
             && !type.isAnonymous()
             && !type.isLocal()
             && !Flags.isAbstract(type.getFlags())
             && Flags.isPublic(type.getFlags())
             && (filter == null || filter.accept(type))) {
           types.add(type);
         }
       } else if (element instanceof IParent
           && !element.getPath().toString().contains("/test/")
           && (!(element instanceof IPackageFragmentRoot)
               || !((IPackageFragmentRoot) element).isExternal())) {
         populateClasses(shell, (IParent) element, types, filter);
       }
     }
   } catch (final JavaModelException e) {
     Activator.error(e);
   }
 }
 @Override
 public boolean isPublic() {
   try {
     return Flags.isPublic(((IMember) tm).getFlags());
   } catch (JavaModelException e) {
     return false;
   }
 }
 /**
  * Returns if the given {@link IJavaElement} is externally visible <br>
  * <br>
  * Changes to the logic here must also be made in the {@link TagValidator} to ensure the
  * visibility is computed equally.
  *
  * @see TagValidator
  * @param element
  * @return <code>true</code> if the given element is visible <code>false</code> otherwise
  * @throws JavaModelException if a model lookup fails
  */
 boolean isVisible(IJavaElement element) throws JavaModelException {
   if (element != null) {
     switch (element.getElementType()) {
       case IJavaElement.FIELD:
       case IJavaElement.METHOD:
         {
           IMember member = (IMember) element;
           int flags = member.getFlags();
           IType type = member.getDeclaringType();
           if (Flags.isPublic(flags)
               || Flags.isProtected(flags)
               || (type != null && type.isInterface())) {
             return isVisible(type);
           }
           break;
         }
       case IJavaElement.TYPE:
         {
           IType type = (IType) element;
           int flags = type.getFlags();
           if (type.isLocal() && !type.isAnonymous() || Flags.isPrivate(flags)) {
             return false;
           }
           if (type.isMember()) {
             if ((Flags.isPublic(flags) && Flags.isStatic(flags))
                 || Flags.isPublic(flags)
                 || Flags.isProtected(flags)
                 || type.isInterface()) {
               return isVisible(type.getDeclaringType());
             }
           } else {
             return Flags.isPublic(flags) || type.isInterface();
           }
           break;
         }
       default:
         {
           break;
         }
     }
   }
   return false;
 }
 private boolean isVisible(TypeNameMatch curr, ICompilationUnit cu) {
   int flags = curr.getModifiers();
   if (Flags.isPrivate(flags)) {
     return false;
   }
   if (Flags.isPublic(flags) || Flags.isProtected(flags)) {
     return true;
   }
   return curr.getPackageName().equals(cu.getParent().getElementName());
 }
  /**
   * @since 2.3
   * @deprecated This method is not used anymore.
   */
  @Deprecated
  protected IType getPrimaryTypeFrom(ICompilationUnit cu) {
    try {
      if (cu.exists()) {
        IType primaryType = cu.findPrimaryType();
        if (primaryType != null) return primaryType;

        // if no exact match is found, return the first public type in CU (if any)
        for (IType type : cu.getTypes()) {
          if (Flags.isPublic(type.getFlags())) return type;
        }
      }
    } catch (JavaModelException e) {
      if (LOGGER.isDebugEnabled()) LOGGER.debug(e, e);
    }
    return null;
  }
  protected void setUp() throws Exception {
    super.setUp();
    fProject = new SWTTestProject();
    IType control = fProject.getProject().findType("org.eclipse.swt.widgets.Control");

    ExtractInterfaceProcessor processor =
        new ExtractInterfaceProcessor(
            control, JavaPreferencesSettings.getCodeGenerationSettings(fProject.getProject()));
    fRefactoring = new ProcessorBasedRefactoring(processor);

    IMethod[] methods = control.getMethods();
    List extractedMembers = new ArrayList();
    for (int i = 0; i < methods.length; i++) {
      IMethod method = methods[i];
      int flags = method.getFlags();
      if (Flags.isPublic(flags) && !Flags.isStatic(flags) && !method.isConstructor()) {
        extractedMembers.add(method);
      }
    }
    processor.setTypeName("IControl");
    processor.setExtractedMembers(
        (IMember[]) extractedMembers.toArray(new IMember[extractedMembers.size()]));
    processor.setReplace(true);
  }
  /**
   * Determines whether the given member is a valid android.view.View to be added to the list of
   * custom views or third party views. It checks that the view is public and not abstract for
   * example.
   */
  private static boolean isValidView(IType type, boolean layoutsOnly) throws JavaModelException {
    // Skip anonymous classes
    if (type.isAnonymous()) {
      return false;
    }
    int flags = type.getFlags();
    if (Flags.isAbstract(flags) || !Flags.isPublic(flags)) {
      return false;
    }

    // TODO: if (layoutsOnly) perhaps try to filter out AdapterViews and other ViewGroups
    // not willing to accept children via XML

    // See if the class has one of the acceptable constructors
    // needed for XML instantiation:
    //    View(Context context)
    //    View(Context context, AttributeSet attrs)
    //    View(Context context, AttributeSet attrs, int defStyle)
    // We don't simply do three direct checks via type.getMethod() because the types
    // are not resolved, so we don't know for each parameter if we will get the
    // fully qualified or the unqualified class names.
    // Instead, iterate over the methods and look for a match.
    String typeName = type.getElementName();
    for (IMethod method : type.getMethods()) {
      // Only care about constructors
      if (!method.getElementName().equals(typeName)) {
        continue;
      }

      String[] parameterTypes = method.getParameterTypes();
      if (parameterTypes == null || parameterTypes.length < 1 || parameterTypes.length > 3) {
        continue;
      }

      String first = parameterTypes[0];
      // Look for the parameter type signatures -- produced by
      // JDT's Signature.createTypeSignature("Context", false /*isResolved*/);.
      // This is not a typo; they were copy/pasted from the actual parameter names
      // observed in the debugger examining these data structures.
      if (first.equals("QContext;") // $NON-NLS-1$
          || first.equals("Qandroid.content.Context;")) { // $NON-NLS-1$
        if (parameterTypes.length == 1) {
          return true;
        }
        String second = parameterTypes[1];
        if (second.equals("QAttributeSet;") // $NON-NLS-1$
            || second.equals("Qandroid.util.AttributeSet;")) { // $NON-NLS-1$
          if (parameterTypes.length == 2) {
            return true;
          }
          String third = parameterTypes[2];
          if (third.equals("I")) { // $NON-NLS-1$
            if (parameterTypes.length == 3) {
              return true;
            }
          }
        }
      }
    }

    return false;
  }