Java Code Examples for org.apache.bcel.classfile.Method#isFinal()

The following examples show how to use org.apache.bcel.classfile.Method#isFinal() . 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: ClassDumper.java    From cloud-opensource-java with Apache License 2.0 6 votes vote down vote up
/**
 * Returns true if {@code parentJavaClass} is not {@code final} and {@code childJavaClass} is not
 * overriding any {@code final} method of {@code parentJavaClass}.
 *
 * @see <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.10">Java
 *     Virtual Machine Specification: 4.10. Verification of class Files</a>
 */
static boolean hasValidSuperclass(JavaClass childJavaClass, JavaClass parentJavaClass) {
  if (parentJavaClass.isFinal()) {
    return false;
  }

  for (Method method : childJavaClass.getMethods()) {
    for (JavaClass parentClass : getClassHierarchy(parentJavaClass)) {
      for (final Method methodInParent : parentClass.getMethods()) {
        if (methodInParent.getName().equals(method.getName())
            && methodInParent.getSignature().equals(method.getSignature())
            && methodInParent.isFinal()) {
          return false;
        }
      }
    }
  }

  return true;
}
 
Example 2
Source File: GenerateStubDialog.java    From j-j-jvm with Apache License 2.0 5 votes vote down vote up
protected String method2str(final Method method) {
  String modifier = "";

  if (method.isPrivate()) {
    modifier = "private ";
  } else if (method.isProtected()) {
    modifier = "protected ";
  } else if (method.isPublic()) {
    modifier = "public ";
  }

  if (method.isStatic()) {
    modifier += "static ";
  }

  if (method.isFinal()) {
    modifier += "final ";
  }

  modifier += method.getReturnType().toString();

  modifier += ' ' + method.getName();

  final StringBuilder buffer = new StringBuilder();
  org.apache.bcel.generic.Type[] argTypes = method.getArgumentTypes();

  for (int li = 0; li < argTypes.length; li++) {
    if (li > 0) {
      buffer.append(", ");
    }
    buffer.append(argTypes[li].toString());
  }

  modifier += '(' + buffer.toString() + ')';

  return modifier;
}
 
Example 3
Source File: MethodsGeneralPane.java    From ApkToolPlus with Apache License 2.0 4 votes vote down vote up
public void actionPerformed(ActionEvent event) {
	if (event.getSource() == addButton) {
		Method method = new Method();
		if (staticCB.isSelected()) {
			method.isStatic(true);
		}
		if (finalCB.isSelected()) {
			method.isFinal(true);
		}
		if (synchronizedCB.isSelected()) {
			method.isSynchronized(true);
		}
		if (nativeCB.isSelected()) {
			method.isNative(true);
		}
		if (abstractCB.isSelected()) {
			method.isAbstract(true);
		}
		if (strictCB.isSelected()) {
			method.isStrictfp(true);
		}
		int selectedItem = dropdown.getSelectedIndex();

		switch (selectedItem) {
		case 1:
			method.isPublic(true);
			break;
		case 2:
			method.isPrivate(true);
			break;
		case 3:
			method.isProtected(true);
			break;
		}

		String fileName = internalFrame.getFileName();
		int accessFlags = method.getAccessFlags();
		String methodName = name.getText();
		String methodDescriptor = descriptor.getText();
		ClassSaver classSaver = new ClassSaver(ClassSaver.ADD_METHOD, fileName,accessFlags, 
				methodName, methodDescriptor);
		ProgressDialog progressDialog = new ProgressDialog(
				internalFrame.getParentFrame(), null,
				"Adding method...");
		progressDialog.setRunnable(classSaver);
		progressDialog.setVisible(true);
		if (classSaver.exceptionOccured()) {
			ErrorReportWindow er = new ErrorReportWindow(internalFrame
					.getParentFrame(), classSaver.getExceptionVerbose(), "Adding method failed");

			er.pack();
			GUIHelper.centerOnParentWindow(er, internalFrame
					.getParentFrame());
			er.setVisible(true);
		} else {

			internalFrame.getParentFrame().doReload();
		}

	}

}
 
Example 4
Source File: FieldsGeneralPane.java    From ApkToolPlus with Apache License 2.0 4 votes vote down vote up
public void actionPerformed(ActionEvent event) {
	if (event.getSource() == addButton) {
		Method method = new Method();
		if (staticCB.isSelected()) {
			method.isStatic(true);
		}
		if (finalCB.isSelected()) {
			method.isFinal(true);
		}
		if (volatileCB.isSelected()) {
			method.isSynchronized(true);
		}
		if (transientCB.isSelected()) {
			method.isNative(true);
		}
		int selectedItem = dropdown.getSelectedIndex();

		switch (selectedItem) {
		case 1:
			method.isPublic(true);
			break;
		case 2:
			method.isPrivate(true);
			break;
		case 3:
			method.isProtected(true);
			break;
		}

		String fileName = internalFrame.getFileName();
		String fieldName = name.getText();
		String fieldDescriptor = descriptor.getText();
		int accessFlags = method.getAccessFlags();
		ClassSaver classSaver = new ClassSaver(ClassSaver.ADD_FIELD, fileName, fieldName, fieldDescriptor, accessFlags);
		ProgressDialog progressDialog = new ProgressDialog(
				internalFrame.getParentFrame(), null,
				"Adding interface...");
		progressDialog.setRunnable(classSaver);
		progressDialog.setVisible(true);
		if (classSaver.exceptionOccured()) {
			ErrorReportWindow er = new ErrorReportWindow(internalFrame
					.getParentFrame(), classSaver.getExceptionVerbose(), "Adding field failed");

			er.pack();
			GUIHelper.centerOnParentWindow(er, internalFrame
					.getParentFrame());
			er.setVisible(true);
		} else {

			internalFrame.getParentFrame().doReload();
		}
	}

}
 
Example 5
Source File: FindNoSideEffectMethods.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void visit(Method method) {
    constructor = method.getName().equals(Const.CONSTRUCTOR_NAME);
    classInit = method.getName().equals(Const.STATIC_INITIALIZER_NAME);
    calledMethods = new ArrayList<>();
    status = SideEffectStatus.NO_SIDE_EFFECT;
    if (hasNoSideEffect(getMethodDescriptor())) {
        handleStatus();
        return;
    }
    if (isObjectOnlyMethod(getMethodDescriptor())) {
        status = SideEffectStatus.OBJECT_ONLY;
    }
    if (method.isNative() || changedArg(getMethodDescriptor()) != -1) {
        status = SideEffectStatus.SIDE_EFFECT;
        handleStatus();
        return;
    }
    boolean sawImplementation = false;
    if (classInit) {
        superClinitCall();
    }
    if (!method.isStatic() && !method.isPrivate() && !method.isFinal() && !constructor && subtypes != null) {
        for (ClassDescriptor subtype : subtypes) {
            try {
                XClass xClass = Global.getAnalysisCache().getClassAnalysis(XClass.class, subtype);
                XMethod matchingMethod = xClass.findMatchingMethod(getMethodDescriptor());
                if (matchingMethod != null) {
                    sawImplementation = true;
                    sawCall(new MethodCall(matchingMethod.getMethodDescriptor(), TARGET_THIS), false);
                }
            } catch (CheckedAnalysisException e) {
            }
        }
    }
    if (method.isAbstract() || method.isInterface()) {
        if (!sawImplementation
                || getClassName().endsWith("Visitor") || getClassName().endsWith("Listener")
                || getClassName().startsWith("java/sql/")
                || (getClassName().equals("java/util/concurrent/Future") && !method.getName().startsWith("is"))
                || (getClassName().equals("java/lang/Process") && method.getName().equals("exitValue"))) {
            status = SideEffectStatus.SIDE_EFFECT;
        } else if (isObjectOnlyMethod(getMethodDescriptor())) {
            status = SideEffectStatus.OBJECT_ONLY;
        } else {
            String[] thrownExceptions = getXMethod().getThrownExceptions();
            if (thrownExceptions != null && thrownExceptions.length > 0) {
                status = SideEffectStatus.SIDE_EFFECT;
            }
        }
    }
    if ((status == SideEffectStatus.SIDE_EFFECT || status == SideEffectStatus.OBJECT_ONLY) || method.isAbstract()
            || method.isInterface() || method.isNative()) {
        handleStatus();
    }
}
 
Example 6
Source File: Pass2Verifier.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
/**
 * Ensures that <B>final</B> methods are not overridden.
 * <B>Precondition to run this method:
 * constant_pool_entries_satisfy_static_constraints() and
 * every_class_has_an_accessible_superclass() have to be invoked before
 * (in that order).</B>
 *
 * @throws ClassConstraintException otherwise.
 * @see #constant_pool_entries_satisfy_static_constraints()
 * @see #every_class_has_an_accessible_superclass()
 */
private void final_methods_are_not_overridden() {
    try {
    final Map<String, String> hashmap = new HashMap<>();
    JavaClass jc = Repository.lookupClass(myOwner.getClassName());

    int supidx = -1;
    while (supidx != 0) {
        supidx = jc.getSuperclassNameIndex();

        final Method[] methods = jc.getMethods();
        for (final Method method : methods) {
            final String nameAndSig = method.getName() + method.getSignature();

            if (hashmap.containsKey(nameAndSig)) {
                if (method.isFinal()) {
                    if (!(method.isPrivate())) {
                        throw new ClassConstraintException("Method '" + nameAndSig + "' in class '" + hashmap.get(nameAndSig) +
                            "' overrides the final (not-overridable) definition in class '" + jc.getClassName() + "'.");
                    }
                    addMessage("Method '" + nameAndSig + "' in class '" + hashmap.get(nameAndSig) +
                        "' overrides the final (not-overridable) definition in class '" + jc.getClassName() +
                        "'. This is okay, as the original definition was private; however this constraint leverage"+
                        " was introduced by JLS 8.4.6 (not vmspec2) and the behavior of the Sun verifiers.");
                } else {
                    if (!method.isStatic()) { // static methods don't inherit
                        hashmap.put(nameAndSig, jc.getClassName());
                    }
                }
            } else {
                if (!method.isStatic()) { // static methods don't inherit
                    hashmap.put(nameAndSig, jc.getClassName());
                }
            }
        }

        jc = Repository.lookupClass(jc.getSuperclassName());
        // Well, for OBJECT this returns OBJECT so it works (could return anything but must not throw an Exception).
    }

    } catch (final ClassNotFoundException e) {
    // FIXME: this might not be the best way to handle missing classes.
    throw new AssertionViolatedException("Missing class: " + e, e);
    }

}